count of $lookup result mongodb

时光毁灭记忆、已成空白 提交于 2021-02-07 05:36:05

问题


Using mongodb with NodeJS driver

I have 2 collections. One for department and other for students.

Sample data for Deparmtent.

{ 
 "_id" : ObjectId("5a24d20590d3d12155f3094e"), "name" : "CSE", "hod" : 
 "abc", 
 "students" : [ 
  ObjectId("5a2129172c3e542acb78c1f5"),
  ObjectId("5a2129172c3e542acb78c1f7"), 
  ObjectId("5a2129172c3e542acb78c1f9"), 
  ObjectId("5a2129172c3e542acb78c1fb")
 ]
}

{
 "_id" : ObjectId("5a24d20590d3d12155f3094f"), 
 "name" : "IT", 
 "hod" : "xyz", , 
 "students" : [ 
  ObjectId("5a2129172c3e542acb78c1f6"),
  ObjectId("5a2129172c3e542acb78c1f8"), 
  ObjectId("5a2129172c3e542acb78c1fa"),
  ObjectId("5a2129172c3e542acb78c1fc") 
 ] 
}

and sample data for Students

{
    "_id" : ObjectId("5a2129172c3e542acb78c1f4"),
    "rollNumber" : 11,
    "name" : "Thor",
    "branch" : ObjectId("5a24d20590d3d12155f3094e"),
}

Here is the query:

aggregate([
   {$lookup: 
     { from: "students", 
       localField: "_id", 
       foreignField: "branch", as: "studentData"
     }
   }
])

Output of the query

[ 
  { _id: 5a24d20590d3d12155f3094e,
    name: 'CSE',
    hod: 'abc',
    studentData: [ [Object], [Object], [Object], [Object], [Object] ] 
  },
  { _id: 5a24d20590d3d12155f3094f,
    name: 'IT',
    hod: 'xyz',
    studentData: [ [Object], [Object], [Object], [Object] ] } 
]

I have two questions

  1. How to print complete student data.
  2. How to get the count of studentData for each Department

回答1:


To get complete student data you can do something like

db.student.aggregate(
        [
            {
            $lookup:
                {
                    from:"department",
                    localField:"branch",
                    foreignField:"_id",
                    as:"branch"
                }
            }
        ]
    )

This will give you something like this:

{
    "_id" : 1.0,
    "rollNumber" : 110.0,
    "name" : "Thor",
    "branch" : [ 
        {
            "_id" : 1.0,
            "name" : "CSE",
            "hod" : "abc"
        }
    ]
}

To get the count of studentData for each Department

db.getCollection('student').aggregate(
    [
        {
        $lookup:
            {
                from:"department",
                localField:"branch",
                foreignField:"_id",
                as:"branch"
            }
        },
        {
            $group:
            {
                _id:"$branch",
                "numOfStudent":{$sum:1},
                "listOfStudents":{$push:"$name"}
            }
        }
    ]
)

This will give you something like this:

{
    "_id" : [ 
        {
            "_id" : 2.0,
            "name" : "IT",
            "hod" : "xyz"
        }
    ],
    "numOfStudent" : 1.0,
    "listOfStudents" : [ 
        "Ironman2"
    ]
}
{
    "_id" : [ 
        {
            "_id" : 1.0,
            "name" : "CSE",
            "hod" : "abc"
        }
    ],
    "numOfStudent" : 3.0,
    "listOfStudents" : [ 
        "Thor", 
        "Ironman", 
        "Ironman3"
    ]
}

You can change $push:$name to $push:$_id If you want to store the Ids of students and not their names.

EDIT Get similar result using "Departments" collection:

db.department.aggregate([
{
    $lookup:
                {
                    from:"student",
                    localField:"students",
                    foreignField:"_id",
                    as:"studentsDetails"
                }
},
{
    $project:{
            _id:0,
            name:"$name",
            hod:"$hod",
            numOfStudents:{$size:"$studentsDetails"},
            students:"$studentsDetails"
        }
}
])

This will give you something like this:

{
    "name" : "CSE",
    "hod" : "abc",
    "numOfStudents" : 2,
    "students" : [ 
        {
            "_id" : 1.0,
            "rollNumber" : 110.0,
            "name" : "Thor",
            "branch" : 1.0
        }, 
        {
            "_id" : 3.0,
            "rollNumber" : 111.0,
            "name" : "Ironman2",
            "branch" : 2.0
        }
    ]
}


来源:https://stackoverflow.com/questions/47627701/count-of-lookup-result-mongodb

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