我试图使用MongoDb列出展开嵌套数组。
产品包含任务,每个任务可以包含零,一个或多个子任务。
以下是产品文档示例:
db.products.find({_id: ObjectId("554a13d4b692088a38f01f3b")})
结果:
{
"_id" : ObjectId("554a13d4b692088a38f01f3b"),
"title" : "product title",
"order" : 3,
"description" : "Description here ",
"status" : "live",
"tasks" : [
{
"title" : "task 1",
"description" : "task 1 desc",
"order" : 10,
"_id" : ObjectId("554a13d4b692088a38f01f3a"),
"status" : "live",
"subTasks" : [
{
"title" : "task 1 sub 1",
"content" : "aaa",
"order" : -2,
"_id" : ObjectId("554a13d4b692088a38f01f5a"),
"status" : "live"
},
{
"title" : "task 1 sub 2",
"content" : "aaa",
"order" : 1,
"_id" : ObjectId("554a13d4b692088a38f01f3a"),
"status" : "live"
},
{
"title" : "task 1 sub 4",
"content" : "aaa",
"order" : 8,
"_id" : ObjectId("554a13d4b692088a38f01f4a"),
"status" : "live"
},
{
"title" : "task 1 sub 3 ",
"content" : "aaa",
"order" : 2,
"_id" : ObjectId("5550d0a61662211332d9a973"),
"status" : "live"
}
]
},
{
"title" : "task 2",
"description" : "task desc 2",
"order" : 1,
"_id" : ObjectId("5550855f9ee2db4e3958d299"),
"status" : "live",
"subTasks" : [
{
"title" : "task 2 sub 1",
"content" : "bbb",
"order" : 1,
"_id" : ObjectId("55508f459ee2db4e3958d29a"),
"status" : "live"
}
]
},
{
"title" : "task 3",
"description" : "task 3 desc",
"order" : 2,
"_id" : ObjectId("5551b844bb343a620f85f323"),
"status" : "live",
"subTasks" : [
{
"title" : "task 3 sub 2",
"content" : "cccc",
"order" : 0,
"_id" : ObjectId("5551b88abb343a620f85f324"),
"status" : "live"
},
{
"title" : "task 3 sub 4",
"content" : "cccc",
"order" : 1,
"_id" : ObjectId("5551b8f1bb343a620f85f325"),
"status" : "hidden"
},
{
"title" : "task 3 sub 3",
"content" : "ccc",
"order" : 2,
"_id" : ObjectId("5551ba40bb343a620f85f327"),
"status" : "hidden"
},
{
"title" : "task 3 sub 1",
"content" : "cccc",
"order" : -1,
"_id" : ObjectId("5551bcb8c31283c051d30b7c"),
"status" : "hidden"
}
]
}
]}
聚合管道非常适合对任务进行排序。 tasks.subTask(使用task.order& task.subTasks.order)感谢上一个问题n9code上的here。
有时任务有没有子任务,例如:
{
"_id" : ObjectId("554a13d4b692088a38f01f3b"),
"title" : "product title",
"order" : 3,
"description" : "Description here ",
"status" : "live",
"tasks" : [
{
"title" : "task 1",
"description" : "task 1 desc",
"order" : 10,
"_id" : ObjectId("554a13d4b692088a38f01f3a"),
"status" : "live",
"subTasks" : [
{
"title" : "task 1 sub 1",
"content" : "aaa",
"order" : -2,
"_id" : ObjectId("554a13d4b692088a38f01f5a"),
"status" : "live"
},
{
"title" : "task 1 sub 2",
"content" : "aaa",
"order" : 1,
"_id" : ObjectId("554a13d4b692088a38f01f3a"),
"status" : "live"
},
{
"title" : "task 1 sub 4",
"content" : "aaa",
"order" : 8,
"_id" : ObjectId("554a13d4b692088a38f01f4a"),
"status" : "live"
},
{
"title" : "task 1 sub 3 ",
"content" : "aaa",
"order" : 2,
"_id" : ObjectId("5550d0a61662211332d9a973"),
"status" : "live"
}
]
},
{
"title" : "task 2",
"description" : "task desc 2",
"order" : 1,
"_id" : ObjectId("5550855f9ee2db4e3958d299"),
"status" : "live",
"subTasks" : [
]
},
{
"title" : "task 3",
"description" : "task 3 desc",
"order" : 2,
"_id" : ObjectId("5551b844bb343a620f85f323"),
"status" : "live",
"subTasks" : [
{
"title" : "task 3 sub 2",
"content" : "cccc",
"order" : 0,
"_id" : ObjectId("5551b88abb343a620f85f324"),
"status" : "live"
},
{
"title" : "task 3 sub 4",
"content" : "cccc",
"order" : 1,
"_id" : ObjectId("5551b8f1bb343a620f85f325"),
"status" : "hidden"
},
{
"title" : "task 3 sub 3",
"content" : "ccc",
"order" : 2,
"_id" : ObjectId("5551ba40bb343a620f85f327"),
"status" : "hidden"
},
{
"title" : "task 3 sub 1",
"content" : "cccc",
"order" : -1,
"_id" : ObjectId("5551bcb8c31283c051d30b7c"),
"status" : "hidden"
}
]
}
]
}
任何有关任务的帮助仍然可以显示他们没有孩子吗?
当前聚合:
[
{$match: {_id: ObjectId("554a13d4b692088a38f01f3b")}},
{$project: {tasks: 1, doc: {title: "$title", order: "$order", description: "$description", status: "$status"}}},
{$unwind: "$tasks"},
{$unwind: "$tasks.subTasks"},
{$sort: {"tasks.subTasks.order": 1, "tasks.order": 1}},
{$project: {doc: 1, task_id: "$tasks._id", tasks_doc: {title: "$tasks.title", description: "$tasks.description", order: "$tasks.order", status: "$tasks.status"}, subTasks: "$tasks.subTasks"}},
{$group: {_id: {_id: "$_id", task_id: "$task_id", doc: "$doc", task_doc: "$tasks_doc"}, subTasks: {$push: "$subTasks"}}},
{$group: {_id: {_id: "$_id._id", doc: "$_id.doc"}, tasks: {$push: {_id: "$_id.task_id", title: "$_id.task_doc.title", description: "$_id.task_doc.description", order: "$_id.task_doc.order", status: "$_id.task_doc.status", subTasks: "$subTasks"}}}},
{$project: {_id: "$_id._id", title: "$_id.doc.title", description: "$_id.doc.description", order: "$_id.doc.order", status: "$_id.doc.status", tasks: 1}}
]
期望的结果:
{
"_id": ObjectId("554a13d4b692088a38f01f3b"),
"title": "product title",
"order": 3,
"description": "Description here ",
"status": "live",
"tasks": [
{
"title": "task 2",
"description": "task desc 2",
"order": 1,
"_id": ObjectId("5550855f9ee2db4e3958d299"),
"status": "live",
"subTasks": [
]
},
{
"title": "task 3",
"description": "task 3 desc",
"order": 2,
"_id": ObjectId("5551b844bb343a620f85f323"),
"status": "live",
"subTasks": [
{
"title": "task 3 sub 1",
"content": "cccc",
"order": -1,
"_id": ObjectId("5551bcb8c31283c051d30b7c"),
"status": "hidden"
},
{
"title": "task 3 sub 2",
"content": "cccc",
"order": 0,
"_id": ObjectId("5551b88abb343a620f85f324"),
"status": "live"
},
{
"title": "task 3 sub 3",
"content": "ccc",
"order": 2,
"_id": ObjectId("5551ba40bb343a620f85f327"),
"status": "hidden"
}{
"title": "task 3 sub 4",
"content": "cccc",
"order": 1,
"_id": ObjectId("5551b8f1bb343a620f85f325"),
"status": "hidden"
}
]
}{
"title": "task 1",
"description": "task 1 desc",
"order": 10,
"_id": ObjectId("554a13d4b692088a38f01f3a"),
"status": "live",
"subTasks": [
{
"title": "task 1 sub 1",
"content": "aaa",
"order": -2,
"_id": ObjectId("554a13d4b692088a38f01f5a"),
"status": "live"
},
{
"title": "task 1 sub 2",
"content": "aaa",
"order": 1,
"_id": ObjectId("554a13d4b692088a38f01f3a"),
"status": "live"
},
{
"title": "task 1 sub 3 ",
"content": "aaa",
"order": 2,
"_id": ObjectId("5550d0a61662211332d9a973"),
"status": "live"
},
{
"title": "task 1 sub 4",
"content": "aaa",
"order": 8,
"_id": ObjectId("554a13d4b692088a38f01f4a"),
"status": "live"
}
]
}
]
答案 0 :(得分:2)
MongoDB在$unwind
步骤中忽略空数组或空数组,所以首先需要用一些值替换空数组或空数组字段,比如"<e>"
。
要执行此操作,只需在$unwind
subTasks
之前添加此管道阶段:
$project: {
doc: 1,
tasks: {
title: 1,
description: 1,
order: 1,
status: 1,
subTasks: {
$cond: [
{
$size: "$tasks.subTasks"
},
"$tasks.subTasks",
['<e>']
]
}
}
}
然后,在$group
期间,您将需要执行相反的操作,用空数组替换"<e>"
,值:
$group: {
_id: {
_id: "$_id._id",
doc: "$_id.doc"},
tasks: {
$push: {
_id: "$_id.task_id",
title: "$_id.task_doc.title",
description: "$_id.task_doc.description",
order: "$_id.task_doc.order",
status: "$_id.task_doc.status",
subTasks: {
$cond: [
{
$eq: ["$subTasks", ["<e>"]]
},
[],
"$subTasks"
]
}
}
}
}
}
所以这是你的最终管道:
[
{$match: {_id: ObjectId("554a13d4b692088a38f01f3b")}},
{$project: {tasks: 1, doc: {title: "$title", order: "$order", description: "$description", status: "$status"}}},
{$unwind: "$tasks"},
{$project: {doc: 1, tasks: {title: 1, description: 1, order: 1, status: 1, subTasks: {$cond: [{$size: "$tasks.subTasks"}, "$tasks.subTasks", ['<e>']]}}}},
{$unwind: "$tasks.subTasks"},
{$sort: {"tasks.order": -1, "tasks.subTasks.order": 1}},
{$project: {doc: 1, task_id: "$tasks._id", tasks_doc: {title: "$tasks.title", description: "$tasks.description", order: "$tasks.order", status: "$tasks.status"}, subTasks: "$tasks.subTasks"}},
{$group: {_id: {_id: "$_id", task_id: "$task_id", doc: "$doc", task_doc: "$tasks_doc"}, subTasks: {$push: "$subTasks"}}},
{$group: {_id: {_id: "$_id._id", doc: "$_id.doc"}, tasks: {$push: {_id: "$_id.task_id", title: "$_id.task_doc.title", description: "$_id.task_doc.description", order: "$_id.task_doc.order", status: "$_id.task_doc.status", subTasks: {$cond: [{$eq: ["$subTasks", ['<e>']]}, [], "$subTasks"]}}}}},
{$project: {_id: "$_id._id", title: "$_id.doc.title", description: "$_id.doc.description", order: "$_id.doc.order", status: "$_id.doc.status", tasks: 1}}
]
干杯:)