如https://www.slideshare.net/mongodb/webinar-working-with-graph-data-in-mongodb中所述,幻灯片 50可以$graphLookup
在视图上使用以获得 2 级深度树结构嵌套格式。
我有一个 MongoDB 集合,其中包含树节点作为文档,格式如下:
{ "_id" : { "$oid" : "5b1a952361c6fa3418a15660" },
"nodeId" : 23978995,
"name" : "settings",
"type" : "Node",
"parentId" : [ 23978893, 23979072, 23979081 ] }
我创建了一个视图,如:
db.createView("treeView", "node", [
{
$graphLookup: {
from: "node",
startWith: "$nodeId",
connectFromField: "nodeId",
connectToField: "parentId",
maxDepth: 0,
as: "children"
}
}
]);
我执行图形查找,如:
db.node.aggregate([
{ $match: {"nodeId": 23978786 } },
{
$graphLookup: {
from: "treeView",
startWith: "$nodeId",
connectFromField: "nodeId",
connectToField: "parentId",
maxDepth: 0,
as: "children"
}
}
]);
我的问题是我怎样才能得到整个层次结构,所有层次都很深?