下面在我的查询中给出以生成组织层次结构,
db.org.aggregate([
{
$group:{
_id: {
parent: "$parent",
level: "$level"
},
level_data: {$push:{name: "$name", id: "$_id"}}
}
},
{
$group:{
_id: "$_id.level",
data: {$push: {parent: "$_id.parent", nodes: "$level_data"}}
}
},
{
$sort:{
_id: 1
}
},
{
$project:{
//_id: 0,
level: "$_id",
data: 1
}
}
]);
我的答复:
/* 1 */
{
"_id" : 1,
"data" : [
{
"parent" : "null",
"nodes" : [
{
"name" : "MBC",
"id" : ObjectId("5c7523efa2f2163114f9feb6")
}
]
}
],
"level" : 1
},
/* 2 */
{
"_id" : 2,
"data" : [
{
"parent" : "MBC",
"nodes" : [
{
"name" : "National",
"id" : ObjectId("5c7523efa2f2163114f9feb7")
}
]
}
],
"level" : 2
},
/* 3 */
{
"_id" : 3,
"data" : [
{
"parent" : "National",
"nodes" : [
{
"name" : "QUEBEC/MARITIMES",
"id" : ObjectId("5c7523efa2f2163114f9febd")
},
{
"name" : "PRAIRIES",
"id" : ObjectId("5c7523efa2f2163114f9febc")
},
{
"name" : "ONTARIO",
"id" : ObjectId("5c7523efa2f2163114f9febb")
},
{
"name" : "GTA",
"id" : ObjectId("5c7523efa2f2163114f9feba")
},
{
"name" : "GMA",
"id" : ObjectId("5c7523efa2f2163114f9feb9")
},
{
"name" : "BRITISH COLUMBIA",
"id" : ObjectId("5c7523efa2f2163114f9feb8")
}
]
}
],
"level" : 3
}
在上一个项目阶段取消注释_id:0时的响应
/* 1 */
{
"data" : [
{
"parent" : "null",
"nodes" : [
{
"name" : "MBC",
"id" : {
"data" : "\u0016\u0000\u0000\u0000\u0007obj\u0000\\u#ï¢ò\u00161\u0014ùþ¶\u0000"
}
}
]
}
],
"level" : {
"data" : "\u0012\u0000\u0000\u0000\u0001obj\u0000\u0000\u0000\u0000\u0000\u0000\u0000ð?\u0000"
}
},
/* 2 */
{
"data" : [
{
"parent" : "MBC",
"nodes" : [
{
"name" : "National",
"id" : {
"data" : "\u0016\u0000\u0000\u0000\u0007obj\u0000\\u#ï¢ò\u00161\u0014ùþ·\u0000"
}
}
]
}
],
"level" : {
"data" : "\u0012\u0000\u0000\u0000\u0001obj\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000@\u0000"
}
},
/* 3 */
{
"data" : [
{
"parent" : "National",
"nodes" : [
{
"name" : "QUEBEC/MARITIMES",
"id" : {
"data" : "\u0016\u0000\u0000\u0000\u0007obj\u0000\\u#ï¢ò\u00161\u0014ùþ½\u0000"
}
},
{
"name" : "PRAIRIES",
"id" : {
"data" : "\u0016\u0000\u0000\u0000\u0007obj\u0000\\u#ï¢ò\u00161\u0014ùþ¼\u0000"
}
},
{
"name" : "ONTARIO",
"id" : {
"data" : "\u0016\u0000\u0000\u0000\u0007obj\u0000\\u#ï¢ò\u00161\u0014ùþ»\u0000"
}
},
{
"name" : "GTA",
"id" : {
"data" : "\u0016\u0000\u0000\u0000\u0007obj\u0000\\u#ï¢ò\u00161\u0014ùþº\u0000"
}
},
{
"name" : "GMA",
"id" : {
"data" : "\u0016\u0000\u0000\u0000\u0007obj\u0000\\u#ï¢ò\u00161\u0014ùþ¹\u0000"
}
},
{
"name" : "BRITISH COLUMBIA",
"id" : {
"data" : "\u0016\u0000\u0000\u0000\u0007obj\u0000\\u#ï¢ò\u00161\u0014ùþ¸\u0000"
}
}
]
}
],
"level" : {
"data" : "\u0012\u0000\u0000\u0000\u0001obj\u0000\u0000\u0000\u0000\u0000\u0000\u0000\b@\u0000"
}
}
下面是我的文档结构示例:
/* 1 createdAt:2/26/2019, 4:33:03 PM*/
{
"_id" : ObjectId("5c7523efa2f2163114f9fefe"),
"name" : "813",
"parent" : "QUEBEC/MARITIMES",
"level" : 4
},
/* 2 createdAt:2/26/2019, 4:33:03 PM*/
{
"_id" : ObjectId("5c7523efa2f2163114f9fefd"),
"name" : "551",
"parent" : "QUEBEC/MARITIMES",
"level" : 4
},
/* 3 createdAt:2/26/2019, 4:33:03 PM*/
{
"_id" : ObjectId("5c7523efa2f2163114f9fefc"),
"name" : "550",
"parent" : "QUEBEC/MARITIMES",
"level" : 4
},
{
"_id" : ObjectId("5c7523efa2f2163114f9feba"),
"name" : "GTA",
"parent" : "National",
"level" : 3
},
/* 70 createdAt:2/26/2019, 4:33:03 PM*/
{
"_id" : ObjectId("5c7523efa2f2163114f9feb9"),
"name" : "GMA",
"parent" : "National",
"level" : 3
},
/* 71 createdAt:2/26/2019, 4:33:03 PM*/
{
"_id" : ObjectId("5c7523efa2f2163114f9feb8"),
"name" : "BRITISH COLUMBIA",
"parent" : "National",
"level" : 3
},
{
"_id" : ObjectId("5c7523efa2f2163114f9feb7"),
"name" : "National",
"parent" : "MBC",
"level" : 2
},
/* 73 createdAt:2/26/2019, 4:33:03 PM*/
{
"_id" : ObjectId("5c7523efa2f2163114f9feb6"),
"name" : "MBC",
"parent" : "null",
"level" : 1
}
在最后一个项目阶段可以看到_id被注释掉了,这是因为当我关闭其投影时,它会破坏我的输出。否则,一切正常。
响应和文档仅供参考,不完全。