apoc.create.setRelProperty

Details

Syntax

apoc.create.setRelProperty(rels, key, value) :: (rel)

Description

Sets the given property on the RELATIONSHIP values.

Input arguments

Name

Type

Description

rels

ANY

The relationships 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

rel

RELATIONSHIP

The updated relationship.

Set Properties using Cypher

Properties can be referenced dynamically in Cypher without using APOC.

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 (station1:Station {name: "Station 1"})
CREATE (station2:Station {name: "Station 3"})
CREATE (station1)-[:JOURNEY {arrival: "0802", departure: "0803"}]->(station2);

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 (:Station)-[journey:JOURNEY]->(:Station)
UNWIND ["arrival", "departure"] AS key
RETURN key + "Time" AS newKey, time(journey[key]) AS time;
Results
newKey time

"arrivalTime"

08:02Z

"departureTime"

08:03Z

Using apoc.create.setRelProperty:

MATCH (:Station)-[journey:JOURNEY]->(:Station)
UNWIND ["arrival", "departure"] AS key
WITH journey, key + "Time" AS newKey, time(journey[key]) AS time
CALL apoc.create.setRelProperty(journey, newKey, time)
YIELD rel
RETURN rel;
Results
rel

[:JOURNEY {departure: "0803", arrival: "0802", arrivalTime: 08:02Z}]

[:JOURNEY {departureTime: 08:03Z, departure: "0803", arrival: "0802", arrivalTime: 08:02Z}]

The Cypher equivalent to this is:

MATCH (:Station)-[journey:JOURNEY]->(:Station)
UNWIND ["arrival", "departure"] AS key
WITH stop, key + "Time" AS newKey, time(journey[key]) AS time
SET journey[newKey] = time
RETURN journey