我需要从数据库中检索整个单个对象层次结构作为JSON。 我尝试聚合几个小时,无法解决如何使用我的数据。所以我有三个系列:
形式
{ "_id" : "1", "name" : "My first form" }
{ "_id" : "2", "name" : "Second one" }
{ "_id" : "3", "name" : "Another" }
问题
{ "_id" : "q1", "form" : "1", "title": "What's your country?"}
{ "_id" : "q2", "form" : "1", "title": "What your favorite color?"}
{ "_id" : "q3", "form" : "1", "title": "Where do you live?"}
{ "_id" : "q4", "form" : "2", "title": "Where to go?"}
选项
{ "_id" : "o1", "question" : "q1", "text" : "Brazil" }
{ "_id" : "o2", "question" : "q1", "text" : "EUA" }
{ "_id" : "o3", "question" : "q1", "text" : "China" }
{ "_id" : "o4", "question" : "q2", "text" : "Red" }
{ "_id" : "o5", "question" : "q2", "text" : "Blue" }
{ "_id" : "o6", "question" : "q2", "text" : "Green" }
我需要使用所有相应的问题检索每个表单,并在每个问题中检索它的选项。像这样:
[
{
_id:"q1",
name: "My first form",
questions: [
{ "_id" : "q1",
"form" : "1",
"title": "What's your country?",
"options": [
{ "_id" : "o1", "question" : "q1", "text" : "Brazil" }
{ "_id" : "o2", "question" : "q1", "text" : "EUA" },
{ "_id" : "o3", "question" : "q1", "text" : "China" }
]
},
{ "_id" : "q2",
"form" : "1",
"title": "What your favorite color",
"options": [
{ "_id" : "o4", "question" : "q2", "text" : "Red" }
{ "_id" : "o5", "question" : "q2", "text" : "Blue" },
{ "_id" : "o6", "question" : "q2", "text" : "Green" }
]
},
{ "_id" : "q3",
"form" : "1",
"title": "Where do you live?",
"options": []
}
]
},
...
]
我尝试了很多$ lookup,$ unwind,另一个$ lookup和$ project,但没有给我带来结果(里面有问题的表单,里面有选项的问题)。
请帮帮我! :)
答案 0 :(得分:2)
我认为周围的人正在查询question
集合,查找他们的question
并按form
分组,最后按顺序查找表单和项目。
这应该这样做。请注意,此聚合的输出中的_id
是_id
形式。
db.question.aggregate([
{$match: {}},
{$lookup: {
from: 'option',
localField: '_id',
foreignField: 'question',
as: 'options'
}},
{$group: {
_id: "$form",
questions: {$push: {
title: "$title",
options: "$options",
form: "$form"
}}
}},
{$lookup: {
from: 'form',
localField: "_id",
foreignField: "_id",
as: 'form'
}},
{$project: {
name: {$arrayElemAt: ["$form.name", 0]},
questions: true
}}
]);
实际上......这似乎是一个更好的选择。它也将返回form
s而没有question
。
db.form.aggregate([
{$match: {}},
{$lookup: {
from: 'question',
localField: '_id',
foreignField: 'form',
as: 'questions'
}},
{$unwind: {
path: "$questions",
preserveNullAndEmptyArrays: true
}},
{$lookup: {
from: 'option',
localField: 'questions._id',
foreignField: 'question',
as: 'options'
}},
{$group: {
_id: "$_id",
name: {$first: "$name"},
question: {$push: {
title: "$questions.title",
form: "$questions.form",
options: "$options"
}}
}}
])