cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Function to know the root or child nodes

Dear,
thanks for this amazing community, I was looking for a function to be used in cypher to get the node children or node parent, how to achieve this in cypher or apoc
noting that I have node property or label only

Thanks and regards

4 REPLIES 4

martin3
Node Clone

Do you mean:

MATCH (child:Person {property: x})-[relationship:CHILD_OF]->(parent:Person) RETURN parent?

MATCH (child:Person {property: x})<-[relationship:PARENT_OF]-(parent:Person) RETURN child?

Or what do you mean? What is your datamodel?

I think there is no inheritance in the standard graph model of neo4j. However, I am also interested in ways to implement concepts of inheritance.

hi, @martin3 thanks for your quick response,
in general, I am trying to get the node children or node parents but it seems we can't know this directly
basically, I have a property name for example "node title" so when I match I use this
MATCH p = (n{property: x})-[r]->(m) RETURN p
after this match, it will return the node and all its relationships,
now I was trying to do a match in cypher or using apoc to get children's only of the node with property x.
The main purpose of this is to find the path of a list of nodes and I don't have except their property value in a given list

if you like this is my graph model

CREATE (root:A{name:"root"}), (rc1:R_C_1{name:"first child of the root"}), (rc2:R_C_2{name:"second child of the root"}), 

												(ch1:R_C_1_1{name:"first child of the first root child"}), (ch2:R_C_1_2{name:"second child of the first root child"}),

												(node1:NODE_1{name:"node 1"}),(node2:NODE_2{name:"node 2"}), (node3:NODE_3{name:"node 3"})



CREATE (root)-[:X]->(rc1)

CREATE (root)-[:X]->(rc2)



CREATE (rc1)-[:Y]->(ch1)

CREATE (rc1)-[:Y]->(ch2)



CREATE (node1)-[:Z]->(ch1)

CREATE (node2)-[:M]->(ch1)

CREATE (node3)-[:R]->(ch1)



CREATE (node1)-[:Z]->(ch2)

CREATE (node2)-[:M]->(ch2)

and I am trying to find the following:

// this should return the given nodes only

WITH ['root', 'first child of the root', 'first child of the first root child', 'node 1', 'node 2', 'node 3'] as names

MATCH p = (n1)-[*]-()

WHERE n1.name IN names

RETURN p

// if I tried to find the path of ['root', 'first child of the root', 'node 1', 'node 2', 'node 3'] // this should return the nodes and thier relasionships ['root', 'first child of the root', 'first child of the first root child' , 'second child of the first root child' , 'node 1', 'node 2', 'node 3']

hope we can find a solution for this.
Thanks

If I'm understanding the situation properly, the simplest solution would be to assign variables to individual objects instead of the whole path. For example, rather than

MATCH p = (n:Label{property: x})-[r]->(m)
RETURN p

you could do

MATCH (n:Label{property: x})-->(m)
RETURN m

This performs exactly the same traversal, but only returns the child nodes. No relationships, and no parent.

@brian.freitas1 thanks a lot, yes I was looking for a function or cypher query to return the parent or the child of the node, so I have to do what you say

MATCH (n:Label{property: x})-->(m)
RETURN m

where node m is the child of node n

thanks for you help.