Syntax to set a property on nodes of multiple types?

cypher

(Rsworden59) #1

Looking for a syntax like "IN [list]" that will work with multiple labels so I can set a property on every node of those types. Something like:

MATCH (n) WHERE n:Label IN [FIRST,SECOND...TENTH] SET n.prop = "true"
or
MATCH (n {Label IN [FIRST,SECOND...TENTH]}) SET n.prop = "true"

I can find IN syntax for properties, and I can see how to match a single node label, but I can't see how to match multiple labels. Not too bad to issue multiple commands when there are just one or two labels to affect, but when there is a long list...

Thanks.


(Andrew Bowman) #2

This is actually a tricky one. While you could manually match on nodes of each label (WHERE n:FIRST OR n:SECOND ...), there isn't a good way to do this using a list of labels, as dynamic label matches aren't generally supported with Cypher (labels usually need to be hardcoded so the Cypher planner can build a plan before query execution).

That said, you could use APOC Procedures for this, allowing you to build the queries you need based on the label list. Provided you pass in a $labels parameter which is a list of labels:

UNWIND $labels as label
CALL apoc.cypher.run('MATCH (n:' + label + ') RETURN n', {}) YIELD value
WITH value.n as n
SET n.prop = true

Make sure the labels passed in are sanitized first, if coming (directly or indirectly) from user input, to avoid Cypher injection.