У меня есть две коллекции, которые я использую для создания формы опроса:
Сборник опросов содержит документ:
{
"_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"
}
]
}
]
}
]
}
]
}