Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"NOT" operator for MATCH in Neo4j

Tags:

neo4j

cypher

In Neo4j 2.x, it is easy to find all nodes with a certain value for a given property using Cypher 2.0, like so:

MATCH (node {property: 'value'}) 
RETURN node;

However, how would I find all nodes with any value for property except for "value"? I tried using the NOT operator like so, but this simply returns a syntax error:

MATCH (node {property: NOT 'value'}) 
RETURN node;
like image 357
Shrey Gupta Avatar asked Mar 20 '15 16:03

Shrey Gupta


People also ask

What does match do in Neo4j?

The MATCH clause allows you to specify the patterns Neo4j will search for in the database. This is the primary way of getting data into the current set of bindings. It is worth reading up more on the specification of the patterns themselves in Patterns.

What is optional match in Neo4j?

An OPTIONAL MATCH matches patterns against your graph database, just like a MATCH does. The difference is that if no matches are found, OPTIONAL MATCH will use a null for missing parts of the pattern. OPTIONAL MATCH could be considered the Cypher equivalent of the outer join in SQL.

What must Relationships in Neo4j have?

Relationships always has a direction (one direction). Relationships must have a type (one type) to define (classify) what type of relationship they are. Nodes and relationships can have properties (key-value pairs), which further describe them.


1 Answers

The concise syntax for properties in the MATCH clause only works with exact matches.

If you want to do any other kind of match condition, you should the WHERE clause:

MATCH (node)
WHERE node.property <> 'value'
RETURN node;

Another example:

MATCH (node)
WHERE NOT node.property IN ["red", "blue", "yellow"]
RETURN node;

Please note this will do an entire graph scan, which is strongly discouraged. On any reasonable dataset size, this query might not complete or at least not in a timely fashion.

like image 180
albertoperdomo Avatar answered Sep 20 '22 17:09

albertoperdomo