Search code examples
cyphermemgraphdbopencypher

How to query nodes that have no relationships in 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?


Solution

  • 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;