Excluding label names in simple Neo4j Query
Asked Answered
L

2

34

Usually I can find everything I need already on SO but not this time. I'm looking for a very simple way to exclude labels, for example (pseudo code):

match (n) where n not in (Label1, Label2) return n

Sorry about crappy query. In short I have labels x,y,z and I want to return all of them apart from z.

Thnx!

Lampert answered 28/9, 2015 at 6:44 Comment(1)
match (n:X, (r:Y) return n, r <-- I don't want to do this as I have too many labels to includeLampert
R
71

This should do it:

MATCH (n)
WHERE NOT n:Label1 AND NOT n:Label2
RETURN n;
Rockery answered 28/9, 2015 at 6:58 Comment(1)
Great @EdBaker, please mark my answer as correct to help others.Rockery
T
2

If you have a long list of labels you want to exclude, I find this syntax to be helpful:

match (n)
where not labels(n) in [['label1'],['label2'],['label3']]
Typewrite answered 29/10, 2022 at 21:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.