Comparing array values in with cypher / neo4j

Given two nodes, n and m, that look something like:

CREATE ({id: 1, color: ["red", "blue", "green", "yellow"]})
CREATE ({id: 2, color: ["red", "blue", "green", "white"]})

You can do something like this:

MATCH n, m
WHERE n.id = 1 AND m.id = 2
RETURN length(FILTER(x in n.color WHERE x in m.color))

The FILTER function iterates through the n.color array, binding the current value to x(arbitrarily chosen by me, could be different). The predicate (x in m.color) is checked for each x value, and if it evaluates to true, that element is pushed into the new array that FILTER returns. You can leave it at that to see the intersection of the two arrays (red, blue, and green in this case), or wrap it in the length function to see the number of colors shared between the two nodes (3 in this case).

Check out the full FILTER docs here: http://docs.neo4j.org/chunked/milestone/query-functions-collection.html#functions-filter

Tags:

Neo4J

Cypher