Search code examples
graphneo4jcyphergraph-databases

Find all children nodes of a particular node until the leaf node


I'm new to Neo4j and trying to figure out a way to get all the children nodes of a node. Here's my graph model: Graph Model

As you can see, the root node is an SecurityGroup node which may have SecurityGroup nodes under it or User nodes under it. I need to find all the children nodes(both User and SecurityGroups) of the root SecurityGroup node until there is no more nesting.


Solution

  • Here is an example of how you can get all the descendant nodes (according to your graph model)) of a SecurityGroup node with the id property value of 123.

    MATCH (sg:SecurityGroup)<-[*]-(d)
    WHERE sg.id = 123
    RETURN d;
    

    You may want to create an index (or uniqueness constraint) on :SecurityGroup(id) to initiate the query efficiently (rather than scanning all SecurityGroup nodes).