Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Neo4j, match node with exactly one label

Tags:

neo4j

cypher

I have 2 type of nodes. The first type have the :Schema label. The second type have both :Root and :Schema labels. I want to write a cypher that will match only the first type (containing only the :Schema label).

I have tried this:

MATCH (s:Schema) return s;

Which return the two types. Also getting the second type is easy, you just use

MATCH (s:Schema:Root) return s;

So, what's the proper cypher to match nodes with exactly the requested label?

like image 671
Gab Avatar asked Feb 27 '15 16:02

Gab


2 Answers

You can check the length of the LABELS() collection (which is handy if you don't know which other labels to exclude, but only know you want a single label):

MATCH (s:Schema)
WHERE LENGTH(LABELS(s)) = 1
return s;
like image 181
David Makogon Avatar answered Nov 13 '22 08:11

David Makogon


Try something like this:

match (s:Schema)
WHERE Not s:Root
RETURN s
like image 28
Simon Avatar answered Nov 13 '22 06:11

Simon