11.4. Set

11.4.1. Set a property
11.4.2. Remove a property
11.4.3. Copying properties between nodes and relationships
11.4.4. Set a property using a parameter
11.4.5. Set all properties using a parameter
11.4.6. Set a label on a node
11.4.7. Set multiple labels on a node

Updating labels on nodes and properties on nodes and relationships is done with the SET clause. SET can also be used with maps from parameters to set properties.

[Note]Note

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:

cypher-set-graph.svg

11.4.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
1 row
Properties set: 1

Node[3]{name:"Andres",age:36,awesome:true,surname:"Taylor"}


Try this query live. create (_0 {`name`:"Emil"}) create (_1 {`name`:"Stefan"}) create (_2 {`age`:34, `name`:"Peter"}) create (_3:`Swedish` {`age`:36, `awesome`:true, `name`:"Andres"}) create _0-[:`KNOWS`]->_2 create _1-[:`KNOWS`]->_3 create _3-[:`KNOWS`]->_2 match (n {name: 'Andres'}) set n.surname = 'Taylor' return n

11.4.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
1 row
Properties set: 1

Node[3]{age:36,awesome:true}


Try this query live. create (_0 {`name`:"Emil"}) create (_1 {`name`:"Stefan"}) create (_2 {`age`:34, `name`:"Peter"}) create (_3:`Swedish` {`age`:36, `awesome`:true, `name`:"Andres"}) create _0-[:`KNOWS`]->_2 create _1-[:`KNOWS`]->_3 create _3-[:`KNOWS`]->_2 match (n {name: 'Andres'}) set n.name = null return n

11.4.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 it’s properties replaced by the properties in the Peter node.

Result

atpn
1 row
Properties set: 3

Node[3]{age:34,name:"Peter"}

Node[2]{name:"Peter",age:34}


Try this query live. create (_0 {`name`:"Emil"}) create (_1 {`name`:"Stefan"}) create (_2 {`age`:34, `name`:"Peter"}) create (_3:`Swedish` {`age`:36, `awesome`:true, `name`:"Andres"}) create _0-[:`KNOWS`]->_2 create _1-[:`KNOWS`]->_3 create _3-[:`KNOWS`]->_2 match (at {name: 'Andres'}), (pn {name: 'Peter'}) set at = pn return at, pn

11.4.4. 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 an surname added.

Result

n
1 row
Properties set: 1

Node[3]{name:"Andres",age:36,awesome:true,surname:"Taylor"}


11.4.5. 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 it’s properties replaced by the properties in the props parameter.

Result

n
1 row
Properties set: 4

Node[3]{position:"Developer",name:"Andres"}


11.4.6. 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
1 row
Labels added: 1

Node[1]{name:"Stefan"}


Try this query live. create (_0 {`name`:"Emil"}) create (_1 {`name`:"Stefan"}) create (_2 {`age`:34, `name`:"Peter"}) create (_3:`Swedish` {`age`:36, `awesome`:true, `name`:"Andres"}) create _0-[:`KNOWS`]->_2 create _1-[:`KNOWS`]->_3 create _3-[:`KNOWS`]->_2 match (n {name: 'Stefan'}) set n :German return n

11.4.7. 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
1 row
Labels added: 2

Node[0]{name:"Emil"}


Try this query live. create (_0 {`name`:"Emil"}) create (_1 {`name`:"Stefan"}) create (_2 {`age`:34, `name`:"Peter"}) create (_3:`Swedish` {`age`:36, `awesome`:true, `name`:"Andres"}) create _0-[:`KNOWS`]->_2 create _1-[:`KNOWS`]->_3 create _3-[:`KNOWS`]->_2 match (n {name: 'Emil'}) set n :Swedish:Bossman return n