3.13. SET

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

3.13.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:

Figure 3.12. Graph
alt

3.13.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 3.93. Result
n.name n.surname

1 row Properties set: 1

"Andy"

"Taylor"

Try this query live.  CREATE (a:Swedish {name: 'Andy', age: 36, hungry: true}), (b {name: 'Stefan'}), (c {name: 'Peter', age: 34}), (d {name: 'George'}), (a)-[:KNOWS]->(c), (b)-[:KNOWS]->(a), (d)-[:KNOWS]->(c) MATCH (n {name: 'Andy'}) SET n.surname = 'Taylor' RETURN n.name, n.surname

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 3.94. Result
n.name n.worksIn

1 row Properties set: 1

"Andy"

"Malmo"

Try this query live.  CREATE (a:Swedish {name: 'Andy', age: 36, hungry: true}), (b {name: 'Stefan'}), (c {name: 'Peter', age: 34}), (d {name: 'George'}), (a)-[:KNOWS]->(c), (b)-[:KNOWS]->(a), (d)-[:KNOWS]->(c) MATCH (n {name: 'Andy'}) SET (CASE WHEN n.age = 36 THEN n END).worksIn = 'Malmo' RETURN n.name, n.worksIn

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.95. Result
n.name n.worksIn

1 row

"Andy"

<null>

Try this query live.  CREATE (a:Swedish {name: 'Andy', age: 36, hungry: true}), (b {name: 'Stefan'}), (c {name: 'Peter', age: 34}), (d {name: 'George'}), (a)-[:KNOWS]->(c), (b)-[:KNOWS]->(a), (d)-[:KNOWS]->(c) MATCH (n {name: 'Andy'}) SET (CASE WHEN n.age = 55 THEN n END).worksIn = 'Malmo' RETURN n.name, n.worksIn

3.13.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 3.96. Result
n.name n.age

1 row Properties set: 1

"Andy"

"36"

Try this query live.  CREATE (a:Swedish {name: 'Andy', age: 36, hungry: true}), (b {name: 'Stefan'}), (c {name: 'Peter', age: 34}), (d {name: 'George'}), (a)-[:KNOWS]->(c), (b)-[:KNOWS]->(a), (d)-[:KNOWS]->(c) MATCH (n {name: 'Andy'}) SET n.age = toString(n.age) RETURN n.name, n.age

3.13.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 3.97. Result
n.name n.age

1 row Properties set: 1

<null>

36

Try this query live.  CREATE (a:Swedish {name: 'Andy', age: 36, hungry: true}), (b {name: 'Stefan'}), (c {name: 'Peter', age: 34}), (d {name: 'George'}), (a)-[:KNOWS]->(c), (b)-[:KNOWS]->(a), (d)-[:KNOWS]->(c) MATCH (n {name: 'Andy'}) SET n.name = null RETURN n.name, n.age

3.13.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 3.98. Result
at.name at.age at.hungry pn.name pn.age

1 row Properties set: 3

"Peter"

34

<null>

"Peter"

34

Try this query live.  CREATE (a:Swedish {name: 'Andy', age: 36, hungry: true}), (b {name: 'Stefan'}), (c {name: 'Peter', age: 34}), (d {name: 'George'}), (a)-[:KNOWS]->(c), (b)-[:KNOWS]->(a), (d)-[:KNOWS]->(c) MATCH (at {name: 'Andy'}), (pn {name: 'Peter'}) SET at = pn RETURN at.name, at.age, at.hungry, pn.name, pn.age

3.13.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 3.99. Result
p.name p.age p.position

1 row Properties set: 3

"Peter Smith"

<null>

"Entrepreneur"

Try this query live.  CREATE (a:Swedish {name: 'Andy', age: 36, hungry: true}), (b {name: 'Stefan'}), (c {name: 'Peter', age: 34}), (d {name: 'George'}), (a)-[:KNOWS]->(c), (b)-[:KNOWS]->(a), (d)-[:KNOWS]->(c) MATCH (p {name: 'Peter'}) SET p = {name: 'Peter Smith', position: 'Entrepreneur'} RETURN p.name, p.age, p.position

3.13.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 3.100. Result
p.name p.age

1 row Properties set: 2

<null>

<null>

