Mongo Join其中reference是另一个集合上的嵌入文档

时间:2018-06-07 10:43:15

标签: mongodb mongodb-query pymongo

我正在尝试加入两个mongo集合,其中外键是另一个集合中的嵌入式文档

例如,

引用嵌入文档的集合如下所示

{"StudentReport": {
    StudentId: "stud1"
    Exams : ["Quarter1","Qurater2","Quarter3"]
}

学生集合看起来像这样

{ 
    "_id":"stud1",
    "Quarter1":{"_id":Q1, "marks":90},
    "Quarter2":{"_id":Q2,"marks":80},
    "Quarter3":{"_id":Q3,"marks":70},
    "Quarter4":{"_id":Q4,"marks":80},
}

我期望的输出是

   {"StudentReport": {
        StudentId: "stud1"
        "Quarter1":{"_id":Q1, "marks":90},
        "Quarter2":{"_id":Q2,"marks":80},
        "Quarter3":{"_id":Q3,"marks":70},
        "Quarter4":{"_id":Q4,"marks":80},
    }

以下是我尝试过的查询示例

{"$unwind": "Exams"},
{"$lookup":
    {
         "from": "Student",
         "localField": "Exams",
         "foreignField": **"<What goes here that will fetch me the output>"**,
         "as": "model"
     }
 },

我尝试了查找,展开等的混合物,无法真正实现结果。

0 个答案:

没有答案