3.3.9. SET

The SET clause is used to update labels on nodes and properties on nodes and relationships.

SET can also be used with maps from parameters to set properties.

Setting labels on a node is an idempotent operations — if you try to set a label on a node that already has that label on it, nothing happens. The query statistics will tell you if something needed to be done or not.

The examples use this graph as a starting point:

alt

3.3.9.1. Set a property

To set a property on a node or relationship, use SET.

Query. 

MATCH (n { name: 'Andres' })
SET n.surname = 'Taylor'
RETURN n

The newly changed node is returned by the query.

Result. 

+------------------------------------------------------------+
| n                                                          |
+------------------------------------------------------------+
| Node[0]{surname:"Taylor",name:"Andres",age:36,hungry:true} |
+------------------------------------------------------------+
1 row
Properties set: 1

3.3.9.2. Remove a property

Normally you remove a property by using REMOVE, but it’s sometimes handy to do it using the SET command. One example is if the property comes from a parameter.

Query. 

MATCH (n { name: 'Andres' })
SET n.name = NULL RETURN n

The node is returned by the query, and the name property is now missing.

Result. 

+-----------------------------+
| n                           |
+-----------------------------+
| Node[0]{hungry:true,age:36} |
+-----------------------------+
1 row
Properties set: 1

3.3.9.3. Copying properties between nodes and relationships

You can also use SET to copy all properties from one graph element to another. Remember that doing this will remove all other properties on the receiving graph element.

Query. 

MATCH (at { name: 'Andres' }),(pn { name: 'Peter' })
SET at = pn
RETURN at, pn

The 'Andres' node has had all its properties replaced by the properties in the 'Peter' node.

Result. 

+-------------------------------------------------------------+
| at                           | pn                           |
+-------------------------------------------------------------+
| Node[0]{name:"Peter",age:34} | Node[2]{name:"Peter",age:34} |
+-------------------------------------------------------------+
1 row
Properties set: 3

3.3.9.4. Adding properties from maps

When setting properties from a map (literal, paremeter, or graph element), you can use the += form of SET to only add properties, and not remove any of the existing properties on the graph element.

Query. 

MATCH (peter { name: 'Peter' })
SET peter += { hungry: TRUE , position: 'Entrepreneur' }

Result. 

+-------------------+
| No data returned. |
+-------------------+
Properties set: 2

3.3.9.5. Set a property using a parameter

Use a parameter to give the value of a property.

Parameters. 

{
  "surname" : "Taylor"
}

Query. 

MATCH (n { name: 'Andres' })
SET n.surname = $surname
RETURN n

The 'Andres' node has got a surname added.

Result. 

+------------------------------------------------------------+
| n                                                          |
+------------------------------------------------------------+
| Node[0]{surname:"Taylor",name:"Andres",age:36,hungry:true} |
+------------------------------------------------------------+
1 row
Properties set: 1

3.3.9.6. Set all properties using a parameter

This will replace all existing properties on the node with the new set provided by the parameter.

Parameters. 

{
  "props" : {
    "name" : "Andres",
    "position" : "Developer"
  }
}

Query. 

MATCH (n { name: 'Andres' })
SET n = $props
RETURN n

The 'Andres' node has had all its properties replaced by the properties in the props parameter.

Result. 

+---------------------------------------------+
| n                                           |
+---------------------------------------------+
| Node[0]{name:"Andres",position:"Developer"} |
+---------------------------------------------+
1 row
Properties set: 4

3.3.9.7. Set multiple properties using one SET clause

If you want to set multiple properties in one go, simply separate them with a comma.

Query. 

MATCH (n { name: 'Andres' })
SET n.position = 'Developer', n.surname = 'Taylor'

Result. 

+-------------------+
| No data returned. |
+-------------------+
Properties set: 2

3.3.9.8. Set a label on a node

To set a label on a node, use SET.

Query. 

MATCH (n { name: 'Stefan' })
SET n :German
RETURN n

The newly labeled node is returned by the query.

Result. 

+------------------------+
| n                      |
+------------------------+
| Node[3]{name:"Stefan"} |
+------------------------+
1 row
Labels added: 1

3.3.9.9. Set multiple labels on a node

To set multiple labels on a node, use SET and separate the different labels using :.

Query. 

MATCH (n { name: 'Emil' })
SET n :Swedish:Bossman
RETURN n

The newly labeled node is returned by the query.

Result. 

+----------------------+
| n                    |
+----------------------+
| Node[1]{name:"Emil"} |
+----------------------+
1 row
Labels added: 2