如何在嵌套子文档 mongodb 中查找带有数组的字段? [英] How to lookup a field with an array in nested subdocument mongodb?

查看:68
本文介绍了如何在嵌套子文档 mongodb 中查找带有数组的字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

以下是数据示例:

{
      "_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 字段.它们指向另一个名为 levels 的集合中的 _id 字段.

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

但是我该怎么做才能得到这样的 json 响应?

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

我正在尝试使用 unwind、project 和 group.但我很困惑.

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........
                    }
                }
]);

推荐答案

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

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)

希望对您有所帮助!

这篇关于如何在嵌套子文档 mongodb 中查找带有数组的字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