apoc.create.setRelProperty
Procedure APOC Core
apoc.create.setRelProperty( [rel,id,ids,rels], key, value) - sets the given property on the relationship(s)
Signature
apoc.create.setRelProperty(relationships :: ANY?, key :: STRING?, value :: ANY?) :: (rel :: RELATIONSHIP?)
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;
newKey |
---|
time |
"arrivalTime" |
08:02Z |
"departureTime" |
08:03Z |
But if we try to save these properties back to the database:
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;
We’ll receive the following error:
Invalid input '[': expected ":" (line 5, column 12 (offset: 160))
"SET journey[newKey] = time;"
^
We can use apoc.create.setRelProperty
to work around this problem, as shown in the query below:
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;
rel |
---|
[:JOURNEY {departure: "0803", arrival: "0802", arrivalTime: 08:02Z}] |
[:JOURNEY {departureTime: 08:03Z, departure: "0803", arrival: "0802", arrivalTime: 08:02Z}] |