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?

3 Answers 3

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
3
  • 10
    use NOT exists(n.foo) for future compatibility. Feb 15, 2016 at 13:48
  • Has is no longer supported by Neo4j and produces an error
    – Liam
    Feb 13, 2017 at 14:17
  • 2
    You can also use IS NULL and IS NOT NULL. For example MATCH (n) WHERE n.foo IS NULL RETURN n
    – bikeman868
    Nov 1, 2019 at 22:50
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
0
MATCH (f) WHERE f.foo IS NULL RETURN f

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.