Try this query live.  CREATE (a:Swedish {name: 'Andy', age: 36, hungry: true}), (b {name: 'Stefan'}), (c {name: 'Peter', age: 34}), (d {name: 'George'}), (a)-[:KNOWS]->(c), (b)-[:KNOWS]->(a), (d)-[:KNOWS]->(c) MATCH (p {name: 'Peter'}) SET p = {} RETURN p.name, p.age

3.13.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 3.101. Result
p.name p.age p.hungry p.position

1 row Properties set: 3

"Peter"

38

true

"Entrepreneur"

Try this query live.  CREATE (a:Swedish {name: 'Andy', age: 36, hungry: true}), (b {name: 'Stefan'}), (c {name: 'Peter', age: 34}), (d {name: 'George'}), (a)-[:KNOWS]->(c), (b)-[:KNOWS]->(a), (d)-[:KNOWS]->(c) MATCH (p {name: 'Peter'}) SET p += {age: 38, hungry: true, position: 'Entrepreneur'} RETURN p.name, p.age, p.hungry, p.position

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 3.102. Result
p.name p.age

1 row

"Peter"

34

Try this query live.  CREATE (a:Swedish {name: 'Andy', age: 36, hungry: true}), (b {name: 'Stefan'}), (c {name: 'Peter', age: 34}), (d {name: 'George'}), (a)-[:KNOWS]->(c), (b)-[:KNOWS]->(a), (d)-[:KNOWS]->(c) MATCH (p {name: 'Peter'}) SET p += {} RETURN p.name, p.age

3.13.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 3.103. Result

0 rows Properties set: 2

(empty result)

Try this query live.  CREATE (a:Swedish {name: 'Andy', age: 36, hungry: true}), (b {name: 'Stefan'}), (c {name: 'Peter', age: 34}), (d {name: 'George'}), (a)-[:KNOWS]->(c), (b)-[:KNOWS]->(a), (d)-[:KNOWS]->(c) MATCH (n {name: 'Andy'}) SET n.position = 'Developer', n.surname = 'Taylor'

3.13.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 3.104. Result
n.name n.surname

1 row Properties set: 1

"Andy"

"Taylor"

Try this query live.  CREATE (a:Swedish {name: 'Andy', age: 36, hungry: true}), (b {name: 'Stefan'}), (c {name: 'Peter', age: 34}), (d {name: 'George'}), (a)-[:KNOWS]->(c), (b)-[:KNOWS]->(a), (d)-[:KNOWS]->(c) MATCH (n {name: 'Andy'}) SET n.surname = $surname RETURN n.name, n.surname

3.13.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 3.105. Result
n.name n.position n.age n.hungry

1 row Properties set: 4

"Andy"

"Developer"

<null>

<null>

Try this query live.  CREATE (a:Swedish {name: 'Andy', age: 36, hungry: true}), (b {name: 'Stefan'}), (c {name: 'Peter', age: 34}), (d {name: 'George'}), (a)-[:KNOWS]->(c), (b)-[:KNOWS]->(a), (d)-[:KNOWS]->(c) MATCH (n {name: 'Andy'}) SET n = $props RETURN n.name, n.position, n.age, n.hungry

3.13.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 3.106. Result
n.name labels

1 row Labels added: 1

"Stefan"

["German"]

Try this query live.  CREATE (a:Swedish {name: 'Andy', age: 36, hungry: true}), (b {name: 'Stefan'}), (c {name: 'Peter', age: 34}), (d {name: 'George'}), (a)-[:KNOWS]->(c), (b)-[:KNOWS]->(a), (d)-[:KNOWS]->(c) MATCH (n {name: 'Stefan'}) SET n:German RETURN n.name, labels(n) AS labels

3.13.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 3.107. Result
n.name labels

1 row Labels added: 2

"George"

["Swedish","Bossman"]

Try this query live.  CREATE (a:Swedish {name: 'Andy', age: 36, hungry: true}), (b {name: 'Stefan'}), (c {name: 'Peter', age: 34}), (d {name: 'George'}), (a)-[:KNOWS]->(c), (b)-[:KNOWS]->(a), (d)-[:KNOWS]->(c) MATCH (n {name: 'George'}) SET n:Swedish:Bossman RETURN n.name, labels(n) AS labels