0
用例如下。使用MongoDB的新聚合框架進行更深入的遞歸搜索
設備已通過DEVICE_ID
每個組件鏈接零個或多個組件已經通過PARENT_ID
預期結果掛鉤它自己的組件:
[
{
id: 1
name: "device_a",
components: [
{
name: "component_a",
id: 2
device_id: 1
components: [
{
name: "component_b"
parent_id: 2
id: 3
}
]
}
]
}
]
使用新的聚合框架,我可以遞歸地獲得設備組件的深度1結果。 (備註:PARENT_ID爲空,指示組件不具有父組件)
db.device.aggregate(
[
{ "$graphLookup": {
"from": "component",
"startWith": "$_id",
"connectFromField": "_id",
"connectToField": "device_id",
"as": "components",
"restrictSearchWithMatch": { "parent_id": null }
}},
{ "$addFields": {
"components": {
"$reverseArray": "$components"
}
}}
]
)
此查詢原來的輸出。
[
{
id: 1
name: "device_a",
components: [
{
name: "component_a",
id: 2
device_id: 1
}
]
}
]
基本上,我也可以聚合組件本身,並獲得其子組件的邏輯相同的列表。
db.component.aggregate(
[
{ "$match": { "parent_id": null } },
{ "$graphLookup": {
"from": "component",
"startWith": "$_id",
"connectFromField": "_id",
"connectToField": "parent_id",
"as": "components"
}},
{ "$addFields": {
"components": {
"$reverseArray": "$components"
}
}}
]
)
/* turns out */
[
{
name: "component_a",
id: 2
device_id: 1
components: [
{
name: "component_b",
parent_id: 2
id: 3
}
]
}
]
有沒有什麼好的方法將它們結合在一起?對於更高級的使用情況下,怎麼能去更多的更深,像component_a - > component_b - > component_c ...
你的問題很難理解。你在做什麼?預期的結果是什麼? – styvane