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

后端 未结 2 464
没有蜡笔的小新
没有蜡笔的小新 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:44

    This is a typical path query. If you link it to both child and root, you only have to retrieve a single path. Below I assume that you pass myid as a parameter and that you have linked the nodes using :HAS_CHILD relationships

    // the path pattern
    MATCH path=(root)-[:HAS_CHILD*]->(child)  
    // limiting the search
    WHERE NOT ()-[:HAS_CHILD]->(root)
          AND child.ID = $myid
    
    //returning the results
    RETURN path
    
    or 
    
    // return the IDs, except the last one, because it is the node you started from
    UNWIND nodes(path)[..-1] AS node
    RETURN node.ID AS parent
    

提交回复
热议问题