apoc.create.setRelProperty
语法 |
|
||
描述 |
在 `RELATIONSHIP` 值上设置给定属性。 |
||
输入参数 |
名称 |
类型 |
描述 |
|
|
要设置属性的关系。 |
|
|
|
要设置的属性键名称。 |
|
|
|
要设置的属性值。 |
|
返回参数 |
名称 |
类型 |
描述 |
|
|
更新后的关系。 |
|
使用 Cypher 设置属性
在 Cypher 中可以动态引用属性,无需使用 APOC。
在节点或关系上动态设置属性的 Cypher 语法
SET n[key]
动态计算的键必须求值为 `STRING` 类型。更多信息请参阅 Cypher 手册 → 动态设置或更新属性。
使用示例
本节示例基于以下示例图
CREATE (station1:Station {name: "Station 1"})
CREATE (station2:Station {name: "Station 3"})
CREATE (station1)-[:JOURNEY {arrival: "0802", departure: "0803"}]->(station2);
我们希望将 `arrival` 和 `departure` 属性转换为时间类型,并将其存储为新属性,新属性的名称基于原始属性键。
我们可以通过运行以下查询生成新的属性键和时间值
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 |
使用 `apoc.create.setRelProperty` 或仅使用 Cypher
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;
使用 Cypher 的动态属性
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
| rel |
|---|
[:JOURNEY {departure: "0803", arrival: "0802", arrivalTime: 08:02Z}] |
[:JOURNEY {departureTime: 08:03Z, departure: "0803", arrival: "0802", arrivalTime: 08:02Z}] |