【问题标题】:Do a join and merge the foreign document in the same structure在相同的结构中加入并合并外部文档
【发布时间】:2018-10-10 10:45:20
【问题描述】:

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

Survey Collection 有一个文档:

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

问题集有以下文档:

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

【问题讨论】:

    标签: mongodb mongodb-query aggregation-framework


    【解决方案1】:

    您可以尝试以下聚合。

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

    【讨论】:

    • survey 是集合的名称吗?将其更改为您的收藏名称可能Survey
    • 您的原始查询工作了吗?我对这些观点不是很熟悉。我所做的只是更新了您的聚合查询。
    • 所以视图查询,我在问题中提出的那个有效!你给出的答案也是如此,它完成了这项工作,当作为查询在 db.survey.aggregate([your aggregation query]) 中执行时,但由于某种原因,当我使用它来创建像 db.createView () 这样的视图时却没有
    • 好的,您可以更新帖子以显示您尝试运行的视图查询吗?我可以试试。
    • 我已经使用您提供的聚合查询更新了带有视图查询的帖子。
    猜你喜欢
    • 2018-02-23
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多