apoc.create.setProperty

Procedure APOC Core

apoc.create.setProperty( [node,id,ids,nodes], key, value) - sets the given property on the node(s)

Signature

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

Input parameters

Name Type Default

nodes

ANY?

null

key

STRING?

null

value

ANY?

null

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})