List operators
Example graph
The following graph is used for the examples below:
To recreate the graph, run the following query in an empty Neo4j database:
CREATE (alice:Person {name:'Alice', age: 65, role: 'Project manager', email: 'alice@company.com'}),
(cecil:Person {name: 'Cecil', age: 25, role: 'Software developer', email: 'cecil@private.se'}),
(cecilia:Person {name: 'Cecilia', age: 31, role: 'Software developer'}),
(charlie:Person {name: 'Charlie', age: 61, role: 'Security engineer'}),
(daniel:Person {name: 'Daniel', age: 39, role: 'Director', email: 'daniel@company.com'}),
(eskil:Person {name: 'Eskil', age: 39, role: 'CEO', email: 'eskil@company.com'})
Examples
LIST
membership checksIN
operatorMATCH (n:Person)
WHERE n.role IN ['Software developer', 'Project manager']
RETURN n.name AS name, n.role AS role
name | role |
---|---|
|
|
|
|
|
|
Rows: 3 |
LIST
MATCH (p:Person)
WITH p, ['Software developer', 'CEO'] AS roles
WHERE p.role IN roles
RETURN p.name AS name, p.role AS role
name | role |
---|---|
|
|
|
|
|
|
Rows: 3 |
The below query finds Person
nodes that share the role
of Cecil
or Eskil
but have a different name
.
IN
operatorsWITH ['Cecil', 'Eskil'] AS names
MATCH (ce:Person)
WHERE ce.name IN names
WITH collect(ce.role) AS roles, names
MATCH (p:Person)
WHERE p.role IN roles AND NOT p.name IN names
RETURN p.name AS name, p.role AS role
Only Cecilia
is returned because she shares a role
with Cecil
(no Person
node in the graph shares a role
with Eskil
).
name | role |
---|---|
|
|
Rows: 1 |
LIST
values containing duplicatesThe presence of duplicate values in a LIST
does not affect the result of the IN
operator, which checks whether an element appears at least once.
For example, the below query returns TRUE
(two Person
nodes are 39
years old).
LIST
with duplicate valuesMATCH (p:Person)
WITH collect(p.age) AS allAges
RETURN 39 IN allAges AS listWithDuplicates
listWithDuplicates |
---|
|
Rows: 1 |
NULL
behavior
When NULL
is involved in a membership check, the result is will be NULL
.
IN
operator with NULL
RETURN NULL IN [1, 2, NULL] AS nullInList, 123 IN NULL AS valueInNull
nullInList | valueInNull |
---|---|
|
|
Rows: 1 |
To check if NULL
is a member of a LIST
, use the any()
function:
NULL
is a member of a LIST
RETURN any(x IN [1, 2, NULL] WHERE x IS NULL) AS containsNull
containsNull |
---|
|
Rows: 1 |
Nested lists
When used with nested LIST
values, the IN
operator evaluates whether a LIST
is an exact match to any of the nested LIST
values that are part of an outer LIST
.
Partial matches of individual elements within a nested LIST
will return FALSE
.
LIST
valuesRETURN [0, 2] IN [[1, 2], [3, 4]] AS listInNestedList
listInNestedList |
---|
|
Rows: 1 |
LIST
valuesRETURN [3, 4] IN [[1, 2], [3, 4]] AS listInNestedList
listInNestedList |
---|
|
Rows: 1 |
LIST
valuesRETURN [1] IN [[1, 2], [3, 4]] AS listInNestedTest
listInNestedList |
---|
|
Rows: 1 |
List subsets
A subset check verifies if all elements of one LIST
exist in another.
The all()
function is used to ensure that every element in the first LIST
is found in the second LIST
.
WITH [1,3,4] AS sub, [3,5,1,7,6,2,8,4] AS list
RETURN all(x IN sub WHERE x IN list) AS subInList
This returns TRUE
because all elements in sub
are part of list
.
subInList |
---|
|
Rows: 1 |
WITH [1,3,9] AS sub, [3,5,1,7,6,2,8,4] AS list
RETURN all(x IN sub WHERE x IN list) AS subInList
This returns FALSE
because all elements in sub
are not part of list
.
subInList |
---|
|
Rows: 1 |