Neo4j Cypher: 查找孤立节点

huangapple go评论51阅读模式
英文:

Neo4j Cypher: Find isolated node

问题

我想找到不与其他节点连接的节点(如下图中的节点A和节点B)。

我尝试过的方法是:

MATCH (n:node) WHERE not ((n)<-[:connect]->(:node)) RETURN n

这似乎只返回了节点B。

如何可以检索到节点A和节点B?

提前感谢!

英文:

I want to find nodes that do not connect with other nodes. (node A and node B in the picture below)

Neo4j Cypher: 查找孤立节点

What I have tried is

MATCH (n:node) WHERE not ((n)<-[:connect]->(:node)) RETURN n 

which seems to return only B.

How can I retrieve both A and B?
Thank you in advance!

答案1

得分: 1

[UPDATED]

如果你只关心connect关系,以下代码将有效:

MATCH (n:node)
WHERE SIZE([(n)-[:connect]-(m:node) WHERE n <> m|1]) = 0
RETURN n

但是,如果你想关注所有关系类型,那么请使用以下代码:

MATCH (n:node)
WHERE SIZE([(n)--(m:node) WHERE n <> m|1]) = 0
RETURN n
英文:

[UPDATED]

This will work if you only care about connect relationships:

MATCH (n:node)
WHERE SIZE([(n)-[:connect]-(m:node) WHERE n &lt;&gt; m|1]) = 0
RETURN n

But if you want to pay attention to all relationship types, then use this:

MATCH (n:node)
WHERE SIZE([(n)--(m:node) WHERE n &lt;&gt; m|1]) = 0
RETURN n

huangapple
  • 本文由 发表于 2023年2月16日 07:21:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75466335.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定