我有两个文档appointment
和assistant
。
{
"_id" : ObjectId("5a4e5a448b70d50e34d204a4"),
"name" : "Dreikoenigstunier",
"date" : ISODate("2013-01-06T00:00:00.000Z"),
"comment" : null,
"tasks" : [
{
"name" : "Speisen und Getränke",
"sections" : [
{
"start" : 37800,
"end" : 55800,
"entirely" : true,
"assistants" : [
{
"assistant" : ObjectId("5a4e5a438b70d50e34d203e3"),
"state" : 3
},
{
"assistant" : ObjectId("5a4e5a438b70d50e34d203e4"),
"state" : 3
},
{
"assistant" : ObjectId("5a4e5a438b70d50e34d203e5"),
"state" : 3
}
]
},
{
"start" : 55800,
"end" : 68400,
"entirely" : true,
"assistants" : [
{
"assistant" : {
"_id" : ObjectId("5a4e5a438b70d50e34d203d5")
},
"state" : 3
},
{
"assistant" : {
"_id" : ObjectId("5a4e5a438b70d50e34d20408")
},
"state" : 3
},
{
"assistant" : {
"_id" : ObjectId("5a4e5a438b70d50e34d2040a")
},
"state" : 3
}
]
}
]
},
{
"name" : "Schiri",
"sections" : [
{
"start" : 55800,
"end" : 68400,
"entirely" : true,
"assistants" : [
{
"assistant" : {
"_id" : ObjectId("5a4e5a438b70d50e34d203d8")
},
"state" : 3
},
{
"assistant" : {
"_id" : ObjectId("5a4e5a438b70d50e34d203e6")
},
"state" : 3
}
]
}
]
}
]
}
{
"_id" : ObjectId("5a4e5a438b70d50e34d203ca"),
"firstname" : "Max",
"lastname" : "Mustermann",
"category" : "Herren 2",
"email" : "test@test.de",
"phone" : "0622-...",
"mobile" : "0151-...",
"gender" : "male",
"enabled" : "1",
"hash" : "$2a$10...",
"token" : null
}
现在,我需要查询所有约会并查找嵌套的助手属性。 因此,我有以下管道
[{
$match: {
"_id": ObjectId("5a4e5a448b70d50e34d204a4")
}
}, {
$unwind: { path: '$tasks', preserveNullAndEmptyArrays: true }
}, {
$unwind: { path: '$tasks.sections', preserveNullAndEmptyArrays: true }
}, {
$unwind: { path: '$tasks.sections.assistants', preserveNullAndEmptyArrays: true }
}, {
$lookup: {
from: 'assistant',
localField: "tasks.sections.assistants.assistant",
foreignField: "_id",
as: "tasks.sections.assistants.assistant"
}
}, {
$unwind: { path: '$tasks.sections.assistants.assistant', preserveNullAndEmptyArrays: true }
}]
但是如何恢复最初的Appointment
结构?我知道我必须使用$group
,但是如何使用?没有可以分组的标识符...