How to get all parent nodes of a particular node based on ID

后端 未结 2 491
没有蜡笔的小新
没有蜡笔的小新 2021-01-28 05:22

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:

  • If I send ID : 7
2条回答
  •  没有蜡笔的小新
    2021-01-28 05:43

    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
    

提交回复
热议问题