插入更新操作是指向当前图集中单一/批量地插入新的点、边数据,或根据 ID 单一/批量的更新已有的点、边的除 ID 以外的属性,由 upsert()
命令触发。
upsert()
可能触发:
- 插入:未携带 ID,或携带的 ID 在图集中不存在
- 更新:携带的 ID 在图集中存在且与 schema 相匹配
- 失败:携带的 ID 在图集中存在但与 schema 不匹配;插入边时未携带 FROM、TO,或携带的 FROM、TO 在图集中不存在。
插入时,细节同 insert()
命令。
更新时:
- 未携带的属性保持不变
- 数据类型错误的属性,将使用默认值
不建议在算法流式返回之后进行插入更新操作,详见《Ultipa 图分析与图算法》-《算法的使用》-《执行方式》-流式返回
stream()
的相关说明。
语法:
- 语句别名:支持,结构为 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
;另两个点的 ID 与 schema 相匹配,其中一个未携带属性值将保留值不变,另一个携带了错误类型的属性值将被更新为默认值(空字符串)
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 | |
示例:更新点时,由于 ID 与 schema 不匹配而得到错误提示
upsert().into(@card).nodes({_id: "U003", name: "Jack"}) as nodes
return nodes{*}
The schema: card does not match with U003!
示例:更新边时,由于未提供 FROM/TO 而得到错误提示
upsert().into(@follow).edges({_uuid: 1, time: "2021-08-31"}) as edges
return edges{*}
_from_uuid must > 0!