FOREACH clause is used to update data within a list, whether components of a path, or result of aggregation.
Lists and paths are key concepts in Cypher.
To use them for updating data, you can use the
It allows you to do updating commands on elements in a path, or a list created by aggregation.
The variable context within the foreach parenthesis is separate from the one outside it.
This means that if you
CREATE a node variable within a
FOREACH, you will not be able to use it outside of the foreach statement, unless you match to find it.
FOREACH parentheses, you can do any of the updating commands —
If you want to execute an additional
MATCH for each element in a list then
UNWIND (see Section 3.3.6, “UNWIND”) would be a more appropriate command.
This query will set the property
marked to true on all nodes along a path.
MATCH p =(begin)-[*]->(END ) WHERE begin.name = 'A' AND END .name = 'D' FOREACH (n IN nodes(p)| SET n.marked = TRUE )
Nothing is returned from this query, but four properties are set.
+-------------------+ | No data returned. | +-------------------+ Properties set: 4