## 14.3. Collection functions

14.3.1. NODES
14.3.2. RELATIONSHIPS
14.3.3. EXTRACT
14.3.4. FILTER
14.3.5. TAIL
14.3.6. RANGE
14.3.7. REDUCE

Collection functions return collections of things — nodes in a path, and so on.

### 14.3.1. NODES

Returns all nodes in a path.

Syntax: `NODES( path )`

Arguments:

• path: A path.

Query.

```START a=node(3), c=node(2)
MATCH p=a-->b-->c
RETURN NODES(p)```

All the nodes in the path `p` are returned by the example query.

Result

NODES(p)
1 row

`[Node[3]{name:"A",age:38,eyes:"brown"},Node[4]{name:"B",age:25,eyes:"blue"},Node[2]{name:"E",age:41,eyes:"blue",array:["one","two","three"]}]`

Try this query live. (1) {"age":54,"eyes":"brown","name":"D"} (2) {"age":41,"array":["one","two","three"],"eyes":"blue","name":"E"} (3) {"age":38,"eyes":"brown","name":"A"} (4) {"age":25,"eyes":"blue","name":"B"} (5) {"age":53,"eyes":"green","name":"C"} (3)-[:KNOWS]->(4) {} (3)-[:KNOWS]->(5) {} (4)-[:KNOWS]->(1) {} (4)-[:MARRIED]->(2) {} (5)-[:KNOWS]->(1) {} start a=node(3), c=node(2) match p=a-->b-->c return NODES(p)

### 14.3.2. RELATIONSHIPS

Returns all relationships in a path.

Syntax: `RELATIONSHIPS( path )`

Arguments:

• path: A path.

Query.

```START a=node(3), c=node(2)
MATCH p=a-->b-->c
RETURN RELATIONSHIPS(p)```

All the relationships in the path `p` are returned.

Result

RELATIONSHIPS(p)
1 row

`[:KNOWS[0] {},:MARRIED[4] {}]`

Try this query live. (1) {"age":54,"eyes":"brown","name":"D"} (2) {"age":41,"array":["one","two","three"],"eyes":"blue","name":"E"} (3) {"age":38,"eyes":"brown","name":"A"} (4) {"age":25,"eyes":"blue","name":"B"} (5) {"age":53,"eyes":"green","name":"C"} (3)-[:KNOWS]->(4) {} (3)-[:KNOWS]->(5) {} (4)-[:KNOWS]->(1) {} (4)-[:MARRIED]->(2) {} (5)-[:KNOWS]->(1) {} start a=node(3), c=node(2) match p=a-->b-->c return RELATIONSHIPS(p)

### 14.3.3. EXTRACT

To return a single property, or the value of a function from a collection of nodes or relationships, you can use `EXTRACT`. It will go through a collection, run an expression on every element, and return the results in an collection with these values. It works like the `map` method in functional languages such as Lisp and Scala.

Syntax: `EXTRACT( identifier in collection : expression )`

Arguments:

• collection: An expression that returns a collection
• identifier: The closure will have an identifier introduced in it’s context. Here you decide which identifier to use.
• expression: This expression will run once per value in the collection, and produces the result collection.

Query.

```START a=node(3), b=node(4), c=node(1)
MATCH p=a-->b-->c
RETURN extract(n in nodes(p) : n.age)```

The age property of all nodes in the path are returned.

Result

extract(n in nodes(p) : n.age)
1 row

`[38,25,54]`

Try this query live. (1) {"age":54,"eyes":"brown","name":"D"} (2) {"age":41,"array":["one","two","three"],"eyes":"blue","name":"E"} (3) {"age":38,"eyes":"brown","name":"A"} (4) {"age":25,"eyes":"blue","name":"B"} (5) {"age":53,"eyes":"green","name":"C"} (3)-[:KNOWS]->(4) {} (3)-[:KNOWS]->(5) {} (4)-[:KNOWS]->(1) {} (4)-[:MARRIED]->(2) {} (5)-[:KNOWS]->(1) {} start a=node(3), b=node(4), c=node(1) match p=a-->b-->c return extract(n in nodes(p) : n.age)

### 14.3.4. FILTER

`FILTER` returns all the elements in a collection that comply to a predicate.

Syntax: `FILTER(identifier in collection : predicate)`

Arguments:

• collection: An expression that returns a collection
• identifier: This is the identifier that can be used from the predicate.
• predicate: A predicate that is tested against all items in the collection.

Query.

```START a=node(2)
RETURN a.array, filter(x in a.array : length(x) = 3)```

This returns the property named `array` and a list of values in it, which have the length `3`.

Result

a.arrayfilter(x in a.array : length(x) = 3)
1 row

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

