$ lookup结果mongodb的数量

时间:2017-12-04 06:32:44

标签: mongodb aggregation-framework

将mongodb与NodeJS驱动程序一起使用

我有2个收藏品。一个用于部门,另一个用于学生。

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

和学生的样本数据

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

以下是查询:

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

查询输出

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

我有两个问题

  1. 如何打印完整的学生数据。
  2. 如何获取每个部门的studentData计数

2 个答案:

答案 0 :(得分:9)

要获得完整的学生数据,您可以执行类似

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

这会给你这样的东西:

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

获取每个部门的studentData计数

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

这会给你这样的东西:

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

您可以将$push:$name更改为$push:$_id如果您想存储学生的ID而不是他们的名字。

修改 使用“Departments”集合获得类似的结果:

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

这会给你这样的东西:

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

答案 1 :(得分:0)

您的 $lookup 完全没问题。要将学生人数添加到您的输出数据中,您可以使用 $addFields。

将此添加到您的聚合中:

{ $addFields: {studentCount: {$size: "$studentData"}}}

}

因此,您的查询应该是这样的:

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