查找没有特定关系的节点(Cypher / neo4j)

时间:2022-04-08 18:04:21

I have a neo4j db with the following:

我有一个neo4j数据库具有以下内容:

a:Foo
b:Bar

about 10% of db have (a)-[:has]->(b)

大约10%的db有(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

以前做() - [r?] - ()会很完美!然而,它不再受支持:(而是,正如他们建议的那样

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...

给我一个null结果,因为可选匹配需要BOTH节点在那里或BOTH节点不在那里......

So how do i get all the a:Foo nodes that are NOT attached to b:Bar?

那么我怎么得到所有的a:没有附加到b:Bar的Foo节点?

Note: dataset is millions of nodes so the query needs to be efficient or otherwise it times out.

注意:数据集是数百万个节点,因此查询需要高效或以其他方式超时。

2 个解决方案

#1


24  

That would be

那就是

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

#2


3  

This also works if you're looking for all singletons/orphans:

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

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

#1


24  

That would be

那就是

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

#2


3  

This also works if you're looking for all singletons/orphans:

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

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