Cypher. How do we get the intersection of multiple lists?

Viewed 32

I have the following code:

match (n) where apoc.coll.isEqualCollection(["Person","Actor","Old"], labels(n))
with distinct apoc.coll.sort(keys(n)) as keys

Now keys is a list of lists. I want the intersection of the items in the lists. Is there an apoc function for that? apoc.coll.intersection works only for two lists and it requires both arguments.

1 Answers

You can use apoc.coll.intersection function like this:

MATCH (n) WHERE apoc.coll.isEqualCollection(["Person","Actor","Old"], labels(n))
WITH collect(n) as nodesList
RETURN reduce(keyList = keys(nodesList[0]), n IN nodesList | apoc.coll.intersectiob(keyList, keys(n)))

Here, we first match and collect nodes in a list, then we reduce that list, to get the distinct keys, the thing to note here is we are providing the keys of the first node in the list as the initial value to the reduce function.

Related