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.
See also the section called “Comparison operators”.
all()
Tests whether a predicate holds for all elements of this list.
Syntax: all(variable IN list WHERE predicate)
Arguments:
- list: An expression that returns a list
- variable: This is the variable that can be used from the predicate.
- 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.
Result
p |
---|
1 row |
|
Try this query live create (_0:`bar`:`foo` {`age`:38, `eyes`:"brown", `name`:"Alice"}) create (_1 {`age`:25, `eyes`:"blue", `name`:"Bob"}) create (_2 {`age`:53, `eyes`:"green", `name`:"Charlie"}) create (_3 {`age`:54, `eyes`:"brown", `name`:"Daniel"}) create (_4:`Spouse` {`age`:41, `array`:["one", "two", "three"], `eyes`:"blue", `name`:"Eskil"}) create (_0)-[:`KNOWS`]->(_2) create (_0)-[:`KNOWS`]->(_1) create (_1)-[:`MARRIED`]->(_4) create (_1)-[:`KNOWS`]->(_3) create (_2)-[:`KNOWS`]->(_3) ; 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()
Tests whether a predicate holds for at least one element in the list.
Syntax: any(variable IN list WHERE predicate)
Arguments:
- list: An expression that returns a list
- variable: This is the variable that can be used from the predicate.
- 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
All nodes in the returned paths has at least one one
value set in the array property named array
.
Result
a |
---|
1 row |
|
Try this query live create (_0:`bar`:`foo` {`age`:38, `eyes`:"brown", `name`:"Alice"}) create (_1 {`age`:25, `eyes`:"blue", `name`:"Bob"}) create (_2 {`age`:53, `eyes`:"green", `name`:"Charlie"}) create (_3 {`age`:54, `eyes`:"brown", `name`:"Daniel"}) create (_4:`Spouse` {`age`:41, `array`:["one", "two", "three"], `eyes`:"blue", `name`:"Eskil"}) create (_0)-[:`KNOWS`]->(_2) create (_0)-[:`KNOWS`]->(_1) create (_1)-[:`MARRIED`]->(_4) create (_1)-[:`KNOWS`]->(_3) create (_2)-[:`KNOWS`]->(_3) ; match (a) where a.name='Eskil' and any(x in a.array WHERE x = "one") return a
none()
Returns true if the predicate holds for no element in the list.
Syntax: none(variable in list WHERE predicate)
Arguments:
- list: An expression that returns a list
- variable: This is the variable that can be used from the predicate.
- 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 nodes in the returned paths has a age
property set to 25
.
Result
p |
---|
2 rows |
|
|
Try this query live create (_0:`bar`:`foo` {`age`:38, `eyes`:"brown", `name`:"Alice"}) create (_1 {`age`:25, `eyes`:"blue", `name`:"Bob"}) create (_2 {`age`:53, `eyes`:"green", `name`:"Charlie"}) create (_3 {`age`:54, `eyes`:"brown", `name`:"Daniel"}) create (_4:`Spouse` {`age`:41, `array`:["one", "two", "three"], `eyes`:"blue", `name`:"Eskil"}) create (_0)-[:`KNOWS`]->(_2) create (_0)-[:`KNOWS`]->(_1) create (_1)-[:`MARRIED`]->(_4) create (_1)-[:`KNOWS`]->(_3) create (_2)-[:`KNOWS`]->(_3) ; 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 list.
Syntax: single(variable in list WHERE predicate)
Arguments:
- list: An expression that returns a list
- variable: This is the variable that can be used from the predicate.
- 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 will have the eyes
property set to "blue"
.
Result
p |
---|
1 row |
|
Try this query live create (_0:`bar`:`foo` {`age`:38, `eyes`:"brown", `name`:"Alice"}) create (_1 {`age`:25, `eyes`:"blue", `name`:"Bob"}) create (_2 {`age`:53, `eyes`:"green", `name`:"Charlie"}) create (_3 {`age`:54, `eyes`:"brown", `name`:"Daniel"}) create (_4:`Spouse` {`age`:41, `array`:["one", "two", "three"], `eyes`:"blue", `name`:"Eskil"}) create (_0)-[:`KNOWS`]->(_2) create (_0)-[:`KNOWS`]->(_1) create (_1)-[:`MARRIED`]->(_4) create (_1)-[:`KNOWS`]->(_3) create (_2)-[:`KNOWS`]->(_3) ; match p=(n)-->(b) where n.name='Alice' and SINGLE(var in nodes(p) WHERE var.eyes = "blue") return p
exists()
Returns true if a match for the pattern exists in the graph, or the property exists in the node, relationship or map.
Syntax: exists( pattern-or-property )
Arguments:
- pattern-or-property: 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
This query returns all the nodes with a name property along with a boolean true/false indicating if they are married.
Result
name | is_married |
---|---|
5 rows | |
|
|
|
|
|
|
|
|
|
|
Try this query live create (_0:`bar`:`foo` {`age`:38, `eyes`:"brown", `name`:"Alice"}) create (_1 {`age`:25, `eyes`:"blue", `name`:"Bob"}) create (_2 {`age`:53, `eyes`:"green", `name`:"Charlie"}) create (_3 {`age`:54, `eyes`:"brown", `name`:"Daniel"}) create (_4:`Spouse` {`age`:41, `array`:["one", "two", "three"], `eyes`:"blue", `name`:"Eskil"}) create (_0)-[:`KNOWS`]->(_2) create (_0)-[:`KNOWS`]->(_1) create (_1)-[:`MARRIED`]->(_4) create (_1)-[:`KNOWS`]->(_3) create (_2)-[:`KNOWS`]->(_3) ; match (n) where EXISTS(n.name) return n.name AS name, EXISTS( (n)-[:MARRIED]->() ) AS is_married