SET

1. Introduction

SET can be used with a map — provided as a literal, a parameter, or a node or relationship — to set properties.

Setting labels on a node is an idempotent operation — nothing will occur if an attempt is made to set a label on a node that already has that label. The query statistics will state whether any updates actually took place.

The examples use this graph as a starting point:

Diagram

2. Set a property

Use SET to set a property on a node or relationship:

Query
MATCH (n { name: 'Andy' })
SET n.surname = 'Taylor'
RETURN n.name, n.surname

The newly-changed node is returned by the query.

Table 1. Result
n.name n.surname

"Andy"

"Taylor"

1 row, Properties set: 1

It is possible to set a property on a node or relationship using more complex expressions. For instance, in contrast to specifying the node directly, the following query shows how to set a property for a node selected by an expression:

Query
MATCH (n { name: 'Andy' })
SET (
CASE
WHEN n.age = 36
THEN n END ).worksIn = 'Malmo'
RETURN n.name, n.worksIn
Table 2. Result
n.name n.worksIn

"Andy"

"Malmo"

1 row, Properties set: 1

No action will be taken if the node expression evaluates to null, as shown in this example:

Query
MATCH (n { name: 'Andy' })
SET (
CASE
WHEN n.age = 55
THEN n END ).worksIn = 'Malmo'
RETURN n.name, n.worksIn

As no node matches the CASE expression, the expression returns a null. As a consequence, no updates occur, and therefore no worksIn property is set.

Table 3. Result
n.name n.worksIn

"Andy"

<null>

1 row

3. Update a property

SET can be used to update a property on a node or relationship. This query forces a change of type in the age property:

Query
MATCH (n { name: 'Andy' })
SET n.age = toString(n.age)
RETURN n.name, n.age

The age property has been converted to the string '36'.

Table 4. Result
n.name n.age

"Andy"

"36"

1 row, Properties set: 1

4. Remove a property

Although REMOVE is normally used to remove a property, it’s sometimes convenient to do it using the SET command. A case in point is if the property is provided by a parameter.

Query
MATCH (n { name: 'Andy' })
SET n.name = NULL RETURN n.name, n.age

The name property is now missing.

Table 5. Result
n.name n.age

<null>

36

1 row, Properties set: 1

5. Copy properties between nodes and relationships

SET can be used to copy all properties from one node or relationship to another. This will remove all other properties on the node or relationship being copied to.

Query
MATCH (at { name: 'Andy' }),(pn { name: 'Peter' })
SET at = pn
RETURN at.name, at.age, at.hungry, pn.name, pn.age

The 'Andy' node has had all its properties replaced by the properties of the 'Peter' node.

Table 6. Result
at.name at.age at.hungry pn.name pn.age

"Peter"

34

<null>

"Peter"

34

1 row, Properties set: 3

6. Replace all properties using a map and =

The property replacement operator = can be used with SET to replace all existing properties on a node or relationship with those provided by a map:

Query
MATCH (p { name: 'Peter' })
SET p = { name: 'Peter Smith', position: 'Entrepreneur' }
RETURN p.name, p.age, p.position

This query updated the name property from Peter to Peter Smith, deleted the age property, and added the position property to the 'Peter' node.

Table 7. Result
p.name p.age p.position

"Peter Smith"

<null>

"Entrepreneur"

1 row, Properties set: 3

7. Remove all properties using an empty map and =

All existing properties can be removed from a node or relationship by using SET with = and an empty map as the right operand:

Query
MATCH (p { name: 'Peter' })
SET p = { }
RETURN p.name, p.age

This query removed all the existing properties — namely, name and age — from the 'Peter' node.

Table 8. Result
p.name p.age

<null>

<null>

1 row, Properties set: 2

8. Mutate specific properties using a map and +=

The property mutation operator += can be used with SET to mutate properties from a map in a fine-grained fashion:

  • Any properties in the map that are not on the node or relationship will be added.

  • Any properties not in the map that are on the node or relationship will be left as is.

  • Any properties that are in both the map and the node or relationship will be replaced in the node or relationship. However, if any property in the map is null, it will be removed from the node or relationship.

Query
MATCH (p { name: 'Peter' })
SET p += { age: 38, hungry: TRUE , position: 'Entrepreneur' }
RETURN p.name, p.age, p.hungry, p.position

This query left the name property unchanged, updated the age property from 34 to 38, and added the hungry and position properties to the 'Peter' node.

Table 9. Result
p.name p.age p.hungry p.position

"Peter"

38

true

"Entrepreneur"

1 row, Properties set: 3

In contrast to the property replacement operator =, providing an empty map as the right operand to += will not remove any existing properties from a node or relationship. In line with the semantics detailed above, passing in an empty map with += will have no effect:

Query
MATCH (p { name: 'Peter' })
SET p += { }
RETURN p.name, p.age
Table 10. Result
p.name p.age

"Peter"

34

1 row

9. Set multiple properties using one SET clause

Set multiple properties at once by separating them with a comma:

Query
MATCH (n { name: 'Andy' })
SET n.position = 'Developer', n.surname = 'Taylor'
Table 11. Result

(empty result)

0 rows, Properties set: 2

10. Set a property using a parameter

Use a parameter to set the value of a property:

Parameters
{
  "surname" : "Taylor"
}
Query
MATCH (n { name: 'Andy' })
SET n.surname = $surname
RETURN n.name, n.surname

A surname property has been added to the 'Andy' node.

Table 12. Result
n.name n.surname

"Andy"

"Taylor"

1 row, Properties set: 1

11. 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" : "Andy",
    "position" : "Developer"
  }
}
Query
MATCH (n { name: 'Andy' })
SET n = $props
RETURN n.name, n.position, n.age, n.hungry

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

Table 13. Result
n.name n.position n.age n.hungry

"Andy"

"Developer"

<null>

<null>

1 row, Properties set: 4

12. Set a label on a node

Use SET to set a label on a node:

Query
MATCH (n { name: 'Stefan' })
SET n:German
RETURN n.name, labels(n) AS labels

The newly-labeled node is returned by the query.

Table 14. Result
n.name labels

"Stefan"

["German"]

1 row, Labels added: 1

13. Set multiple labels on a node

Set multiple labels on a node with SET and use : to separate the different labels:

Query
MATCH (n { name: 'George' })
SET n:Swedish:Bossman
RETURN n.name, labels(n) AS labels

The newly-labeled node is returned by the query.

Table 15. Result
n.name labels

"George"

["Swedish","Bossman"]

1 row, Labels added: 2