从另一个 collection 获取嵌套数组中 id 匹配的所有字段

Get all fields from another collection on id match in nested array

我有三个collection:

Collection 培训计划:

        {
            "_id": "622683098c0a837e9d8e62ca",
            
            "days": [
                {
                    "rest": false,
                    "training_id": "622662616c279faf3e0e00bd",
                    "_id": "622683098c0a837e9d8e62cb"
                },
                {
                    "rest": true,
                    "_id": "622683098c0a837e9d8e62cc"
                },
                {
                    "rest": false,
                    "training_id": "6226649f6c279faf3e0e00cc",
                    "_id": "622683098c0a837e9d8e62cd"
                },
                {
                    "rest": true,
                    "_id": "622683098c0a837e9d8e62ce"
                }
            ],
            "__v": 0
        }

每天,我将 training_id 存储为指向 training collection:

的外键
        {
            "_id": "622662616c279faf3e0e00bd",
            "name": "Core easy",
            "exerc": [
                {
                    "ex_id": "62265892d68c687412db2423",
                    "sets":4,
                    
                    "_id": "622662616c279faf3e0e00be"
                },
                {
                    "ex_id": "622655f383c6328b70380f72",
                    "sets":4,
                    
                    "_id": "622662616c279faf3e0e00c2"
                },
                {
                    "ex_id": "62265bb4b8a1af9d8532c7fd",
                   "sets":4,
                    "_id": "622662616c279faf3e0e00c7"
                }
            ],
            "__v": 0
        },
        {
            "_id": "6226649f6c279faf3e0e00cc",
            "name": "Core medium",
            "exerc": [
                {
                    "ex_id": "62265cd3b8a1af9d8532c802",
                    "sets":4,
                    "_id": "6226649f6c279faf3e0e00cd"
                },
                {
                    "ex_id": "62265bb4b8a1af9d8532c7fd",
                    "sets":4,
                    "_id": "6226649f6c279faf3e0e00d2"
                },
                {
                    "ex_id": "62265892d68c687412db2423",
                    "sets":4,
                    "_id": "6226649f6c279faf3e0e00d7"
                }
            ],
            "__v": 0
        }
    

每个训练都有数组 exec,其中包含对练习的引用 collection:

    {
        "_id": "6226526a96b270821aa418bd",
        "name": "Leg killer",
        "muscle": [
            "kvadriceps"
        ],
        "__v": 0
    },
    {
        "_id": "622655f383c6328b70380f72",
        "name": "Bulgarian squat",
        "muscle": [
            "kvadriceps",
            "zadnja loža",
            "gluteus"
        ],
        "__v": 0
    },
    {
        "_id": "62265892d68c687412db2423",
        "name": "Plank",
        "muscle": [
            "core",
            "trbušnjaci"
        ],
        "__v": 0
    },
    
    {
        "_id": "62265bb4b8a1af9d8532c7fd",
        "name": "Leg Raise",
        "muscle": [
            "core",
            "trbušnjaci"
        ],
        "linkVideo": "https://www.youtube.com/watch?v=l4kQd9eWclE",
        "__v": 0
    },
    
    {
        "_id": "62265cd3b8a1af9d8532c802",
        "name": "Sit up",
        "muscle": [
            "core",
            "trbušnjaci"
        ],
        "linkVideo": "https://www.youtube.com/watch?v=1fbU_MkV7NE",
        "__v": 0
    }

我尝试过聚合、嵌套查找(这不适合我,因为我必须在第一次查找后使用 unwind)、addField 和 map 但它不起作用。所以任何人都可以帮助我或推荐我应该走的路。我发现了类似的问题 $lookup with foreign field of array & nested foreign field 但它也没有答案。

我想要的结果应该是:

{
            "_id": "622683098c0a837e9d8e62ca",
            
            "days": [
                {
                    "rest": false,
                    "training_id": "622662616c279faf3e0e00bd",
                    "_id": "622683098c0a837e9d8e62cb",
                    "name": "Core easy",
                    "exerc": [
                    {
                        "ex_id": "62265892d68c687412db2423",
                        "name": "Plank",
                        "muscle": [
                        "core",
                        "trbušnjaci"
                         ],
                        "sets":4,
                        
                        "_id": "622662616c279faf3e0e00be"
                    },
                    {
                    "ex_id": "622655f383c6328b70380f72",
                     
        
                     "name": "Bulgarian squat",
                     "muscle": [
                        "kvadriceps",
                        "zadnja loža",
                        "gluteus"
                      ],
              
    
                    "sets":4,
                    
                    "_id": "622662616c279faf3e0e00c2"
                    }
                    ]
                },
                  

                  and so on...
                   
                
        }

我认为这可以满足您的需求,希望有更简单的方法。我认为您还没有考虑过的拼图是 "$mergeObjects"

db.trainingPlan.aggregate([
  { "$unwind": "$days" },
  { "$lookup": {
      "from": "training",
      "localField": "days.training_id",
      "foreignField": "_id",
      "as": "training"
    }
  },
  { "$unwind": "$training" },
  { "$unwind": "$training.exerc" },
  { "$set": {
      "days": {
        "$mergeObjects": [ "$training", "$days" ]
      }
    }
  },
  { "$lookup": {
      "from": "exec",
      "localField": "days.exerc.ex_id",
      "foreignField": "_id",
      "as": "exec"
    }
  },
  { "$set": {
      "days.exerc": {
        "$mergeObjects": [ { "$first": "$exec" }, "$days.exerc" ]
      }
    }
  },
  { "$unset": [ "__v", "days.__v", "days.exerc.__v" ] },
  { "$group": {
      "_id": "$_id",
      "days": { "$push": "$days" }
    }
  }
])

mongoplayground.net 上试用。