Mongodb:仅获取树的叶子 [英] Mongodb: get only leaves of tree

查看:134
本文介绍了Mongodb:仅获取树的叶子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在mongo数据库中有一个树结构可以满足我的需求.树节点由其路径标识:根节点路径为

I have a tree structure in a mongo database that fills my need. A tree node is identified by its path: a root node path is

#<id>

和一个子节点在其路径中描述其完整层次结构:

and a sub node describes its full hierarchy in its path:

#<id of parent>#<id of parent>#<id>

例如,我可以拥有以下树:

For instance I can have the following tree:

#a
|_#a#b
|    |_#a#b#1
|    |_#a#b#2
|_#a#c
    |_#a#c#1
          |_#a#c#1#x

我目前有一种方法可以使用$ regex获取树节点的所有子级.应用于

I currently have a mean to get all children of a tree node using $regex. Applying it to

#a#c

在这种情况下将返回以下节点

would in this case return the following nodes

#a#c#1
#a#c#1#x

现在,我正在尝试找到一种仅查找树上叶子的方法.因此,将其应用于同一节点,我只会得到:

Now I am trying to find a way to find only the leaves of my tree. So applying it to the same node I would only get:

#a#c#1#x

我可以获取完整列表,然后删除"group"元素,但这不是很聪明,对吗?

I could get the full list then remove the 'group' elements but that would not be really clever, would it?

由于问题过于简化而进行
万,谢谢您,下面提供的解决方案回答了我提出的问题,以防结构中的元素具有包含父级 path 的字段"parent". 就我而言,我有一个父母"字段,但是它包含父母的 id ,而不是其路径.

Editing because question was oversimplified:
Thank you Wan, the solution provided below answers the question I asked in case elements in the structure have a field 'parent' containing the path of the parent. In my case, I have a field 'parent', but it contains the id of the parent, not its path.

下一个问题是如何将connectToField修改为字段?就我而言,我不想连接到父域,而是想连接到#hierarchy of parent#id of parent

The next question is how do you connectToField to a modified field? In my case instead of connecting to the field parent I would like to connect to #hierarchy of parent#id of parent

是否可以对查询结果进行某种形式的预格式化,以便父级包含路径而不是id?

Is it possible to do some sort of preformating of the query result so that parent contains the path instead of the id?

推荐答案

如果添加字段parent来捕获每个节点的父级,则可能有助于优化对更大数据集的查询.例如:

If you add a field parent to capture the parent for each node, it may help to optimise querying on bigger dataset. For example:

{"parent": "", "node": "#a"}
{"parent": "#a", "node": "#a#b"}
{"parent": "#a", "node": "#a#c"}
{"parent": "#a#b", "node": "#a#b#1"}
{"parent": "#a#b", "node": "#a#b#2"}
{"parent": "#a#c", "node": "#a#c#1"}
{"parent": "#a#c#1", "node": "#a#c#1#x"}

然后,您可以使用 $ graphLookup(聚合)运算符遍历.

Then you can utilise $graphLookup (aggregation) operator to traverse.

正则表达式查询的替代方法,用于获取#a#c的树节点的所有子代:

An alternative to your regex query to get all children of a tree node for #a#c:

db.tree.aggregate([
        {$match:{"node":"#a#c"}}, 
        {$graphLookup:{
                       from:"tree", 
                       startWith:"$node", 
                       connectFromField:"node", 
                       connectToField:"parent", 
                       as:"dep"}}, 
        {$project:{"dep.node":1, "_id":0}}
])

仅查找#a#c的叶子:

db.tree.aggregate([
        {$match:{"parent": {$regex:"^#a#c"}}}, 
        {$graphLookup:{
                       from:"tree", 
                       startWith:"$node", 
                       connectFromField:"node", 
                       connectToField:"parent", 
                       as:"dep"}}, 
        {$match:{dep:[]}}, 
        {$project:{"_id":0, node:1}}
])

我还建议您查看模型树结构,在MongoDB中有多种使用树数据结构的方法.根据您的用例,您应采用某些结构来获取应用程序查询的好处.

I would also recommend to review Model Tree Structures, there are various ways to use tree data structures in MongoDB. Depending on your use case you should employ certain structures for your application querying benefits.

这篇关于Mongodb:仅获取树的叶子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