`["one","two"]`

Try this query live. (1) {"age":54,"eyes":"brown","name":"D"} (2) {"age":41,"array":["one","two","three"],"eyes":"blue","name":"E"} (3) {"age":38,"eyes":"brown","name":"A"} (4) {"age":25,"eyes":"blue","name":"B"} (5) {"age":53,"eyes":"green","name":"C"} (3)-[:KNOWS]->(4) {} (3)-[:KNOWS]->(5) {} (4)-[:KNOWS]->(1) {} (4)-[:MARRIED]->(2) {} (5)-[:KNOWS]->(1) {} start a=node(2) return a.array, filter(x in a.array : length(x) = 3)

### 14.3.5. TAIL

`TAIL` returns all but the first element in a collection.

Syntax: `TAIL( expression )`

Arguments:

• expression: This expression should return a collection of some kind.

Query.

```START a=node(2)
RETURN a.array, tail(a.array)```

This returns the property named `array` and all elements of that property except the first one.

Result

a.arraytail(a.array)
1 row

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

`["two","three"]`

Try this query live. (1) {"age":54,"eyes":"brown","name":"D"} (2) {"age":41,"array":["one","two","three"],"eyes":"blue","name":"E"} (3) {"age":38,"eyes":"brown","name":"A"} (4) {"age":25,"eyes":"blue","name":"B"} (5) {"age":53,"eyes":"green","name":"C"} (3)-[:KNOWS]->(4) {} (3)-[:KNOWS]->(5) {} (4)-[:KNOWS]->(1) {} (4)-[:MARRIED]->(2) {} (5)-[:KNOWS]->(1) {} start a=node(2) return a.array, tail(a.array)

### 14.3.6. RANGE

Returns numerical values in a range with a non-zero step value step. Range is inclusive in both ends.

Syntax: `RANGE( start, end [, step] )`

Arguments:

• start: A numerical expression.
• end: A numerical expression.
• step: A numerical expression.

Query.

```START n=node(1)
RETURN range(0,10), range(2,18,3)```

Two lists of numbers are returned.

Result

range(0,10)range(2,18,3)
1 row

`[0,1,2,3,4,5,6,7,8,9,10]`

`[2,5,8,11,14,17]`

Try this query live. (1) {"age":54,"eyes":"brown","name":"D"} (2) {"age":41,"array":["one","two","three"],"eyes":"blue","name":"E"} (3) {"age":38,"eyes":"brown","name":"A"} (4) {"age":25,"eyes":"blue","name":"B"} (5) {"age":53,"eyes":"green","name":"C"} (3)-[:KNOWS]->(4) {} (3)-[:KNOWS]->(5) {} (4)-[:KNOWS]->(1) {} (4)-[:MARRIED]->(2) {} (5)-[:KNOWS]->(1) {} start n=node(1) return range(0,10), range(2,18,3)

### 14.3.7. REDUCE

To run an expression against individual elements of a collection, and store the result of the expression in an accumulator, you can use `REDUCE`. It will go through a collection, run an expression on every element, storing the partial result in the accumulator. It works like the `fold` or `reduce` method in functional languages such as Lisp and Scala.

Syntax: `REDUCE( accumulator = initial, identifier in collection : expression )`

Arguments:

• accumulator: An identifier that will hold the result and the partial results as the collection is iterated
• initial: An expression that runs once to give a starting value to the accumulator
• collection: An expression that returns a collection
• identifier: The closure will have an identifier introduced in it’s context. Here you decide which identifier to use.
• expression: This expression will run once per value in the collection, and produces the result value.

Query.

```START a=node(3), b=node(4), c=node(1)
MATCH p=a-->b-->c
RETURN reduce(totalAge = 0, n in nodes(p) : totalAge + n.age)```

The age property of all nodes in the path are summed and returned as a single value.

Result

reduce(totalAge = 0, n in nodes(p) : totalAge + n.age)
1 row

`117`

Try this query live. (1) {"age":54,"eyes":"brown","name":"D"} (2) {"age":41,"array":["one","two","three"],"eyes":"blue","name":"E"} (3) {"age":38,"eyes":"brown","name":"A"} (4) {"age":25,"eyes":"blue","name":"B"} (5) {"age":53,"eyes":"green","name":"C"} (3)-[:KNOWS]->(4) {} (3)-[:KNOWS]->(5) {} (4)-[:KNOWS]->(1) {} (4)-[:MARRIED]->(2) {} (5)-[:KNOWS]->(1) {} start a=node(3), b=node(4), c=node(1) match p=a-->b-->c return reduce(totalAge = 0, n in nodes(p) : totalAge + n.age)