英文:
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;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论