如何使用MongoDB递归查询树结构?

mmu*_*478 3 recursion mongodb node.js

例如一个树结构;

[
    {id: 1 , childrenIdList: [2, 3]},
    {id: 2 , childrenIdList: [4, 5]},
    {id: 3 , childrenIdList: []},
    {id: 4 , childrenIdList: [6, 7]},
    {id: 5 , childrenIdList: []},
    {id: 6 , childrenIdList: []},
    {id: 7 , childrenIdList: []}
]
Run Code Online (Sandbox Code Playgroud)

就像

               1
          2        3
       4    5
    6    7
Run Code Online (Sandbox Code Playgroud)

如何从启动叶节点(id = 7)到根(id = 1)跟踪树?

寻找的父母id=7很容易;因为

db.document.find({childrenList: { $in: [7]}}, {id: 1}).toArray(function(err), result{
  /*result gives 
  {"id" : NumberInt(4)}
  now I should look the parent of id=4, and parent of id=2 as you know.
  */
})
Run Code Online (Sandbox Code Playgroud)

在mongodb上可以进行递归查询吗?我该如何实施?

Wan*_*iar 7

根据您的用例,MongoDB v3.4提供了一个聚合管道运算符,称为$ graphLookup。聚合运算符能够对集合执行递归搜索。在$ graphLookup定义上查看更多定义

使用上面的文档层次结构和值作为示例,您可以尝试在聚合以下运行:

db.collectionName.aggregate([

                {$unwind:{
                        path:"$childrenIdList", 
                        preserveNullAndEmptyArrays: true}
                  }, 
                {$graphLookup:{
                        from:"collectionName", 
                        startWith:"$_id", 
                        connectFromField:"_id", 
                        connectToField:"childrenIdList", 
                        as:"myparents",  
                        restrictSearchWithMatch: {"_id"}}
                  }, 
                {$match: {"_id": 7 } },
                {$group:{
                        _id:"$_id", 
                        parents:{$addToSet:"$myparents._id"}
                  }}
]);
Run Code Online (Sandbox Code Playgroud)

上面应该返回结果如下:

{ "_id" : 7, "parents" : [ [ 1, 2, 4 ] ] }
Run Code Online (Sandbox Code Playgroud)

话虽如此,如果您有大量的收藏夹,上面的查询可能无法执行,因为您将在每个文档上执行$ unwind并且将无法使用索引。正如其他人所建议的,您应该重新考虑您的文档模型结构。请参见数据模型树结构。根据您的应用程序逻辑和查询用例进行优化,并遵循灵活的文档架构。