apoc.create.setProperty

Procedure

apoc.create.setProperty(nodes ANY, key STRING, value ANY) - sets the given property to the given NODE values.

Signature

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

Input parameters

Name Type Default Description

nodes

ANY

null

nodes can be of type STRING (elementId()), INTEGER (id()), NODE or LIST<STRING | INTEGER | NODE>.

key

STRING

null

The property key to add to the given NODE values.

value

ANY

null

The value to add to the property.

Output parameters

Name Type

node

NODE

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;
Table 1. Results
newKey

time

"arrivalTime"

08:02Z

"departureTime"

08:03Z

But if we try to save these properties back to the database:

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

We’ll receive the following error:

Table 2. Results

Invalid input '[': expected an identifier character, whitespace, '{', node labels, a property map, a relationship pattern, '.', '(', '=' or "+=" (line 5, column 9 (offset: 119)) "SET stop[newKey] = time;"

We can use apoc.create.setProperty to work around this problem, as shown in the query below:

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;
Table 3. Results
node

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

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