user2805657
user2805657

Reputation: 1

neo4j Cypher Query

i have a following graph in neo4j graph database and by using the cypher query language, i want to retrieve the whole data with is connected to root node and their child node.

For example :

kindly find the below graph image.

[As per the image, node 1 has two child and their child also have too many child with the same relationship. now what i want, using Cypher, i hit the node 1 and it should response with the whole data of child node and there child node and so on, relationship between nodes are "Parent_of" relationship.]

enter image description here

can anyone help me on this.

Upvotes: 0

Views: 292

Answers (2)

Praveen Tankala
Praveen Tankala

Reputation: 13

You can simply traverse from the parent to all of the children using the * operator.

Example cypher query:

// To get entire tree
MATCH(parent: Node{id:1})-[:Parent_of*]->(Childs: Node)
return parent, Childs

// To get all child ids
MATCH(parent: Node{id:1})-[:Parent_of*]->(Childs: Node)
return distinct childs.id;

Upvotes: 0

aherve
aherve

Reputation: 4070

start n=node(1) // use the id, or find it using an index
match n-[:parent_of*0..]->m
return m

will get you all the graph nodes in m. You could also take m.some_property instead of m if you don't want the node itself, but some property that is stored in your nodes.

Careful though, as the path has no limit, this query could become pretty huge in a large graph.

Upvotes: 2

Related Questions