使用$ lookup mongodb填充到较深的级别 [英] Populate to the deep level using $lookup mongodb

查看:269
本文介绍了使用$ lookup mongodb填充到较深的级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用$ lookup来加入两个集合并从下面的查询中获取数据:

I am using $lookup to join two collections and get data from below query:

let condition = {status:{$ne:config.PROJECT_STATUS.completed}, assignId:mongoose.Types.ObjectId(req.params.id)};
Project.aggregate([
    {
      $match: condition  
    },
    {
      "$group":{
        "_id": "$_id"
      }
    },
    { 
      "$lookup": {
        "from": "worksheets",
        "let": { "projectId": "$_id" },
        "pipeline": [
          { "$match": { "$expr": { "$eq": [ "$projectId", "$$projectId" ] }}},
          { "$group": {_id:"$projectId", totalHours:{"$sum": "$hours"}}},
          { 
            "$lookup": {
              "from": "projects",
              "let": { "projectId": "$_id" },
              "pipeline": [
                { "$match": { "$expr": { "$eq": [ "$_id", "$$projectId" ] }}},
                { "$project": { "projectName": 1,"upworkdId":1,"status":1,"developers":1,"hoursApproved":1 }}
              ],
              "as": "project"
            }
          }
        ],
        "as": "projects"
      }
    }
])
.then((data)=>{
    res.json(data);
})

上面的查询给了我下面的结果:

And above query is giving me below result:

[
  {
    "_id": "5c0a4083753a321c6c4ee024",
    "projects": [
        {
            "_id": "5c0a4083753a321c6c4ee024",
            "totalHours": 11,
            "project": [
                {
                    "_id": "5c0a4083753a321c6c4ee024",
                    "hoursApproved": 24,
                    "developers": [
                        "5c0a29c697e71a0d28b910a9"
                    ],
                    "projectName": "fallbrook winery",
                    "status": "pending"
                }
            ]
        }
    ]
  }
]

现在,我想填充项目数组中的 developers 子字段.我该如何修改上面的代码以获取该信息.

Now i want to populate the developers subfield inside the project Array. How can i modify the above code to get that.

推荐答案

您可以使用

You can use $lookup one more deep level with the developers array.

类似这样的东西

Project.aggregate([
  { "$match": condition },
  { "$group": { "_id": "$_id" }},
  { "$lookup": {
    "from": "worksheets",
    "let": { "projectId": "$_id" },
    "pipeline": [
      { "$match": { "$expr": { "$eq": ["$projectId", "$$projectId"] } } },
      { "$group": { "_id": "$projectId", "totalHours": { "$sum": "$hours" } }},
      { "$lookup": {
        "from": "projects",
        "let": { "projectId": "$_id" },
        "pipeline": [
          { "$match": { "$expr": { "$eq": ["$_id", "$$projectId"] } } },
          { "$lookup": {
            "from": "developers",
            "let": { "developers": "$developers" },
            "pipeline": [
              { "$match": { "$expr": { "$in": ["$_id", "$$developers"] } } },
            ],
            "as": "developers"
          }},
          { "$project": { 
            "projectName": 1, "upworkdId": 1, "status": 1, "developers": 1,  "hoursApproved": 1
          }}
        ],
        "as": "project"
      }}
    ],
    "as": "projects"
  }}
])

这篇关于使用$ lookup mongodb填充到较深的级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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