apoc.create.setProperty

Details

Syntax

apoc.create.setProperty(nodes, key, value) :: (node)

Description

Sets the given property to the given NODE values.

Input arguments

Name

Type

Description

nodes

ANY

The nodes to set a property on.

key

STRING

The name of the property key to set.

value

ANY

The value of the property to set.

Return arguments

Name

Type

Description

node

NODE

The updated node.

Set Properties using Cypher

Properties can be referenced dynamically in Cypher without using Cypher.

Cypher syntax for dynamically setting a property on a node or a relationship
SET n[key]

The dynamically calculated key must evaluate to a STRING value. For more information, see the Cypher Manual → Dynamically setting or updating a property.

Usage Examples

The examples in this section are based on the following sample graph:

CREATE (stop:Stop {arrival: "0802", departure: "0803"});

We want to convert the arrival and departure properties into Time types and store them as new properties, whose names are based on the original property keys.

We can generate the new property keys and Time values, by running the following query:

MATCH (stop:Stop)
UNWIND ["arrival", "departure"] AS key
RETURN key + "Time" AS newKey, time(stop[key]) AS time;
Results
newKey

time

"arrivalTime"

08:02Z

"departureTime"

08:03Z

Using apoc.create.setProperty:

MATCH (stop:Stop)
UNWIND ["arrival", "departure"] AS key
WITH stop, key + "Time" AS newKey, time(stop[key]) AS time
CALL apoc.create.setProperty(stop, newKey, time)
YIELD node
RETURN node;
Results
node

(:Stop {departure: "0803", arrival: "0802", arrivalTime: 08:02Z})

(:Stop {departureTime: 08:03Z, departure: "0803", arrival: "0802", arrivalTime: 08:02Z})

The Cypher equivalent to this is:

MATCH (stop:Stop)
UNWIND ["arrival", "departure"] AS key
WITH stop, key + "Time" AS newKey, time(stop[key]) AS time
SET stop[newKey] = time
RETURN stop