如何查询在Memgraph中没有关系的节点?

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

How to query nodes that have no relationships in Memgraph?

问题

以下是翻译好的部分:

我正在尝试找到一种简单的方法来查询数据库,以查找孤立节点 - 即那些没有关系的节点。我尝试运行:

MATCH (n:NodeA)
WHERE NOT (n)-[]->(:NodeB)
RETURN n;

但在Memgraph中不起作用。是否有人知道如何做到这一点?

英文:

I am trying to find a simple way to query the database to find orphan nodes - the ones that have no relationships. I tried running:

MATCH (n:NodeA)
WHERE NOT (n)-[]->(:NodeB)
RETURN n; 

but it is not working in Memgraph. Does anyone know how to do it?

答案1

得分: 1

@KateLatte的建议使用Memgraph的degree函数是最佳答案,如果您真正想要查找“所有”“孤立节点”,因为它应该很快。

但是我还想纠正一下您原始的openCypher查询。假设您正在使用Memgraph 2.5.2,您的查询实际上并没有尝试查找“所有”“孤立节点”。它只是在查找NodeA节点,这些节点没有指向NodeB节点的出站关系。

要在openCypher中查找“所有”孤立节点,您应该使用以下查询:

MATCH (n)
WHERE NOT (n)--()
RETURN n;

英文:

@KateLatte's suggestion to use Memgraph's degree function is the best answer if you truly want to find all "orphan nodes", since it should be fast.

But I want to also correct your original openCypher query. Assuming that you are using Memgraph 2.5.2, your query was not actually attempting to find all "orphan nodes". It is just looking for NodeA nodes that have no outgoing relationship to a NodeB node.

To find all orphan nodes in openCypher, you should have to used this query:

MATCH (n)
WHERE NOT (n)--()
RETURN n; 

答案2

得分: 0

使用Memgraph 2.5.2 最简单的方法是运行:

MATCH (n) WHERE degree(n) = 0 RETURN n;

这个查询不使用任何聚合,因此内存消耗极低,算法复杂度是线性的。

从Memgraph 2.5.3 开始,您将能够使用您提到的查询:

MATCH (n:NodeA)
WHERE NOT (n)-[]->(:NodeB)
RETURN n;
英文:

The easiest way to do it with Memgraph 2.5.2 is by running:

MATCH (n) WHERE degree(n) = 0 RETURN n; 

That query doesn't use any aggregation, so super low memory consumption and linear algorithmic complexity.

From Memgraph 2.5.3, you'll be able to use the query you mentioned:

MATCH (n:NodeA)
WHERE NOT (n)-[]->(:NodeB)
RETURN n;

huangapple
  • 本文由 发表于 2023年3月7日 17:46:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75660294.html
匿名

发表评论

匿名网友

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

确定