cancel
Showing results for 
Search instead for 
Did you mean: 

Cypher query for subset

kaptenh
Node Clone

Hi,
I have to lists, say A and B. I want to check if all elements of B are contained in A, i.e if B is a subset of A..
More specifically I have a list of labels and I want to find nodes having all those labels, and they can possibly have more labels.
I have tried various of collect, all and so on and cant find a good way. Any suggestions?

Thanks

1 REPLY 1

You can use a list predicate to ensure a condition exists for all elements of a list:

WITH listA, listB
WHERE all(element in listB WHERE element in listA)

If you're starting from nodes and want to check this for node labels, then something like:

WITH nodeA, nodeB
WHERE all(label in labels(nodeB) WHERE label in labels(nodeA))
Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

On November 16 and 17 for 24 hours across all timezones, you’ll learn about best practices for beginners and experts alike.