3.3.13. SET

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

3.3.13.1. Introduction

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:

Figure 3.17. Graph
alt

3.3.13.2. Set a property

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

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.168. 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

3.3.13.3. Remove a property

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

Query. 

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

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

Table 3.169. 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.3.13.4. Copying properties between nodes and relationships

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

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 in the 'Peter' node.

Table 3.170. 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.3.13.5. Adding properties from maps

When setting properties from a map (literal, parameter, 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 (p { name: 'Peter' })
SET p += { hungry: TRUE , position: 'Entrepreneur' }

Table 3.171. 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 (p {name: 'Peter'}) SET p += {hungry: true, position: 'Entrepreneur'}

3.3.13.6. Set a property using a parameter

Use a parameter to give the value of a property.

Parameters. 

{
  "surname" : "Taylor"
}

Query. 

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

The 'Andy' node has got a surname added.

Table 3.172. 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.3.13.7. 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.173. 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.3.13.8. 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: 'Andy' })
SET n.position = 'Developer', n.surname = 'Taylor'

Table 3.174. 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.3.13.9. 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.name, labels(n) AS labels

The newly labeled node is returned by the query.

Table 3.175. Result
n.name labels

1 row Labels added: 1

"Stefan"

JavaListWrapper(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.3.13.10. 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: 'George' })
SET n:Swedish:Bossman
RETURN n.name, labels(n) AS labels

The newly labeled node is returned by the query.

Table 3.176. Result
n.name labels

1 row Labels added: 2

"George"

JavaListWrapper(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