UltipaDocs
Try Playground
  • Introduction
  • Terminologies
    • Graphset
    • Schema
    • Property
    • Constraints
    • Insert
    • Overwrite or Insert
    • Upsert
    • Update
    • Delete
    • Find Nodes
    • Find Edges
      • AB
      • Autonet
      • Spread
      • Path Template
      • K-Hop
      • K-Hop Template
    • GROUP BY
    • ORDER BY
    • SKIP
    • LIMIT
    • WHERE
    • RETURN
    • WITH
    • UNCOLLECT
    • UNION
    • UNION ALL
    • CALL
    • All Functions
    • Path Functions
    • Aggregate Functions
    • Mathematical Functions
    • Trigonometric Functions
    • String Functions
    • List Functions
    • Datetime Functions
    • Spatial Functions
    • Table Functions
    • Null Functions
    • Type Conversion Functions
  • Operators
  • Expressions
    • Index
    • Full-text Index
    • Vector Index
    • Cache
    • Overview
    • Managing HDC Graphs
    • HDC Graph Queries
    • Process
    • Job
    • Execution Plan
    • Alias
    • Filter
    • Values and Types
    • Data Flow in Queries
    • Comments
    • Reserved Words
  • Access Control
  1. Docs
  2. /
  3. UQL
  4. /
  5. Data Modification

Overwrite or Insert

Overview

The insert().overwrite() statement either (1) overwrites existing nodes and edges, or (2) inserts new nodes and edges into a single schema in a graphset.

Syntax
// Overwrites or inserts nodes
insert().overwrite().into(@<schema>).nodes([
  {<property1?>: <value1?>, <property2?>: <value2?>, ...},
  {<property1?>: <value1?>, <property2?>: <value2?>, ...},
  ...
])

// Overwrites or inserts edges
insert().overwrite().into(@<schema>).edges([
  {_from: <fromValue>, _to: <toValue>, <property1?>: <value1?>, <property2?>: <value2?>, ...},
  {_from: <fromValue>, _to: <toValue>, <property1?>: <value1?>, <property2?>: <value2?>, ...},
  ...
])
Method
Param
Description
overwrite()/Enables overwriting. Edge overwriting is allowed only when an EDGE KEY constraint is defined.
into()<schema>Specifies the node or edge schema (e.g., @user).
nodes() or edges()List of property specificationsDefines the node(s) or edge(s) to be overwritten or inserted into the specified schema, with each property specification wrapped in {}.

Overwriting

A node is overwritten when an existing _id value is provided in the property specification. An edge is overwritten when an existing EDGE KEY constraint value is specified along with the corresponding _from and _to values.

When a node or edge is overwritten:

  • Provided custom property values replace the existing ones.
  • Missing custom properties are removed and set to null.
  • System properties remain unchanged.

Insertion

A new node is inserted when a new _id value is provided or when it is missing. A new edge is inserted when no overwriting (as described above) takes place. Note that edge properties _from and _to must be provided to specify the source and destination nodes of the edge.

When a node or edge is inserted:

  • Provided properties are assigned the given values.
  • Missing custom properties default to null.
  • Node _id is generated by the system if not provided. Node or edge _uuid is 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:

UQL
create().node_schema("user").edge_schema("follow")
create().node_property(@user, "name").node_property(@user, "age", int32).edge_property(@*, "time", datetime).edge_property(@*, "weight", int32).edge_property(@*, "flag")
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", weight:2, flag: "green"}, {_from:"U003", _to:"U001", time:"2020-3-12", weight:1, flag: "green"}, {_from:"U004", _to:"U002", time:"2023-7-30", weight:3, flag: "red"}])

Overwriting or Inserting Nodes

To overwrite or insert nodes into @user:

UQL
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
U001Sys-genuser{name: "John", age: null}
U005Sys-genuser{name: "Alice", age: null}
Sys-genSys-genuser{name: null, age: 12}

Overwriting or Inserting Edges

No EDGE KEY Constraint

When there is no EDGE KEY constraint defined, this query throws an error:

UQL
insert().overwrite().into(@follow).edges([
  {_from: "U004", _to: "U001", time: "2021-9-10"},
  {_from: "U001", _to: "U004", time: "2021-10-3", weight: 2}
])

Single-Property EDGE KEY Constraint

To create a single-property EDGE KEY constraint on the edge property time:

UQL
CREATE CONSTRAINT key_time
FOR ()-[e]-() REQUIRE e.time IS EDGE KEY
OPTIONS {
  type: {time: "datetime"}
}

To overwrite or insert edges into @follow:

UQL
insert().overwrite().into(@follow).edges([
  {_from: "U004", _to: "U001", time: "2021-9-10"},
  {_from: "U001", _to: "U004", time: "2021-10-3", weight: 2}
]) as e
return e{*}
  • First edge: The provided EDGE KEY constraint (time) with the value 2021-9-10 already exists in the graph, and both _from and _to match, so the corresponding edge is overwritten.
  • Second edge: The provided EDGE KEY constraint (time) with the value 2021-10-3 is new, so a new edge is inserted.

Result: e

_uuid
_from
_to
_from_uuid
_to_uuid
schema
values
Sys-genU004U001UUID of U004UUID of U001follow{time: "2021-09-10 00:00:00", weight: null, flag: null}
Sys-genU001U002UUID of U001UUID of U002follow{time: "2021-10-03 00:00:00", weight: 2, flag: null}

Composite EDGE KEY Constraint

To create a composite EDGE KEY constraint on the edge properties time and weight:

UQL
CREATE CONSTRAINT key_time_weight
FOR ()-[e]-() REQUIRE (e.time, e.weight) IS EDGE KEY
OPTIONS {
  type: {time: "datetime", weight: "int32"}
}

To overwrite or insert edges into @follow:

UQL
insert().overwrite().into(@follow).edges([
  {_from: "U004", _to: "U001", time: "2021-9-10", weight: 2},
  {_from: "U003", _to: "U001", time: "2020-3-12", weight: 2},
  {_from: "U001", _to: "U004", time: "2021-10-3", weight: 1, flag: "green"}
]) as e
return e{*}
  • First edge: The provided EDGE KEY constraint (time, weight) with the combined value (2021-9-10, 2) already exists in the graph, and both _from and _to match, so the corresponding edge is overwritten.
  • Second edge: The provided EDGE KEY constraint (time, weight) with the combined value (2020-3-12, 2) is new, so a new edge is inserted.
  • Third edge: The provided EDGE KEY constraint (time, weight) with the combined value (2021-10-3, 1) is new, so a new edge is inserted.

Result: e

_uuid
_from
_to
_from_uuid
_to_uuid
schema
values
Sys-genU004U001UUID of U004UUID of U001follow{time: "2021-09-10 00:00:00", weight: 2, flag: null}
Sys-genU003U001UUID of U003UUID of U001follow{time: "2020-03-12 00:00:00", weight: 2, flag: null}
Sys-genU001U004UUID of U001UUID of U004follow{time: "2021-10-03 00:00:00", weight: 1, flag: "green"}