Search code examples
neo4jcypher

Neo4j Cypher: Find isolated node


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

enter image description here

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!


Solution

  • [UPDATED]

    This will work if you only care about connect relationships:

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