Merge Processing

The actor’s name for Michael Caine is misspelled in the video. It has been corrected in the transcript.

Merge processing

You have learned that you can use MERGE to create nodes and relationships in the graph. MERGE operations work by first trying to find a pattern in the graph. If the pattern is found then the data already exists and is not created. If the pattern is not found, then the data can be created.

Customizing MERGE behavior

You can also specify behavior at runtime that enables you to set properties when the node is created or when the node is found. We can use the ON CREATE SET or ON MATCH SET conditions, or the SET keywords to set any additional properties.

In this example, if the Person node for McKenna Grace does not exist, it is created and the createdAt property is set. If the node is found, then the updatedAt property is set. In both cases, the born property is set.

Run this Cypher code at least 2 times to observe what properties are set. You can see the properties in table view.

// Find or create a person with this name
MERGE (p:Person {name: 'McKenna Grace'})

// Only set the `createdAt` property if the node is created during this query
ON CREATE SET p.createdAt = datetime()

// Only set the `updatedAt` property if the node was created previously
ON MATCH SET p.updatedAt = datetime()

// Set the `born` property regardless
SET p.born = 2006


If you want to set multiple properties for an ON CREATE SET or ON MATCH SET clause, you separate them by commas. For example:

ON CREATE SET m.released = 2020, m.tagline = `A great ride!'

Merging with relationships

You can use MERGE to create nodes or relationships:

Run this Cypher code:

// Find or create a person with this name
MERGE (p:Person {name: 'Michael Caine'})

// Find or create a movie with this title
MERGE (m:Movie {title: 'The Cider House Rules'})

// Find or create a relationship between the two nodes
MERGE (p)-[:ACTED_IN]->(m)

Another way your can create these nodes and relationship is as follows:

MERGE (p:Person {name: 'Michael Caine'})-[:ACTED_IN]->(m:Movie {title: 'The Cider House Rules'})

Here is what happens in the query processor:

  1. Neo4j will attempt to find a Person node with the name Michael Caine.

  2. If it does not exist, it creates the node.

  3. Then, it will attempt to expand the ACTED_IN relationships in the graph for this node.

  4. If there are any ACTED_IN relationships from this node, it looks for a Movie with the title 'The Cider House Rules'.

  5. If there is no node for the Movie, it creates the node.

  6. If there is no relationship between the two nodes, it then creates the ACTED_IN relationship between them.

Check your understanding

1. MERGE processing

Suppose we have no Person or Movie nodes in our graph. What does this Cypher code do?

MERGE (p:Person {name: 'Lucille Ball'})-[:ACTED_IN]->(m:Movie {title: 'Forever, Darling'})
  • ❏ It creates one relationship and no nodes.

  • ❏ It creates two nodes and no relationship.

  • ✓ It creates two nodes and one relationship.

  • ❏ It does not modify the graph.


This code will try to find the Person node for Lucile Ball. It will also try to find the Movie node for Forever Darling. The graph has neither of these nodes in it. It will continue by completing the pattern.


This code will try to find the Person node for Lucile Ball. Since this graph has no Person node for Lucile Ball, it will create it. It will also try to find the Movie node for Forever Darling. It will not find it and create the Movie node. It will then create the relationship between the two newly-created nodes.

2. Update an existing node in the graph

Our graph has a Person node for Lucille Ball. Suppose we want to add the year that Lucille Ball was born to this node. The Person node for Lucille Ball has only the name property set to Lucille Ball.

How can we update this code to include her birth year of 1911?

Use the dropdown below complete the code.

MERGE (p:Person {name: 'Lucille Ball'})
/*select:ON MATCH*/
SET p.born = 1911




Once you have selected your option, click the Check Results query button to continue.


Remember that MERGE first does a MATCH and if not found, it wll create the node. Since the Lucille Ball node already exists, what condition do we expect when MERGE executes?


ON MATCH is the correct answer. We want the property set when the Lucille Ball node is found in the graph.

You use ON CREATE for behavior to execute if the node is created.

There are no ON UPDATE ot ON FOUND clauses in Cypher.


In this lesson, you learned how you can perform additional setting of properties when you create or find a node in the graph. In the next challenge, you will demonstrate how to add or update properties when a node is created or found in the graph.


Hi, I am an Educational Learning Assistant for Intelligent Network Exploration. You can call me E.L.A.I.N.E.

How can I help you today?