【发布时间】:2018-02-20 08:06:52
【问题描述】:
我有多个集合,我使用了单独的集合和外键的方法,我想加入这个集合来构建一个嵌套的集合。 这是我的集合模式:
const SurveySchema = new Schema({
_id:{ type: Schema.ObjectId, auto: true },
name: String,
enabled: {type: Boolean, Default: true},
created_date:{type: Date, Default: Date.now},
company: {type: Schema.Types.ObjectId, ref: 'Company'},});
const GroupSchema = new Schema({
_id:{ type: Schema.ObjectId, auto: true },
name: String,
order: String,
created_date:{type: Date, Default: Date.now},
questions: [{type: Schema.Types.ObjectId, ref: 'Question'}],
survey: {type: Schema.Types.ObjectId, ref: 'Survey'}
});
const ResponseSchema = new Schema({
_id:{ type: Schema.ObjectId, auto: true },
response_text: String,
order: String,
created_date:{type: Date, Default: Date.now},
question:{type: Schema.Types.ObjectId, ref: 'Question'}
});
这是我构建这个嵌套对象的代码:
Survey.aggregate([
{ $match: {} },
{ $lookup: {
from: 'groups',
localField: '_id',
foreignField: 'survey',
as: 'groupsofquestions',
}},
{ $unwind: {
path: "$groupsofquestions",
preserveNullAndEmptyArrays: true
}},
{ $lookup: {
from: 'questions',
localField: 'groupsofquestions._id',
foreignField: 'group',
as: 'questionsofgroup',
}},
{ $lookup: {
from: 'response',
localField: 'questionsofgroup._id',
foreignField: 'question',
as: 'responses',
}},
{ $group: {
_id: "$_id",
name: {$first: "$name"},
groups: {$push: {
id: "$groupsofquestions._id",
name: "$groupsofquestions.name",
questions: "$questionsofgroup",
reponses: "$responses"
}}
}}
])
我想如下结构,(也有外部链接):
http://jsoneditoronline.org/?id=d7d1779b3b95e3acb28f8a2be0785423
[
{
"__v": 0,
"_id": "59b6715725dcd2060da7f591",
"company": "59b6715725dcd2060da7f58f",
"created_date": "2017-09-11T11:19:51.709Z",
"enabled": true,
"name": "function String() { [native code] }",
"groups": [
{
"_id": "59b6715725dcd2060da7f592",
"name": "groupe 1 des question",
"order": "1",
"created_date": "2017-09-11T11:19:51.709Z",
"survey": "59b6715725dcd2060da7f591",
"__v": 0,
"questions": [
{
"_id": "59b6715725dcd2060da7f594",
"question_text": "question 1 group 1",
"order": "1",
"created_date": "2017-09-11T11:19:51.709Z",
"group": "59b6715725dcd2060da7f592",
"__v": 0,
"responses": [
{
"_id": "59b6715725dcd2060da7f598",
"response_text": "reponse 1 question 1 group 1",
"order": "1",
"created_date": "2017-09-11T11:19:51.710Z",
"question": "59b6715725dcd2060da7f594",
"__v": 0
},
{
"_id": "59b6715725dcd2060da7f599",
"response_text": "reponse 2 question 1 group 1",
"order": "2",
"created_date": "2017-09-11T11:19:51.710Z",
"question": "59b6715725dcd2060da7f594",
"__v": 0
}
]
},
{
"_id": "59b6715725dcd2060da7f595",
"question_text": "question 2 group 1",
"order": "2",
"created_date": "2017-09-11T11:19:51.710Z",
"group": "59b6715725dcd2060da7f592",
"__v": 0,
"responses": [
{
"_id": "59b6715725dcd2060da7f59a",
"response_text": "reponse 1 question 2 group 1",
"order": "1",
"created_date": "2017-09-11T11:19:51.710Z",
"question": "59b6715725dcd2060da7f595",
"__v": 0
},
{
"_id": "59b6715725dcd2060da7f59b",
"response_text": "reponse 2 question 2 group 1",
"order": "2",
"created_date": "2017-09-11T11:19:51.710Z",
"question": "59b6715725dcd2060da7f595",
"__v": 0
}
]
}
]
},
{
"_id": "59b6715725dcd2060da7f593",
"name": "groupe 2 des question",
"order": "2",
"created_date": "2017-09-11T11:19:51.709Z",
"survey": "59b6715725dcd2060da7f591",
"__v": 0,
"questions": [
{
"_id": "59b6715725dcd2060da7f596",
"question_text": "question 1 group 1",
"order": "1",
"created_date": "2017-09-11T11:19:51.710Z",
"group": "59b6715725dcd2060da7f592",
"__v": 0,
"responses": [
{
"_id": "59b6715725dcd2060da7f59c",
"response_text": "reponse 1 question 1 group 2",
"order": "1",
"created_date": "2017-09-11T11:19:51.710Z",
"question": "59b6715725dcd2060da7f596",
"__v": 0
},
{
"_id": "59b6715725dcd2060da7f59d",
"response_text": "reponse 2 question 1 group 2",
"order": "2",
"created_date": "2017-09-11T11:19:51.710Z",
"question": "59b6715725dcd2060da7f596",
"__v": 0
}
]
},
{
"_id": "59b6715725dcd2060da7f597",
"question_text": "question 2 group 1",
"order": "2",
"created_date": "2017-09-11T11:19:51.710Z",
"group": "59b6715725dcd2060da7f592",
"__v": 0,
"responses": [
{
"_id": "59b6715725dcd2060da7f59e",
"response_text": "reponse 1 question 2 group 2",
"order": "1",
"created_date": "2017-09-11T11:19:51.710Z",
"question": "59b6715725dcd2060da7f597",
"__v": 0
},
{
"_id": "59b6715725dcd2060da7f59f",
"response_text": "reponse 2 question 2 group 2",
"order": "2",
"created_date": "2017-09-11T11:19:51.710Z",
"question": "59b6715725dcd2060da7f597",
"__v": 0
}
]
}
]
}
]
}
]
有人可以帮我按照示例中所示的方式构建响应吗?
【问题讨论】:
-
您使用
preserveNullAndEmptyArrays有什么原因,还是您只是从某个地方复制了示例?在$lookup与外部集合中的任何结果不匹配的情况下,它确实会有所不同。然而,如果在关系总是期望匹配某些东西时不需要这样做,这是一个合理的性能改进。您还错过了Question架构定义,但我们可以合理地假设它与示例输出匹配,因为其他架构也匹配。 -
我不确定你在问什么,因为你似乎正在做你需要做的一切。
标签: javascript node.js mongodb mongoose aggregation-framework