Overview
The insert().overwrite().into()
clause allows for the overwriting of existing nodes or the insertion of new nodes and edges within a single schema in a graphset.
// Overwrites or inserts nodes
insert().overwrite().into(@<schema>).nodes([
{<property1>: <value1>, <property2>: <value2>, ...},
{<property1>: <value1>, <property2>: <value2>, ...},
...
])
// Inserts edges
insert().overwrite().into(@<schema>).edges([
{<property1>: <value1>, <property2>: <value2>, ...},
{<property1>: <value1>, <property2>: <value2>, ...},
...
])
Method |
Param |
Description | Optional |
---|---|---|---|
overwrite() |
/ | Specifies the overwriting feature; applicable only to nodes. | Yes |
into() |
<schema> |
Specifies the node or edge schema (e.g., @user ) |
No |
nodes() / edges() |
List of property specifications | Allows insertion of one or more nodes or edges into the specified schema, with each property specification wrapped in {} . |
No |
The overwritting feature applies only to nodes and takes effect when an existing _id
value is provided in the property specification. When a node is overwritten:
- Provided custom property values will replace current values, while the values of missing custom properties will be removed and set to
null
. - The
_uuid
and_id
values remain unchanged.
A new node is inserted when a new _id
value is provided, or when it is missing. Node _id
values will be generated by the system if not provided.
A new edge is always inserted when the properties _from
and _to
(or _from_uuid
and _to_uuid
) are provided to specify its source and destination nodes.
During insertion, each provided property will be assigned its given value, while any missing custom properties will default to null
. Node and edge _uuid
values are always generated by the system and cannot be manually assigned.
Example Graph
To create the graph, execute each of the following UQL queries sequentially in an empty graphset:
create().node_schema("user").edge_schema("follow")
create().node_property(@user, "name").node_property(@user, "age", int32).edge_property(@follow, "time", datetime)
insert().into(@user).nodes([{_id:"U001", name:"Jason", age:30}, {_id:"U002", name:"Tim"}, {_id:"U003", name:"Grace", age:25}, {_id:"U004", name:"Ted", age:26}])
insert().into(@follow).edges([{_from:"U004", _to:"U001", time:"2021-9-10"}, {_from:"U003", _to:"U001", time:"2020-3-12"}, {_from:"U004", _to:"U002", time:"2023-7-30"}])
Overwriting or Inserting Nodes
To overwrite or insert nodes into @user
:
insert().overwrite().into(@user).nodes([
{_id: "U001", name: "John"},
{_id: "U005", name: "Alice"},
{age: 12}
]) as n
return n{*}
- First node: The provided
_id
U001 already exists in the graph, so the corresponding node is overwritten. - Second node: The provided
_id
U005 is new, so a new node is inserted. - Third node:
_id
is not provided, so a new node is inserted.
Result: n
_id |
_uuid |
schema |
values |
---|---|---|---|
U001 | Sys-gen | user | {name: "John", age: null } |
U005 | Sys-gen | user | {name: "Alice", age: null } |
Sys-gen | Sys-gen | user | {name: null , age: 12} |
Inserting Edges
To insert two edges into @follow
:
insert().overwrite().into(@follow).edges([
{_from: "U004", _to: "U001", time: "2021-10-3"},
{_from: "U001", _to: "U002"}
])
This is equivalent to the following insert()
query without the overwrite()
method:
insert().into(@follow).edges([
{_from: "U004", _to: "U001", time: "2021-10-3"},
{_from: "U001", _to: "U002"}
])