38

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?

Karol Selak
  • 4,248
  • 6
  • 35
  • 65
Andrei R
  • 4,904
  • 5
  • 25
  • 26

3 Answers3

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
Charlotte
  • 1,253
  • 1
  • 16
  • 21
manonthemat
  • 6,101
  • 1
  • 24
  • 49
7

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
Dan Christos
  • 71
  • 1
  • 2
0
MATCH (f) WHERE f.foo IS NULL RETURN f
Andrei R
  • 4,904
  • 5
  • 25
  • 26