I'm very new to graph databases and neo4j/cypher. I can load info from a csv file and make some basic cypher sentences, but I'm having a hard time understanding how to exclude subtrees from my result tree. Below is an image of an example of my graph, it is a simple parent-child tree.
The problem comes when I want to apply a business rule: when you find a child node where the level is greater or equals than his father, then the child must be excluded and all his descendants too, all the relations and nodes of the subtree must not be part of the final result.
I tried with:
match (p:Person {id:"A" })<-[r:CHILD_OF*]-(c:Person)
where c.level < p.level
return c,p;
But unfortunately I'm getting undesired nodes and relations because I can not traverse and exclude the complete subtrees with its branches:
What I'm trying to achieve is this:
Any help would be greatly appreciated.
I will collect all children with levels more than their parents then check the ALL of these child do not have a child or children in the result.
MATCH (p:Person {name:"A" })<-[ :CHILD_OF*]-(c:Person)
WHERE c.level >= p.level
WITH p, collect(c) as allChildren
MATCH (p)<-[ :CHILD_OF*]-(child:Person)
WHERE child.level < p.level AND ALL(c in allChildren WHERE NOT EXISTS( (c)<-[:CHILD_OF*]-(child)))
RETURN p, child