6
votes

There is the situation in which i need to match any one of the label of node.

We can do it for relationship types like

(n)-[:KNOWS|LOVES]->(m)

Can we match node labels like this? eg.

MATCH (c:computer)<-[:belongs_to]-(comp:HP|IBM)
return comp

Currently I have tried this and it gives results, Is there any simpler way?

MATCH (c:computer)<-[:belongs_to]-(comp)
WHERE 'HP' IN labels(comp) OR  'IBM' IN labels(comp)
return comp
3
FWIW I think your third example is pretty simple, and a good approach. You might investigate using OPTIONAL MATCH (docs.neo4j.org/chunked/stable/query-optional-match.html) but the resulting query wouldn't be simpler than what you have.FrobberOfBits

3 Answers

7
votes

I Think

WHERE 'HP' IN labels(comp) OR  'IBM' IN labels(comp)

AND

WHERE comp:HP OR comp:IBM

Will work with same manner second one is simple to use

1
votes

This form of your last query is at least simpler to write and more easily understood:

MATCH (c:computer)<-[:belongs_to]-(comp)
WHERE comp:HP OR comp:IBM
return comp;
0
votes

Currently facing this same problem.

Since I have quite a few labels to match on (revealing a bit of a flaw in my architecture!) I found the following to solve this problem concisely:

MATCH (n:computer)
WHERE any(label in labels(n) WHERE label in ['HP', 'IBM'])
RETURN n