概述
更新操作根据过滤条件更新图集中已有点、边的自定义属性。更新由update()
命令触发。
插入覆盖数据时,需首先设置过滤条件,然后指定全部或部分属性的值:
- 未指定的自定义属性值保持不变。
- 若指定的属性值与其数据类型不符,系统会使用该数据类型的默认值给该属性赋值。各数据类型的默认值如下:
- ""(string,text)
- 0(int32,int64,uint32,uint64,float,double,decimal)
- "1970-01-01 08:00:00 +08:00"(timestamp)
- "0000-00-00 00:00:00"(datetime)
- [](list)
- POINT(0.00 0.00)(point)
常见的失败原因:
- 试图更新UID属性值。
- 更新边时,指定的新起点或新终点在图集中不存在。
更新操作会增加图集中的无效数据,从而影响数据查询效率。建议定期对图集进行碎片整理,详见碎片整理命令
compact()
。
不建议在流式返回的算法语句之后组装更新语句。
语法
- 子句别名:支持,数据类型为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属性的节点的name属性值:
update().nodes().set({name: "(none)"}) as nodes
return nodes{*}
| _id | _uuid | name |
|------|-------|--------|
| U001 | 1 | (none) |
| U002 | 2 | (none) |
| U003 | 3 | (none) |
| U004 | 4 | (none) |
限制数量
本例节点过滤条件为空,因此更新目标是所有点,但limit()
参数限制仅对最先找到的两个点进行操作:
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 |
禁止更新UID
本例尝试更新指定节点的_id
和_uuid
,得到错误提示:
update().nodes({_id == "U001"}).set({_id: "U010", _uuid: 10}) as nodes
return nodes{*}
update _id or _uuid is not allowed