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.

I have a database with nodes having (n)-[r]-(m) relationship. How to get all the top level nodes having no [Parent] relationship?

I am using the cypher
</>match (n:NodeEntity)
where not (n)<--(:NodeEntity)
return n</>
But this is also returning nodes which have a parent relationship..

2 REPLIES 2

I think you specified an inbound relationship when you should be using an outbound relationship for a parent

where not (n)<--(:NodeEntity)

This is the right way to express this, but this will return tree leaves not parents. Try reversing the arrow direction

Assuming data as follows:

3X_7_b_7b0ec3ae6222336d26354d8a7575daad12db5ded.png

The following query worked:

match(n:NodeEntity)
where not exists ((:NodeEntity)-->(n))
return n

3X_3_8_38f68d9ccd0ecedc4cee5e533f3b68d6f078e1bc.png

if your relationships are in the other direction, flip the direction in the where pattern predicate, as @david.allen suggests.