0
votes

Let's say we have nodes that has an array property.

Node 1 fruits = ['apple','mango']

Node 2 fruits = ['apple']

Node 3 fruits = ['tomato']

and we want to find all nodes wherein one of their fruits exists in Maria's basket.

Maria's basket = ['orange','grape','apple']

So our end result would be : Node 1 and Node 2.

My approach would be matching all nodes whose elements of its fruits array exists with Maria's basket. But I couldn't get it to work

match (n) where x in n.fruits in ['orange','grape','apple'] return n

I tried the query above and returns syntax error since x is not defined. How do we properly approach this problem?

The second approach I'm thinking is, match all nodes if there is a UNION that exists between a node's fruits and Maria's fruits.

1

1 Answers

1
votes

If you want to find nodes where exactly one fruit matches:

MATCH (n) 
WHERE single(x IN n.fruits WHERE x IN ['orange', 'grape', 'apple'])
RETURN n;

If you want to find nodes where >= 1 fruits match:

MATCH (n) 
WHERE any(x IN n.fruits WHERE x IN ['orange', 'grape', 'apple'])
RETURN n;

I wasn't sure which one you wanted based on your wording.