UNWIND and MERGE — Iterating and Upserting
Two clauses that don't fit neatly into read or write:
UNWINDturns a list into rows, one per element.MERGEfinds a pattern in the graph, or creates it if missing (the Cypher upsert).
Overview
| Goal | Clause |
|---|---|
| Turn a list into rows | UNWIND |
| Bulk-load from a parameter | UNWIND $rows AS row CREATE … |
| Upsert a node | MERGE (n:L {key: value}) |
| Different side-effect on insert vs update | ON CREATE / ON MATCH |
| Ensure a relationship exists | MERGE (a)-[:R]->(b) |
UNWIND
UNWIND takes a list and emits
one row per element.
UNWIND [1, 2, 3] AS n RETURN n
;// 1, 2, 3
UNWIND list.range(1, 5) AS n RETURN n
;// 1, 2, 3, 4, 5
UNWIND [] AS n RETURN n
// zero rows