Get Count of specific field mongodb

北城余情 提交于 2019-12-24 11:43:11

问题


I am using below query to get combined data from users and project collections:

db.collection.aggregate([
 { 
   "$group": {
     "_id": "$userId",
     "projectId": { "$push": "$projectId" }
    }
 },
 { 
   "$lookup": {
     "from": "users",
     "let": { "userId": "$_id" },
     "pipeline": [
        { "$match": { "$expr": { "$eq": [ "$_id", "$$userId" ] }}},
        { "$project": { "firstName": 1 }}
      ],
     "as": "user"
    }
 },
 { "$unwind": "$user" },
 {
   "$lookup": {
     "from": "projects",
     "let": { "projectId": "$projectId" },
     "pipeline": [
        { "$match": { "$expr": { "$in": [ "$_id", "$$projectId" ] }}},
        { "$project": { "projectName": 1 }}
      ],
     "as": "projects"
    }
 }
])

and it results like below:

[
  {
    "_id": "5c0a29e597e71a0d28b910aa",
    "projectId": [
        "5c0a2a8897e71a0d28b910ac",
        "5c0a4083753a321c6c4ee024"
    ],
    "user": {
        "_id": "5c0a29e597e71a0d28b910aa",
        "firstName": "Amit"
    },
    "projects": [
        {
            "_id": "5c0a2a8897e71a0d28b910ac",
            "projectName": "LN-PM"
        },
        {
            "_id": "5c0a4083753a321c6c4ee024",
            "projectName": "fallbrook winery"
        }
    ]
  },
  {
    "_id": "5c0a29c697e71a0d28b910a9",
    "projectId": [
        "5c0a4083753a321c6c4ee024"
    ],
    "user": {
        "_id": "5c0a29c697e71a0d28b910a9",
        "firstName": "Rajat"
    },
    "projects": [
        {
            "_id": "5c0a4083753a321c6c4ee024",
            "projectName": "fallbrook winery"
        }
    ]
  }
]

Now i have another table "Worksheets" and want to include hours field in projects Array, which will be calculated from the worksheets table by specifying the projectId which is _id in the projects array. It will be find in worksheet table and hours will be incremented how many times this _id has in worksheets table. Below is my worksheet collection:

{
  "_id" : ObjectId("5c0a4efa91b5021228681f7a"),
  "projectId" : ObjectId("5c0a4083753a321c6c4ee024"),
  "hours" : 8,
  "userId" : ObjectId("5c0a29c697e71a0d28b910a9"),
  "__v" : 0 
}

{
 "_id" : ObjectId("5c0a4f4191b5021228681f7c"),
  "projectId" : ObjectId("5c0a2a8897e71a0d28b910ac"),
  "hours" : 6,
  "userId" : ObjectId("5c0a29e597e71a0d28b910aa"),
 "__v" : 0
}

The result will look like below:

{
    "_id": "5c0a29c697e71a0d28b910a9",
    "projectId": [
        "5c0a4083753a321c6c4ee024"
    ],
    "user": {
        "_id": "5c0a29c697e71a0d28b910a9",
        "firstName": "Rajat"
    },
    "projects": [
        {
            "_id": "5c0a4083753a321c6c4ee024",
            "projectName": "fallbrook winery",
             "hours":8
        }
    ]
}

回答1:


You can use below aggregation

$lookup 3.6 nested syntax allows you to join nested collection inside the $lookup pipeline. You can perform all the aggregation inside the nested $lookup pipline

db.collection.aggregate([
  { "$group": {
    "_id": "$userId",
    "projectId": { "$push": "$projectId" }
  }},
  { "$lookup": {
    "from": "users",
    "let": { "userId": "$_id" },
    "pipeline": [
      { "$match": { "$expr": { "$eq": [ "$_id", "$$userId" ] }}},
      { "$project": { "firstName": 1 }}
    ],
    "as": "user"
  }},
  { "$unwind": "$user" },
  { "$lookup": {
    "from": "projects",
    "let": { "projectId": "$projectId" },
    "pipeline": [
      { "$match": { "$expr": { "$in": [ "$_id", "$$projectId" ] }}},
      { "$lookup": {
        "from": "worksheets",
        "let": { "projectId": "$_id" },
        "pipeline": [
          { "$match": { "$expr": { "$eq": [ "$projectId", "$$projectId" ] }}},
          { "$group": {
            "_id": "$projectId",
            "totalHours": { "$sum": "$hours" }
          }}
        ],
        "as": "workHours"
      }}
      { "$project": {
        "projectName": 1,
        "hours": { "$arrayElemAt": ["$workHours.totalHours", 0] }
      }}
    ],
    "as": "projects"
  }}
])


来源:https://stackoverflow.com/questions/53704352/get-count-of-specific-field-mongodb

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