I have a neo4j db with the following:
a:Foo
b:Bar
about 10% of db have (a)-[:has]->(b)
I need to get only the nodes that do NOT have that relationship!
previously doing ()-[r?]-()
would've been perfect! However it is no longer supported :( instead, doing as they suggest a
OPTIONAL MATCH (a:Foo)-[r:has]->(b:Bar) WHERE b is NULL RETURN a
gives me a null result since optional match needs BOTH nodes to either be there or BOTH nodes not to be there...
So how do i get all the a:Foo
nodes that are NOT attached to b:Bar
?
Note: dataset is millions of nodes so the query needs to be efficient or otherwise it times out.
That would be
MATCH (a:Foo) WHERE not ((a)-[:has]->(:Bar)) RETURN a;
This also works if you're looking for all singletons/orphans:
MATCH (a:Foo) WHERE not ((a)--()) RETURN a;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With