简体   繁体   中英

Finding nodes that do not have specific relationship (Cypher/neo4j)

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.

那就是

MATCH (a:Foo) WHERE not ((a)-[:has]->(:Bar)) RETURN a;

如果您正在寻找所有单身人士/孤儿,这也有效:

MATCH (a:Foo) WHERE not ((a)--()) RETURN a;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM