How to query to get n level depth nodes in neo4j

570 views Asked by At

I want to write a query that will fetch the ciphers(nodes) along with its child nodes to the n-level.

enter image description here

e.g. if any child is having 1 child node and that child node is also having a sub child and that sub child node is also having a sub child node and it continues to the n times. then I want to fetch a result like

{
P: {
    // parent node info,
    child: [
        // data
        {
            a1: {
                // data
                child: [
                    {
                        a2: {
                            // data

                            // And so on...
                        }
                    }
                ]
            }
        },

        {
            b1: {
                // data
                child: [
                    {
                        b2: {
                            // data
                            // And so on...
                        }
                    }
                ]
            }
        }
    ]
}
1

There are 1 answers

0
Thennan On
MATCH(P)-[:child*n]->(C)
RETURN P,C

Will return all the nodes in the 'child' path from node P.
'n' specifies the number of hops between P and C with the child relationship.

MATCH(P)-[:child*]->(C)
RETURN P,C

Will return all the nodes in the 'child' path, irrespective of the number of child relationship hops in-between.
Reference - Specifying varying length paths