How to find nodes which are not linked to specific other nodes

I wonder if there is a more elegant way to find nodes which are not linked to other specific nodes.

nodes in darker green are "regions"
nodes in lighter green are "countries"
there can be a mix of children, with both "regions" and "countries"
all relationships are of the same type ("rel")

I would like to extract the end region nodes (those regions) which only contain countries underneath, and I thought to be a no-brainer...silly me.

Eventually, this worked, but i do not like it:
match (w:Region{Code:'R1'})
with w optional match (w)-[r]->(i:Region)
with w, collect(r.acr) as p where size(p)=0
return w.Code

Note:r.acr is just some random property which i have been using with collect

I was expecting something like "optional match (e:Region) where not( (e)--(e1:Region) )but as e1 needs to be declared prior to "where", it creates a cartesian product in which, of course, if finds some regions which are not directly linked.

Also tried the count directly in where


match (w:Region{Code:'R1'})
optional match (w)-->(i:Region)
with w, count(i) as cnt
where cnt = 0
return w.Code


match (e:Region{Code:'R1'})
where not exists( (e)-->(:Region ) )
return e.Code

1 Like