Predicates are boolean functions that return true or false for a given set of input.
They are most commonly used to filter out subgraphs in the WHERE
part of a query.
Functions:
all()
returns true if the predicate holds for all elements in the given list.
Syntax: all(variable IN list WHERE predicate)
Returns:
A Boolean. |
Arguments:
Name | Description |
---|---|
|
An expression that returns a list. |
|
This is the variable that can be used from within the predicate. |
|
A predicate that is tested against all items in the list. |
Query.
MATCH p =(a)-[*1..3]->(b)
WHERE a.name = 'Alice' AND b.name = 'Daniel' AND ALL (x IN nodes(p) WHERE x.age > 30)
RETURN p
All nodes in the returned paths will have an age
property of at least '30'.
p |
---|
1 row |
|
Try this query live. CREATE (alice {name:'Alice', age: 38, eyes: 'brown'}), (bob {name: 'Bob', age: 25, eyes: 'blue'}), (charlie {name: 'Charlie', age: 53, eyes: 'green'}), (daniel {name: 'Daniel', age: 54, eyes: 'brown'}), (eskil {name: 'Eskil', age: 41, eyes: 'blue', array: ['one', 'two', 'three']}), (alice)-[:KNOWS]->(bob), (alice)-[:KNOWS]->(charlie), (bob)-[:KNOWS]->(daniel), (charlie)-[:KNOWS]->(daniel), (bob)-[:MARRIED]->(eskil) MATCH p = (a)-[*1..3]->(b) WHERE a.name = 'Alice' AND b.name = 'Daniel' AND all(x IN nodes(p) WHERE x.age > 30) RETURN p
any()
returns true if the predicate holds for at least one element in the given list.
Syntax: any(variable IN list WHERE predicate)
Returns:
A Boolean. |
Arguments:
Name | Description |
---|---|
|
An expression that returns a list. |
|
This is the variable that can be used from within the predicate. |
|
A predicate that is tested against all items in the list. |
Query.
MATCH (a)
WHERE a.name = 'Eskil' AND ANY (x IN a.array WHERE x = 'one')
RETURN a.name, a.array
All nodes in the returned paths have at least one 'one' value set in the array property named array
.
a.name | a.array |
---|---|
1 row |
|
|
|
Try this query live. CREATE (alice {name:'Alice', age: 38, eyes: 'brown'}), (bob {name: 'Bob', age: 25, eyes: 'blue'}), (charlie {name: 'Charlie', age: 53, eyes: 'green'}), (daniel {name: 'Daniel', age: 54, eyes: 'brown'}), (eskil {name: 'Eskil', age: 41, eyes: 'blue', array: ['one', 'two', 'three']}), (alice)-[:KNOWS]->(bob), (alice)-[:KNOWS]->(charlie), (bob)-[:KNOWS]->(daniel), (charlie)-[:KNOWS]->(daniel), (bob)-[:MARRIED]->(eskil) MATCH (a) WHERE a.name = 'Eskil' AND any(x IN a.array WHERE x = 'one') RETURN a.name, a.array
exists()
returns true if a match for the given pattern exists in the graph, or if the specified property exists in the node, relationship
or map.
Syntax: exists(pattern-or-property)
Returns:
A Boolean. |
Arguments:
Name | Description |
---|---|
|
A pattern or a property (in the form 'variable.prop'). |
Query.
MATCH (n)
WHERE exists(n.name)
RETURN n.name AS name, exists((n)-[:MARRIED]->()) AS is_married
The names of all nodes with the name
property are returned, along with a boolean true
/ false
indicating if they are married.
name | is_married |
---|---|
5 rows |
|
|
|
|
|
|
|
|
|
|
|
Try this query live. CREATE (alice {name:'Alice', age: 38, eyes: 'brown'}), (bob {name: 'Bob', age: 25, eyes: 'blue'}), (charlie {name: 'Charlie', age: 53, eyes: 'green'}), (daniel {name: 'Daniel', age: 54, eyes: 'brown'}), (eskil {name: 'Eskil', age: 41, eyes: 'blue', array: ['one', 'two', 'three']}), (alice)-[:KNOWS]->(bob), (alice)-[:KNOWS]->(charlie), (bob)-[:KNOWS]->(daniel), (charlie)-[:KNOWS]->(daniel), (bob)-[:MARRIED]->(eskil) MATCH (n) WHERE exists(n.name) RETURN n.name AS name, exists((n)-[:MARRIED]->()) AS is_married
none()
returns true if the predicate holds for no element in the given list.
Syntax: none(variable IN list WHERE predicate)
Returns:
A Boolean. |
Arguments:
Name | Description |
---|---|
|
An expression that returns a list. |
|
This is the variable that can be used from within the predicate. |
|
A predicate that is tested against all items in the list. |
Query.
MATCH p =(n)-[*1..3]->(b)
WHERE n.name = 'Alice' AND NONE (x IN nodes(p) WHERE x.age = 25)
RETURN p
No node in the returned paths has an age
property set to '25'.
p |
---|
2 rows |
|
|
Try this query live. CREATE (alice {name:'Alice', age: 38, eyes: 'brown'}), (bob {name: 'Bob', age: 25, eyes: 'blue'}), (charlie {name: 'Charlie', age: 53, eyes: 'green'}), (daniel {name: 'Daniel', age: 54, eyes: 'brown'}), (eskil {name: 'Eskil', age: 41, eyes: 'blue', array: ['one', 'two', 'three']}), (alice)-[:KNOWS]->(bob), (alice)-[:KNOWS]->(charlie), (bob)-[:KNOWS]->(daniel), (charlie)-[:KNOWS]->(daniel), (bob)-[:MARRIED]->(eskil) MATCH p = (n)-[*1..3]->(b) WHERE n.name = 'Alice' AND none(x IN nodes(p) WHERE x.age = 25) RETURN p
single()
returns true if the predicate holds for exactly one of the elements in the given list.
Syntax: single(variable IN list WHERE predicate)
Returns:
A Boolean. |
Arguments:
Name | Description |
---|---|
|
An expression that returns a list. |
|
This is the variable that can be used from within the predicate. |
|
A predicate that is tested against all items in the list. |
Query.
MATCH p =(n)-->(b)
WHERE n.name = 'Alice' AND SINGLE (var IN nodes(p) WHERE var.eyes = 'blue')
RETURN p
Exactly one node in every returned path has the eyes
property set to 'blue'.
p |
---|
1 row |
|
Try this query live. CREATE (alice {name:'Alice', age: 38, eyes: 'brown'}), (bob {name: 'Bob', age: 25, eyes: 'blue'}), (charlie {name: 'Charlie', age: 53, eyes: 'green'}), (daniel {name: 'Daniel', age: 54, eyes: 'brown'}), (eskil {name: 'Eskil', age: 41, eyes: 'blue', array: ['one', 'two', 'three']}), (alice)-[:KNOWS]->(bob), (alice)-[:KNOWS]->(charlie), (bob)-[:KNOWS]->(daniel), (charlie)-[:KNOWS]->(daniel), (bob)-[:MARRIED]->(eskil) MATCH p = (n)-->(b) WHERE n.name = 'Alice' AND single(var IN nodes(p) WHERE var.eyes = 'blue') RETURN p