13.3. Merge

13.3.1. Introduction
13.3.2. Merge single node with a label
13.3.3. Merge single node with properties
13.3.4. Merge single node specifying both label and property
13.3.5. Merge with ON CREATE
13.3.6. Merge with ON MATCH
13.3.7. Merge with ON CREATE and ON MATCH

13.3.1. Introduction

MERGE either matches existing nodes and binds them, or it creates new data and binds that. For example, you can search for a user node by id, and if the node does not already exist in the database, it will create it.

MERGE is not only used for single nodes - just like MATCH, if multiple nodes match, they will all be bound to the identifier.

The following graph is used for the examples below:

Figure 13.1. Graph


13.3.2. Merge single node with a label

Merging a single node with a given label.

Query. 

merge (robert:Critic)
RETURN robert, labels(robert)

Because there are no nodes labeled Critic in the database, a new node is created.

Result

robertlabels(robert)
1 row
Nodes created: 1
Labels added: 1

Node[9]{}

["Critic"]


13.3.3. Merge single node with properties

Merging a single node with properties where not all matches any existing node.

Query. 

merge (charlie {name:'Charlie Sheen', age:10})
RETURN charlie

A new node with the name Charlie Sheen will be created since not all properties matched the existing Charlie Sheen node.

Result

charlie
1 row
Nodes created: 1
Properties set: 2

Node[9]{age:10,name:"Charlie Sheen"}


13.3.4. Merge single node specifying both label and property

Merging a single node with both label and property matching an existing node.

Query. 

merge (michael:Person {name:'Michael Douglas'})
RETURN michael

Michael Douglas will be matched and returned

Result

michael
1 row

Node[7]{name:"Michael Douglas"}


13.3.5. Merge with ON CREATE

Merge a node and set properties if the node needs to be created.

Query. 

merge (keanu:Person {name:'Keanu Reeves'}) on
CREATE keanu
SET keanu.created = timestamp()
RETURN keanu

Creates the Keanu node, and sets a timestamp on creation time.

Result

keanu
1 row
Nodes created: 1
Properties set: 2
Labels added: 1

Node[9]{created:1366592642882,name:"Keanu Reeves"}


13.3.6. Merge with ON MATCH

Merging nodes and setting properties on found nodes.

Query. 

merge (person:Person) on
MATCH person
SET person.found = true
RETURN person

Finds all the :Person nodes, sets a property on them, and returns them.

Result

person
5 rows
Properties set: 5

Node[1]{name:"Oliver Stone",found:true}

Node[2]{name:"Charlie Sheen",found:true}

Node[3]{name:"Martin Sheen",found:true}

Node[6]{name:"Rob Reiner",found:true}

Node[7]{name:"Michael Douglas",found:true}


13.3.7. Merge with ON CREATE and ON MATCH

Merge a node and set properties if the node needs to be created.

Query. 

merge (keanu:Person {name:'Keanu Reeves'})
on
CREATE keanu
SET keanu.created = timestamp()
on
MATCH keanu
SET keanu.lastSeen = timestamp()
RETURN keanu

Creates the Keanu node, and sets a timestamp on creation time. If Keanu already existed, a different property would be set

Result

keanu
1 row
Nodes created: 1
Properties set: 2
Labels added: 1

Node[9]{created:1366592643559,name:"Keanu Reeves"}