概述
SET
语句用于更新或删除点、边的属性值。使用前,需先使用MATCH
语句获取待修改的点、边数据。
注意:点、边的Schema(标签)无法修改。它们的系统属性(_id
、_uuid
、_from
、_to
、_from_uuid
和_to_uuid
)也无法修改。
示例图

CREATE GRAPH myGraph ANY
INSERT (rowlock:User {_id: "U01", name: "rowlock"}),
(brainy:User {_id: "U02", name: "Brainy", gender: "male"}),
(purplechalk:User {_id: "U03", name: "purplechalk", gender: "female"}),
(mochaeach:User {_id: "U04", name: "mochaeach", gender: "female"}),
(c:Club {_id: "C01"}),
(rowlock)-[:Follows {createdOn: date("2024-01-05")}]->(brainy),
(purplechalk)-[:Follows {createdOn: date("2024-02-01")}]->(brainy),
(mochaeach)-[:Follows {createdOn: date("2024-02-10"}]->(brainy),
(brainy)-[:Joins {memberNo: 1}]->(c)
更新属性
获取点n
并更新其属性gender
,获取边e
并更新其属性createdOn
:
MATCH (n:User {name: 'rowlock'})-[e:Follows]->(:User {name: 'Brainy'})
SET n.gender = 'male', e.createdOn = '2024-01-07'
RETURN n.gender, e.createdOn
结果:
n.gender | e.createdOn |
---|---|
male | 2024-01-07 |
删除属性
获取点n
并删除属性gender
的值:
MATCH (n:User {name: 'mochaeach'})
SET n.gender = null
RETURN n
Result: n
_id | _uuid | schema | values |
---|---|---|---|
U04 | 1080867209103802371 | User | {name: "mochaeach", gender: null } |
覆盖所有属性
获取点n
并覆盖其全部属性,更新属性name
并删除所有其他属性值:
MATCH (n:User {name: 'purplechalk'})
SET n = {name: 'MasterSwift'}
RETURN n
结果:n
_id | _uuid | schema | values |
---|---|---|---|
U03 | 12393909473058488322 | User | {name: "MasterSwift", gender: null } |
删除所有属性
获取点n
并删除其全部属性值:
MATCH (n:User {name: 'rowlock'})
SET n = {}
RETURN n
结果:n
_id | _uuid | schema | values |
---|---|---|---|
U03 | 9223375335389659138 | User | {name: null , gender: null } |
使用CASE更新属性
根据Follows
边的createdOn
属性值,为其新属性weight
设置值:
ALTER EDGE Follows ADD PROPERTY {weight UINT32};
MATCH ()-[e:Follows]->()
SET e.weight = CASE
WHEN e.createdOn < '2024-01-31' THEN 10
ELSE 8
END
RETURN e
结果:e
_uuid |
_from |
_to |
_from_uuid |
_to_uuid |
schema |
values |
---|---|---|---|---|---|---|
1 | U01 | U02 | 9223375335389659138 | 4179343752734703618 | Follows | {createdOn: "2024-01-05", weight: 10} |
3 | U04 | U02 | 1080867209103802371 | 4179343752734703618 | Follows | {createdOn: "2024-02-10", weight: 8} |
2 | U03 | U02 | 12393909473058488322 | 4179343752734703618 | Follows | {createdOn: "2024-02-01", weight: 8} |
值类型不符
如果提供的值与属性值类型不同并且无法转换,将使用该属性值类型的默认值作为新值。
将边属性memberNo
(UINT64
类型)更新为一个字符串:
MATCH ()-[e:Joins]->()
SET e.memberNo = 'm2'
RETURN e
结果:e
_uuid |
_from |
_to |
_from_uuid |
_to_uuid |
schema |
values |
---|---|---|---|---|---|---|
4 | U02 | C01 | 4179343752734703618 | 4755803405526499332 | Follows | {memberNo: 0} |