Two problems with the original query:
- You have to have a collection to use
FOREACH
. - You bind
n
to a node id, and you can't set labels on node ids, only on nodes.
You can use FOREACH
to set labels by doing
MATCH ()-[:KNOWS]->(b)
WITH collect (distinct b) as bb
FOREACH (b IN bb | SET b:MyLabel)
In this case you don't need to do it as a collection, you can just do
MATCH ()-[:KNOWS]->(b)
WITH distinct b
SET b:MyLabel
And in general you can pipe results to an additional query part with WITH