进行合并并以相同结构合并外部文档

时间:2018-10-10 10:45:20

标签: mongodb mongodb-query aggregation-framework

我有两个集合用于构建调查表:

调查收集有一个文档:

{
    "_id" : ObjectId("5bbcebfc2436f1d3e6275a5c"),
    "name" : "Some survey name",
    "score" : 12,
    "evidences" : [ 
        {
            "name" : "Evidence Method 1",
            "sections" : [ 
                {
                    "name" : "section1",
                    "questions" : [ 
                        "QID1", 
                        "QID2"
                    ]
                }, 
                {
                    "name" : "section2",
                    "questions" : [ 
                        "QID3", 
                        "QID4"
                    ]
                }
            ]
        }, 
        {
            "name" : "Evidence method 2",
            "sections" : [ 
                {
                    "name" : "section1",
                    "questions" : [ 
                        "QID5"
                    ]
                }, 
                {
                    "name" : "section2",
                    "questions" : [ 
                        "QID6"
                    ]
                }
            ]
        }
    ]
}

Question Collection具有以下文档:

/* 1 */
{
    "_id" : ObjectId("5bbcec1e2436f1d3e6275a6a"),
    "questionId" : "QID1",
    "answer" : "",
    "options" : [ 
        {
            "value" : "yes",
            "label" : "YES"
        }, 
        {
            "value" : "no",
            "label" : "NO"
        }
    ]
}

/* 2 */
{
    "_id" : ObjectId("5bbcec322436f1d3e6275a73"),
    "questionId" : "QID2",
    "answer" : "",
    "options" : [ 
        {
            "value" : "yes",
            "label" : "YES"
        }, 
        {
            "value" : "no",
            "label" : "NO"
        }
    ]
}

/* 3 */
{
    "_id" : ObjectId("5bbe12fd2436f1d3e62795e7"),
    "questionId" : "QID3",
    "answer" : "",
    "options" : [ 
        {
            "value" : "yes",
            "label" : "YES"
        }, 
        {
            "value" : "no",
            "label" : "NO"
        }
    ]
}

/* 4 */
{
    "_id" : ObjectId("5bbe130a2436f1d3e62795ef"),
    "questionId" : "QID4",
    "answer" : "",
    "options" : [ 
        {
            "value" : "yes",
            "label" : "YES"
        }, 
        {
            "value" : "no",
            "label" : "NO"
        }
    ]
}

/* 5 */
{
    "_id" : ObjectId("5bbe7fdc2436f1d3e627a108"),
    "questionId" : "QID5",
    "answer" : "",
    "options" : [ 
        {
            "value" : "yes",
            "label" : "YES"
        }, 
        {
            "value" : "no",
            "label" : "NO"
        }
    ]
}

/* 6 */
{
    "_id" : ObjectId("5bbe7fe92436f1d3e627a10e"),
    "questionId" : "QID6",
    "answer" : "",
    "options" : [ 
        {
            "value" : "yes",
            "label" : "YES"
        }, 
        {
            "value" : "no",
            "label" : "NO"
        }
    ]
}

我正在使用mongodb 4,目前正在使用以下查询,我能够加入2个集合,但是它的作用是将引用的文档放在文档的单独字段中,并且我希望它位于文档结构本身。

db.createView (
"surveyquestions",
"survey",
    [
        {
                $lookup: {
                from: "questions",
                localField: "evidences.sections.questions",
                foreignField: "questionId",
                as: "question_docs"
            }
        }
    ]
)

Veeram的答案有效,不同的是它通过证据生成了2个不同的文档,而不是下面的一个-

/* 1 */
{
    "_id" : {
        "_id" : {
            "_id" : ObjectId("5bbcebfc2436f1d3e6275a5c"),
            "evidences_name" : "Evidence Method 1"
        }
    },
    "name" : "Some survey name",
    "score" : 12,
    "evidences" : [ 
        {
            "sections" : [ 
                {
                    "name" : "section1",
                    "questions" : [ 
                        {
                            "_id" : ObjectId("5bbcec1e2436f1d3e6275a6a"),
                            "questionId" : "QID1",
                            "answer" : "",
                            "options" : [ 
                                {
                                    "value" : "yes",
                                    "label" : "YES"
                                }, 
                                {
                                    "value" : "no",
                                    "label" : "NO"
                                }
                            ]
                        }, 
                        {
                            "_id" : ObjectId("5bbcec322436f1d3e6275a73"),
                            "questionId" : "QID2",
                            "answer" : "",
                            "options" : [ 
                                {
                                    "value" : "yes",
                                    "label" : "YES"
                                }, 
                                {
                                    "value" : "no",
                                    "label" : "NO"
                                }
                            ]
                        }
                    ]
                }, 
                {
                    "name" : "section2",
                    "questions" : [ 
                        {
                            "_id" : ObjectId("5bbe12fd2436f1d3e62795e7"),
                            "questionId" : "QID3",
                            "answer" : "",
                            "options" : [ 
                                {
                                    "value" : "yes",
                                    "label" : "YES"
                                }, 
                                {
                                    "value" : "no",
                                    "label" : "NO"
                                }
                            ]
                        }, 
                        {
                            "_id" : ObjectId("5bbe130a2436f1d3e62795ef"),
                            "questionId" : "QID4",
                            "answer" : "",
                            "options" : [ 
                                {
                                    "value" : "yes",
                                    "label" : "YES"
                                }, 
                                {
                                    "value" : "no",
                                    "label" : "NO"
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ]
}

/* 2 */
{
    "_id" : {
        "_id" : {
            "_id" : ObjectId("5bbcebfc2436f1d3e6275a5c"),
            "evidences_name" : "Evidence method 2"
        }
    },
    "name" : "Some survey name",
    "score" : 12,
    "evidences" : [ 
        {
            "sections" : [ 
                {
                    "name" : "section1",
                    "questions" : [ 
                        {
                            "_id" : ObjectId("5bbe7fdc2436f1d3e627a108"),
                            "questionId" : "QID5",
                            "answer" : "",
                            "options" : [ 
                                {
                                    "value" : "yes",
                                    "label" : "YES"
                                }, 
                                {
                                    "value" : "no",
                                    "label" : "NO"
                                }
                            ]
                        }
                    ]
                }, 
                {
                    "name" : "section2",
                    "questions" : [ 
                        {
                            "_id" : ObjectId("5bbe7fe92436f1d3e627a10e"),
                            "questionId" : "QID6",
                            "answer" : "",
                            "options" : [ 
                                {
                                    "value" : "yes",
                                    "label" : "YES"
                                }, 
                                {
                                    "value" : "no",
                                    "label" : "NO"
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ]
}

1 个答案:

答案 0 :(得分:0)

您可以尝试以下汇总。

db.createView(
    "surveyquestions",
    "survey",
    [
      {"$unwind":"$evidences"},
      {"$unwind":"$evidences.sections"},
      {"$lookup":{
        "from":"questions",
        "localField":"evidences.sections.questions",
        "foreignField":"questionId",
        "as":"evidences.sections.questions"
      }},
      {"$group":{
        "_id":{"_id":"$_id","evidences_name":"$evidences.name"},
        "name":{"$first":"$name"},
        "score":{"$first":"$score"},
        "sections":{"$push":"$evidences.sections"}
       }},
       {"$group":{
         "_id":"$_id._id",
         "name":{"$first":"$name"},
         "score":{"$first":"$score"},
         "evidences":{"$push":{"name":"$_id.evidences_name","sections":"$sections"}}
       }}
     ]
)