8.6. Collections

8.6.1. Collections in general
8.6.2. List comprehension
8.6.3. Literal maps

Cypher has good support for collections.

8.6.1. Collections in general

A literal collection is created by using brackets and separating the elements in the collection with commas.

Query. 

RETURN [0,1,2,3,4,5,6,7,8,9] AS collection

Result

collection
1 row

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


Try this query live. none RETURN [0,1,2,3,4,5,6,7,8,9] AS collection

In our examples, we’ll use the range function. It gives you a collection containing all numbers between given start and end numbers.

To access individual elements in the collection, we use the square brackets again.

Query. 

RETURN range(0,10)[3]

Result

range(0,10)[3]
1 row

3


Try this query live. none RETURN range(0,10)[3]

You can also use negative numbers, to start from the end of the collection instead.

Query. 

RETURN range(0,10)[-3]

Result

range(0,10)[-3]
1 row

8


Try this query live. none RETURN range(0,10)[-3]

Finally, you can use ranges inside the brackets to return ranges of the collection.

Query. 

RETURN range(0,10)[0..3]

Result

range(0,10)[0..3]
1 row

[0,1,2]


Try this query live. none RETURN range(0,10)[0..3]

Query. 

RETURN range(0,10)[0..-5]

Result

range(0,10)[0..-5]
1 row

[0,1,2,3,4,5]


Try this query live. none RETURN range(0,10)[0..-5]

Query. 

RETURN range(0,10)[-5..]

Result

range(0,10)[-5..]
1 row

[6,7,8,9,10]


Try this query live. none RETURN range(0,10)[-5..]

Query. 

RETURN range(0,10)[..4]

Result

range(0,10)[..4]
1 row

[0,1,2,3]


Try this query live. none RETURN range(0,10)[..4]

Note: Out-of-bound slices are simply truncated, but out-of-bound single elements return null.

Query. 

RETURN range(0,10)[15]

Result

range(0,10)[15]
1 row

<null>


Try this query live. none RETURN range(0,10)[15]

Query. 

RETURN range(0,10)[5..15]

Result

range(0,10)[5..15]
1 row

[5,6,7,8,9,10]


Try this query live. none RETURN range(0,10)[5..15]

8.6.2. List comprehension

List comprehension is a syntactic construct available in Cypher for creating a collection based on existing collections. It follows the form of the mathematical set-builder notation (set comprehension) instead of the use of map and filter functions.

Query. 

RETURN [x IN range(0,10) WHERE x % 2 = 0 | x^3] AS result

Result

result
1 row

[0.0,8.0,64.0,216.0,512.0,1000.0]


Try this query live. none RETURN [x IN range(0,10) WHERE x % 2 = 0 | x^3] AS result

Either the WHERE part, or the expression, can be omitted, if you only want to filter or map respectively.

Query. 

RETURN [x IN range(0,10) WHERE x % 2 = 0] AS result

Result

result
1 row

[0,2,4,6,8,10]


Try this query live. none RETURN [x IN range(0,10) WHERE x % 2 = 0] AS result

Query. 

RETURN [x IN range(0,10)| x^3] AS result

Result

result
1 row

[0.0,1.0,8.0,27.0,64.0,125.0,216.0,343.0,512.0,729.0,1000.0]


Try this query live. none RETURN [x IN range(0,10)| x^3] AS result

8.6.3. Literal maps

From Cypher, you can also construct maps. In Java, they will be java.util.Map<String,Object>, through REST you will get JSON objects.

Query. 

RETURN { key : "Value", collectionKey: [{ inner: "Map1" }, { inner: "Map2" }]}

Result

{ key : "Value", collectionKey: [ { inner: "Map1" }, { inner: "Map2" } ] }
1 row

{key -> "Value", collectionKey -> [{inner -> "Map1"},{inner -> "Map2"}]}


Try this query live. none RETURN { key : "Value", collectionKey: [{ inner: "Map1" }, { inner: "Map2" }]}