如何根据ID获取特定节点的所有父节点 [英] How to get all parent nodes of a particular node based on ID
问题描述
我想获取所有第3级节点(下图中的4、5、6和7)及其关系以及其父节点详细信息 在下面的示例中:
I want to fetch all 3rd level nodes(4,5,6 and 7 in below pic) and its relationships along with its parent node details In the below example:
- 如果我发送
ID : 7
,则应该获得3
和`1 的节点信息
- 如果我发送
ID : 4
,则应该获得2
和`1 的节点信息
- If I send
ID : 7
then I should get node info of3
and `1 - If I send
ID : 4
then I should get node info of2
and `1
如何获取父节点详细信息?请帮助
How can I get parent node details? Please help
我正在尝试查询以获取节点,边和直接父级详细信息.我得到的节点和边缘,但与父级一起得到的节点列表很大.不知道为什么
I am trying query to get nodes, edges and immediate parent details. Nodes and edges I am getting but with parent I am getting big list of nodes. Not sure why
Match (n)-[r]-() OPTIONAL MATCH (parent:ParentNodeType)<--(child) return n,r,COLLECT(parent) as parent
推荐答案
因此,如果要获取父级的所有节点,请使用以下查询
So If you want to get all nodes of parent then use below query
Match (n)-[r]-() OPTIONAL MATCH (parent:ParentNodeType)<-[r1*]-(child) return n,r,COLLECT(parent) as parent
正如您在评论中提到的那样,您正在寻找三级家长,因此您也可以提供任何想要的级别编号.请参阅下面的查询以了解具体级别
As you mentioned in comment you are looking for 3rd level parent so you can give level number also whatever you want to get. Please see below query for specific level
Match (n)-[r]-() OPTIONAL MATCH (parent:ParentNodeType)<-[r1*3]-(child) return n,r,COLLECT(parent) as parent
根据我的理解,您无需编写此较长的查询.检查以下查询,是否获得所需的结果
and as per my understanding you do not need to write this long query. check below query if you are getting your required result
MATCH (p)<-[r*3]-(c) return p,r,c
这篇关于如何根据ID获取特定节点的所有父节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!