lookup结果的计数mongodb

lookup结果的计数mongodb

本文介绍了$ lookup结果的计数mongodb的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将mongodb与NodeJS驱动程序配合使用

我有2个收藏集.一个用于部门,另一个用于学生.

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

Deparmtent的样本数据.

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

和学生样本数据

{
    "_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计数

推荐答案

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

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

如果要存储学生的ID而不是他们的姓名,则可以将 $ push:$ name 更改为 $ push:$ _ id .

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

编辑使用部门"集合获得相似的结果:

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

这会给你这样的东西:

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

这篇关于$ lookup结果的计数mongodb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 20:21