Boolean operators
Boolean operators are used to combine or evaluate logical conditions. Cypher® contains the following boolean operators:
-
Conjunction:
AND
-
Disjunction:
OR
-
Exclusive disjunction:
XOR
-
Negation:
NOT
a | b | a AND b |
a OR b |
a XOR b |
NOT a |
---|---|---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Example graph
The following graph is used for the examples below:
To recreate the graph, run the following query in an empty Neo4j database:
CREATE (alice:Person {name:'Alice', age: 65, role: 'Project manager', email: 'alice@company.com'}),
(cecil:Person {name: 'Cecil', age: 25, role: 'Software developer', email: 'cecil@private.se'}),
(cecilia:Person {name: 'Cecilia', age: 31, role: 'Software developer'}),
(charlie:Person {name: 'Charlie', age: 61, role: 'Security engineer'}),
(daniel:Person {name: 'Daniel', age: 39, role: 'Director', email: 'daniel@company.com'}),
(eskil:Person {name: 'Eskil', age: 39, role: 'CEO', email: 'eskil@company.com'})
Examples
AND
operatorMATCH (n:Person)
WHERE n.age > 30 AND n.role = 'Software developer'
RETURN n.name AS name, n.age AS age, n.role AS role
name | age | role |
---|---|---|
|
|
|
Rows: 1 |
OR
operatorMATCH (n:Person)
WHERE n.age < 30 OR n.role = 'Software developer'
RETURN n.name AS name, n.age AS age, n.role AS role
name | age | role |
---|---|---|
|
|
|
|
|
|
Rows: 2 |
XOR
operatorMATCH (n:Person)
WHERE n.age > 30 XOR n.role = 'Software developer'
RETURN n.name AS name, n.age AS age, n.role AS role
name | age | role |
---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Rows: 5 |
NOT
operatorMATCH (n:Person)
WHERE NOT n.age = 39
RETURN n.name AS name, n.age AS age
name | age |
---|---|
|
|
|
|
|
|
|
|
Rows: 4 |
MATCH (n:Person)
WHERE n.role = 'Software developer' XOR (n.age > 60 AND n.role = 'Security engineer') OR NOT (n.role = 'Director' OR n.name = 'Eskil')
RETURN n.name AS name, n.age AS age, n.role AS role
name | age | role |
---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
Rows: 3 |