Find Neo4j nodes where the property is not set
Asked Answered
M

3

39

Using Cypher, how can I find a node where a property doesn't exist?

For example, I have two nodes:

A = {foo: true, name: 'A'},  B = { name: 'B'}

Now I'd like to find B, selecting it on the basis of not having the foo property set. How can I do this?

Mellie answered 15/2, 2016 at 2:36 Comment(0)
A
64

As Michael Hunger mentioned

MATCH (n) WHERE NOT EXISTS(n.foo) RETURN n

On older versions of Neo4j you can use HAS:

# Causes error with later versions of Neo4j
MATCH (n) WHERE NOT HAS(n.foo) RETURN n
Angellaangelle answered 15/2, 2016 at 6:35 Comment(4)
use NOT exists(n.foo) for future compatibility.Adolescent
Has is no longer supported by Neo4j and produces an errorMultiplicate
You can also use IS NULL and IS NOT NULL. For example MATCH (n) WHERE n.foo IS NULL RETURN nMoreta
This answer is no longer correct, as EXISTS has long been deprecated.Zsazsa
Z
11

As of version 4.3 EXISTS has been deprecated on properties and instead, you should use IS NOT NULL.

So for the example in your question your query would now be:

MATCH (n) WHERE n.foo IS NULL RETURN n
Zsazsa answered 30/4, 2022 at 4:2 Comment(2)
This should be set as the correct answer. Is still the case as of 2024Deforce
I agree, but by the time it makes it up the list it might be irrelevant... hahaZsazsa
M
1
MATCH (f) WHERE f.foo IS NULL RETURN f
Mellie answered 15/2, 2016 at 2:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.