### 3.4.1. Predicate functions

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:

#### 3.4.1.1. all()

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

list

An expression that returns a list.

variable

This is the variable that can be used from within 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'.

Table 3.212. Result
p

1 row

(0)-[KNOWS,1]->(2)-[KNOWS,3]->(3)

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

#### 3.4.1.2. any()

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

list

An expression that returns a list.

variable

This is the variable that can be used from within 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.name, a.array

All nodes in the returned paths have at least one 'one' value set in the array property named array.

Table 3.213. Result
a.name a.array

1 row

"Eskil"

["one","two","three"]

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

#### 3.4.1.3. exists()

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

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

The names of all nodes with the name property are returned, along with a boolean true / false indicating if they are married.

Table 3.214. Result
name is_married

5 rows

"Alice"

false

"Bob"

true

"Charlie"

false

"Daniel"

false

"Eskil"

false

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

#### 3.4.1.4. none()

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

list

An expression that returns a list.

variable

This is the variable that can be used from within 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 node in the returned paths has an age property set to '25'.

Table 3.215. Result
p

2 rows

(0)-[KNOWS,1]->(2)

(0)-[KNOWS,1]->(2)-[KNOWS,3]->(3)

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

#### 3.4.1.5. single()

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

list

An expression that returns a list.

variable

This is the variable that can be used from within 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 has the eyes property set to 'blue'.

Table 3.216. Result
p

1 row

(0)-[KNOWS,0]->(1)

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