0

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?

cybersam
  • 63,203
  • 6
  • 53
  • 76
KateLatte
  • 611
  • 1
  • 12

2 Answers2

1

@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; 
cybersam
  • 63,203
  • 6
  • 53
  • 76
0

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;
KateLatte
  • 611
  • 1
  • 12