我的文档具有以下结构:
[{
"_id": "1",
"prop1": "..."
}, {
"_id": "2",
"prop1": "...",
"derivationOf": "1"
}, {
"_id": "3",
"prop1": "...",
"derivationOf": "1"
}, {
"_id": "4",
"prop1": "...",
"derivationOf": "3"
}, {
"_id": "5",
"prop1": "...",
"derivationOf": "4"
}]
我想执行一个检索查询,该查询将生成具有相同数据的树状版本,如下所示:
{
"_id": "1",
"prop1": "...",
"children": [{
"_id": "2",
"prop1": "...",
"children": []
}, {
"_id": "3",
"prop1": "...",
"children": [{
"_id": "4",
"prop1": "...",
"children": [{
"_id": "5",
"prop1": "...",
"children": []
}]
}]
}]
}
我看过$graphLookup
和this question,但没有取得太大的成功。
有人知道如何通过mongodb查询实现这一目标吗?