如何在嵌套的子文档mongodb中查找带有数组的字段?

时间:2019-05-07 09:16:50

标签: javascript node.js mongodb mongoose restify

我正在尝试为文档中的嵌入式数组检索一些查找数据。

以下是数据示例:

{
      "_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"
              }
          ]
      }
}

如您所见,teamInfo.challenges是一个数组,其中包含levelId字段。它们指向另一个称为“级别”的集合中的_id字段。

但是如何获得这样的json响应?

{
      "_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"
              }
          ]
      }
}

我正在尝试使用展开,项目和组。但是我很困惑。

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 个答案:

答案 0 :(得分:0)

您可以使用查找管道来处理嵌套查找

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)

希望获得帮助!