Mongodb嵌套树结构
如何获取树格式的直接子数据 考虑以下示例数据…如果搜索键为India,则结果应包含所有包含项目数据的子节点 项目集合Mongodb嵌套树结构,mongodb,mongodb-query,aggregation-framework,aggregate,Mongodb,Mongodb Query,Aggregation Framework,Aggregate,如何获取树格式的直接子数据 考虑以下示例数据…如果搜索键为India,则结果应包含所有包含项目数据的子节点 项目集合 [ { id : 1, name : "p1" location : Pune }, { id : 2, name : "p2" location : Mumbai }, { id : 3, name : "p3" location : Nashik }, { id : 4, name : "p4" location : Pune }, { id : 5, name
[
{ id : 1, name : "p1" location : Pune },
{ id : 2, name : "p2" location : Mumbai },
{ id : 3, name : "p3" location : Nashik },
{ id : 4, name : "p4" location : Pune },
{ id : 5, name : "p5" location : Maharashstra },
{ id : 6, name : "p6" location : Pune }
]
[
{id : 1 name : 'l1' , 'location' : Pune , parentLocation : Maharashstra},
{id : 2 name : 'l2' , 'location' : Nashik , parentLocation : Maharashstra},
{id : 3 name : 'l3' , 'location' : Mumbai , parentLocation : Maharashstra},
{id : 4 name : 'l4' , 'location' : Maharashstra , parentLocation : India},
{id : 5 name : 'l5' , 'location' : India , parentLocation : null}
]
位置集合
[
{ id : 1, name : "p1" location : Pune },
{ id : 2, name : "p2" location : Mumbai },
{ id : 3, name : "p3" location : Nashik },
{ id : 4, name : "p4" location : Pune },
{ id : 5, name : "p5" location : Maharashstra },
{ id : 6, name : "p6" location : Pune }
]
[
{id : 1 name : 'l1' , 'location' : Pune , parentLocation : Maharashstra},
{id : 2 name : 'l2' , 'location' : Nashik , parentLocation : Maharashstra},
{id : 3 name : 'l3' , 'location' : Mumbai , parentLocation : Maharashstra},
{id : 4 name : 'l4' , 'location' : Maharashstra , parentLocation : India},
{id : 5 name : 'l5' , 'location' : India , parentLocation : null}
]
预期结果应低于…使用mongo聚合
我想检索所有的项目计数与细节
预期产出
India - p1,p2,p3,p4,p5
Maharashstra - p5
Pune - p1,p4
Mumbai - p2
Nashik - p3
{
"location": "India",
"parentLocation": null,
"Child": [
{
"location": "Maharashstra",
"parentLocation": "India",
"ProjectCount": 6,
"ProjectDetails" : [
{ "id" : 5, "name" : "p5", "location" : "Maharashstra"
}
],
"Child": [
{
"location": "Pune",
"parentLocation": "Maharashstra",
"ProjectCount": 3,
"ProjectDetails": [
{
"id": 1,
"name": "p1",
"location": "Pune"
},
{
"id": 4,
"name": "p4",
"location": "Pune"
},
{
"id": 6,
"name": "p6",
"location": "Pune"
}
]
},
{
"location": "Nashik",
"parentLocation": "Maharashstra",
"ProjectCount": 1,
"ProjectDetails": [
{
"id": 3,
"name": "p3",
"location": "Nashik"
}
]
},
{
"location": "Mumbai",
"parentLocation": "Maharashstra",
"ProjectCount": 1,
"ProjectDetails": [
{
"id": 2,
"name": "p2",
"location": "Mumbai"
}
]
}
]
}
]
预期Json输出
India - p1,p2,p3,p4,p5
Maharashstra - p5
Pune - p1,p4
Mumbai - p2
Nashik - p3
{
"location": "India",
"parentLocation": null,
"Child": [
{
"location": "Maharashstra",
"parentLocation": "India",
"ProjectCount": 6,
"ProjectDetails" : [
{ "id" : 5, "name" : "p5", "location" : "Maharashstra"
}
],
"Child": [
{
"location": "Pune",
"parentLocation": "Maharashstra",
"ProjectCount": 3,
"ProjectDetails": [
{
"id": 1,
"name": "p1",
"location": "Pune"
},
{
"id": 4,
"name": "p4",
"location": "Pune"
},
{
"id": 6,
"name": "p6",
"location": "Pune"
}
]
},
{
"location": "Nashik",
"parentLocation": "Maharashstra",
"ProjectCount": 1,
"ProjectDetails": [
{
"id": 3,
"name": "p3",
"location": "Nashik"
}
]
},
{
"location": "Mumbai",
"parentLocation": "Maharashstra",
"ProjectCount": 1,
"ProjectDetails": [
{
"id": 2,
"name": "p2",
"location": "Mumbai"
}
]
}
]
}
]
}
请参考上面的树…以获取输出
谢谢您……您能将您的json输出和一些解释发布到question@AnthonyWinzlet..我已经上传了Json输出…你能检查一下OK吗?但是在上面的输出中引用是如何工作的?这两个集合中的公共键是什么?@AnthonyWinzlet,我想用上面的树检索直接子级的所有项目计数structure@AnthonyWinzlet,公共字段是两个集合中的位置