How to lookup a field with an array in nested subdocument mongodb?

眉间皱痕 提交于 2021-01-27 22:57:57

问题


I am trying to retrieve some lookup data for an embedded array in a document.

Here is a sample of the data:

{
      "_id": "58a4fa0e24180825b05e14e9",
      "fullname": "Test User",
      "username": "testuser"
      "teamInfo": {
          "challenges": [
              {
                  "levelId": "5e14e958a4fa0",
                  "title": "test challenge 1.1"
              },
              {
                  "levelId": "5e14e958a4fa0",
                  "title": "test challenge 1.2"
              },
              {
                  "levelId": "5e14e958a4fa1",
                  "title": "test challenge 2.1"
              }
          ]
      }
}

As you see, teamInfo.challenges is an array, containing levelId fields. These are pointing to the _id field in another collection called levels.

But how can I do to getting json response like this?

{
      "_id": "58a4fa0e24180825b05e14e9",
      "fullname": "Test User",
      "username": "testuser"
      "teamInfo": {
          "challenges": [
              {
                  "levelInfo": {
                      "name": "Level 1"
                  },
                  "title": "test challenge 1.1"
              },
              {
                  "levelInfo": {
                      "name": "Level 1"
                  },
                  "title": "test challenge 1.2"
              },
              {
                  "levelInfo": {
                      "name": "Level 2"
                  },
                  "title": "test challenge 2.1"
              }
          ]
      }
}

Im trying using unwind, project, and group. But im so confused.

const user = await User.aggregate([
                {
                    $match: {_id: new mongoose.Types.ObjectId(req.user.userId)}
                },
                {
                    $lookup: {
                        from: 'levels',
                        localField: 'teamInfo.challenges.levelId',
                        foreignField: '_id',
                        as: 'challLevelInfo'
                    }
                },
                {
                    $group: {
                        _id: "$_id",
                        ........IM CONFUSED HERE........
                    }
                }
]);

回答1:


You can use lookup pipeline to handle nested lookup

const pipeline = [
   {
     $match: {_id: new mongoose.Types.ObjectId(req.user.userId)}
   },
   {
     $lookup: {
       from: 'levels',
       let: { level_id: "$teamInfo.challenges.levelId" },
       pipeline: [
         {
           $match: {
             $expr: {
               $eq: ["$_id", "$$level_id"]
             }
           }
         },
         {
           $lookup: {
             from: '<level collection>',
             localField: "levelId",
             foreignField: "_id",
             as: "levelInfo"
           }
         },
         {
           $project: {
             levelInfo: {
               name: "$levelInfo.name"
             }
             title: 1
           }
         }
       ],
       as: "challenges"
     },

   },
   { $project: {
     _id: 1,
     fullname: 1,
     username: 1,
     teamInfo: {
       challenges: "$challenges"
     }
   }}
]

const result = await User.Aggregate(pipeline)

hope this help !



来源:https://stackoverflow.com/questions/56019324/how-to-lookup-a-field-with-an-array-in-nested-subdocument-mongodb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!