Cypher - find orphans

974 views
Skip to first unread message

Linus Nikander

unread,
Jan 27, 2015, 8:45:04 AM1/27/15
to ne...@googlegroups.com
I want to identify all nodes without relationships to other nodes (orphans). I can find some query examples but they only seem to work with legacy versions of neo4j. Is there an easy way to accomplish this?

I've tried getting the following old query to work but haven't succeeded so far:


START root=node(0), n=node(*)
where not(shortestPath(n-[*..2]-root))
RETURN n

A re-write of the above query or one that accomplishes the same would be perfect. 

Aru Sahni

unread,
Jan 27, 2015, 8:50:01 AM1/27/15
to ne...@googlegroups.com
MATCH (n)
WHERE NOT (n)-[r]-()
RETURN n;

--
You received this message because you are subscribed to the Google Groups "Neo4j" group.
To unsubscribe from this group and stop receiving emails from it, send an email to neo4j+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Linus Nikander

unread,
Jan 28, 2015, 5:15:25 AM1/28/15
to ne...@googlegroups.com
That was easy enough, thank you.

Although I did have to change it slightly to:

MATCH (n)
WHERE NOT (n)-[*]-()
RETURN n;

since r wasn't defined.

Chris Vest

unread,
Jan 28, 2015, 7:14:46 AM1/28/15
to ne...@googlegroups.com
The query will probably run faster if you leave off the `[*]` bit:

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

--
Chris Vest
System Engineer, Neo Technology
[ skype: mr.chrisvest, twitter: chvest ]

Reply all
Reply to author
Forward
0 new messages