The Cypher documentation describes the behavior of the MERGE command very well:
MERGE either matches existing nodes and binds them, or it creates new data and binds that. It’s like a combination of MATCH and CREATE that additionally allows you to specify what happens if the data was matched or created.
Let's see an example:
MERGE (n:Label {id: 1})
ON CREATE SET n.timestamp_created = timestamp()
ON MATCH SET n.timestamp_last_update = timestamp()
Here, we are trying to access a node with Label and a single property id, with a value of 1. If such a node already exists in the graph, the subsequent operations will be performed using that node. This statement is then equivalent to a MATCH in that case. However, if the node with label Label and id=1 doesn't exist, then it will be created, hence the parallel with the CREATE statement.
The two other optional statements are also important:
- ON CREATE SET will be executed if and only if the node was not found...