更新操作是指根据过滤条件修改图集中已有的点、边的除 ID 以外的属性,由 update()
命令触发。
企图更新 ID、更新边时设置的 FROM、TO 在图集中不存在时,会导致操作失败。
更新时:
- 未设置的属性保持不变
- 数据类型错误的属性,将使用默认值
修改操作会增加图集中的无效数据,影响数据查询的效率,建议定期对图集进行碎片整理,详见《图集》中的
compact()
命令。
不建议在算法流式返回之后进行更新操作,详见《Ultipa 图分析与图算法》-《算法的使用》-《执行方式》-流式返回
stream()
的相关说明。
语法:
- 语句别名:支持,结构为 NODE 或 EDGE
// 修改当前图集中的 N 个点数据
update().nodes(<filter>)
.set({<property1> : <value1>, <property2> : <value2> ...})
.limit(<N>)
// 修改当前图集中的 N 个边数据
update().edges(<filter>)
.set({<property1> : <value1>, <property2> : <value2> ...})
.limit(<N>)
示例图集:(以下示例将在本图基础上运行)
在空图集中依次运行以下各行代码以创建该图数据:create().node_schema("account").edge_schema("follow")
create().node_property(@account, "name").edge_property(@follow, "time", datetime)
insert().into(@account).nodes([{_id:"U001", _uuid:1, name:"Jason"}, {_id:"U002", _uuid:2, name:"Tim"}, {_id:"U003", _uuid:3, name:"Grace"}, {_id:"U004", _uuid:4, name:"Ted"}])
insert().into(@follow).edges([{_uuid:1, _from_uuid:4, _to_uuid:1, time:"2021-09-10"}, {_uuid:2, _from_uuid:3, _to_uuid:2, time:"2020-03-12"}, {_uuid:3, _from_uuid:4, _to_uuid:2, time:"2023-07-30"}])
示例:将任意节点的 name 更新为 '(none)'
update().nodes().set({name: "(none)"}) as nodes
return nodes{*}
| _id | _uuid | name |
|------|-------|--------|
| U001 | 1 | (none) |
| U002 | 2 | (none) |
| U003 | 3 | (none) |
| U004 | 4 | (none) |
示例:将任意节点的 name 更新为 '(none)',仅对最先找到的两条数据进行操作
update().nodes().set({name: "(none)"}).limit(2) as nodes
return nodes{*}
| _id | _uuid | name |
|------|-------|--------|
| U001 | 1 | (none) |
| U002 | 2 | (none) |
示例:将 time 晚于 2020-01-01 的边的 time 延后一天
update().edges({time > "2020-01-01"}).set({time: dateAdd(time,1,"day")}) as edges
return edges{*}
| _uuid | _from | _to | _from_uuid | _to_uuid | time |
|-------|-------|-------|------------|----------|---------------------|
| 2 | U003 | U001 | 3 | 1 | 2020-03-13 00:00:00 |
| 3 | U004 | U002 | 4 | 2 | 2020-07-31 00:00:00 |
示例:尝试修改 ID 而得到错误提示
update().nodes({_id == "U001"}).set({_id: "U010", _uuid: 10}) as nodes
return nodes{*}
update _id or _uuid is not allowed