繁体   English   中英

如何从 MongoDB 中的不同集合文档中查询所有参考 ID

[英]How to query all the reference ids as from different documents of collection in MongoDB

我有一个名为“ sectionstudents ”的集合,其中数据以以下样式保存:

{
    "_id" : ObjectId("605df416e91c9d54184921bb"),
    "course" : ObjectId("5fdcbcace5a1ba11191b0823"),
    "section" : ObjectId("60036d2ae3e2866e606845a8"),
    "student" : ObjectId("6027fdb087d0b94d18611db4"),
    
},
{
    "_id" : ObjectId("603605a3ce8d63116c5ce6fc"),
    "course" : ObjectId("5ffeec372b2234556439d1da"),
    "section" : ObjectId("6002fe44cf5e2b51a8d730e1"),
    "student" : ObjectId("6027fdb087d0b94d18611db4"),
    
},
{
    "_id" : ObjectId("602941880fce660b7ce2a79d"),
    "course" : ObjectId("5ffeec372b2234556439d1da"),
    "section" : ObjectId("6002fae3e58bc750b4394229"),
    "student" : ObjectId("6027fdb087d0b94d18611db4"),
    
}

我想从与“学生”objcetId 匹配的特定学生的文档中以数组的形式返回所有课程详细信息。 例如,如果 id为 6027fdb087d0b94d18611db4的学生有三门课程,我希望课程数组如下所示:

[
  {
    "_id" : ObjectId("5fdcbcace5a1ba11191b0823"),
    "courseCode" : "history 101",
    "title" : "History of Ancient Greek",
    "description" : "It is a long established fact
 },
 {
    "_id" : ObjectId("5ffeec372b2234556439d1da"),
    "courseCode" : "history 101",
    "title" : "History of Ancient Greek",
    "description" : "It is a long established fact that a reader 
 },
 

 {
    "_id" : ObjectId("5ffeec372b2234556439d1da"),
    "courseCode" : "history 101",
    "title" : "History of Ancient Greek",
    "description" : "It is a long established fact that a reader
  }
]

不只是喜欢

[
   ObjectId("5fdcbcace5a1ba11191b0823"),
   ObjectId("5ffeec372b2234556439d1da"),
   ObjectId("5ffeec372b2234556439d1da")
]
  1. 使用$match阶段过滤所需的学生。
  2. student分组结果并将所有course累积到courses数组中。
  3. 使用$lookup运算符执行与courses集合的连接。

尝试这个:

db.sectionstudents.aggregate([
    {
        $match: {
            "student" : ObjectId("6027fdb087d0b94d18611db4")
        }
    },
    {
        $group: {
            _id: "$student",
            courses: { $push: "$course" }
        }
    },
    {
        $lookup: {
            from: "courses",
            localField: "courses",
            foreignField: "_id",
            as: "courses"
        }
    }
])

Output:

{
    "_id" : ObjectId("6027fdb087d0b94d18611db4"),
    "courses" : [
        {
            "_id" : ObjectId("5fdcbcace5a1ba11191b0823"),
            "courseCode" : "history 101",
            "title" : "History of Ancient Greek",
            "description" : "It is a long established fact"
        },
        {
            "_id" : ObjectId("5ffeec372b2234556439d1da"),
            "courseCode" : "history 101",
            "title" : "History of Ancient Greek",
            "description" : "It is a long established fact that a reader"
        },
        {
            "_id" : ObjectId("5ffeec372b2234556439d1db"),
            "courseCode" : "history 101",
            "title" : "History of Ancient Greek",
            "description" : "It is a long established fact that a reader"
        }
    ]
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM