概述
插入更新操作向当前图集单一或批量地插入新的点、边数据,或更新已有点、边自定义属性的值。插入更新由upsert()
命令触发。
插入更新数据时,需首先指定schema,然后指定全部或部分属性的值,也可以不指定任何属性的值:
- 插入:未指定UID属性值,或指定的UID属性值在图集中不存在时,则插入新数据。此时,未指定的UID属性值由系统生成,未携带的自定义属性会被赋值为null。
- 更新:指定的UID属性值在图集中存在,且与指定的schema匹配时,则更新原数据。此时,未指定的自定义属性值保持不变。
- 不论是插入还更新,若指定的属性值与其数据类型不符,系统会使用该数据类型的默认值给该属性赋值。各数据类型的默认值如下:
- ""(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属性值在图集中已存在,但与指定的schema不匹配。
- 插入更新边时,未指定其起点和终点。
- 插入更新边时,指定的起点或终点在图集中不存在。
不建议在流式返回的算法语句之后组装插入更新语句。
语法
- 子句别名:支持,数据类型为NODE或EDGE
- 参数别名:所有参数均不支持定义别名
// 向当前图集的某个schema中插入或更新点数据
upsert().into(@<schema>)
.nodes([ //只插入更新一个点时可省略中括号
{<property1>:<value1>, <property2>:<value2>, ...},
{<property1>:<value1>, <property2>:<value2>, ...},
...
])
// 向当前图集的某个schema中插入或更新边数据,必须携带_from和_to(或_from_uuid和_to_uuid)
upsert().into(@<schema>)
.edges([ //只插入更新一个边时可省略中括号
{<property1>:<value1>, <property2>:<value2>, ...},
{<property1>:<value1>, <property2>:<value2>, ...},
...
])
示例
示例图集
在一个空图集中,依次运行以下各行语句创建示例图集:
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"}])
属性赋值
本例插入更新三个@account点。第一个不携带任何属性,插入该点时,它的_id
和_uuid
由系统自动生成,其余属性值为null;第二个点未携带的name属性的值保持不变;第三个点携带的name属性值类型错误,因此name属性被更新为默认值(空字符串):
upsert().into(@account).nodes([{}, {_id: "U001"}, {_id: "U002", name: [1,2,3]}]) as nodes
return nodes{*}
| _id | _uuid | name |
|------------------------|-------|--------|
| ULTIPA8000000000000001 | 5 | null |
| U001 | 1 | Jason |
| U002 | 2 | |
UID与Schema不匹配
本例指定的_id
为U003的点在图集中已存在,但其schema是@account而不是@card,因此插入更新失败:
upsert().into(@card).nodes({_id: "U003", name: "Jack"}) as nodes
return nodes{*}
The schema: card does not match with U003!
未指定边的起/终点
本例没有指定边的起点和终点,因此插入更新失败:
upsert().into(@follow).edges({_uuid: 1, time: "2021-08-31"}) as edges
return edges{*}
_from_uuid must > 0!