UPSERT VERTEX¶
UPSERT VERTEX <vid> SET <update_columns> [WHEN <condition>] [YIELD <columns>]
vidis the ID of the vertex to be updated.update_columnsis the properties of the vertex to be updated. For example,tag1.col1 = $^.tag2.col2 + 1means to updatetag1.col1totag2.col2+1.NOTE:
$^indicates the vertex to be updated.
conditionis some constraints. Only when the conditions are met,UPSERTis executed successfully.conditionsupports expression operations.columnsis the columns to be returned,YIELDreturns the latest updated values.
UPSERT is a combination of UPDATE and INSERT. Use UPSERT VERTEX to update properties on a vertex if it exists or insert a new vertex if it does not exist. The UPDATE VERTEX statement only updates one tag of a vertex at a time.
The performance of UPSERT is much lower than that of INSERT, because UPSERT is a read-modify-write serialization operation at the partition level.
DON'T: DO NOT use
UPSERTfor scenarios with highly concurrent writes.
- If the vertex does not exist, a new vertex is created no matter whether the condition in the
WHENclause is met or not. The property columns not specified by theSETstatement use the default values of the columns. If there are no default values, an error is returned. - If the vertex exists and the
WHENcondition is met, the vertex is updated. - If the vertex exists and the
WHENcondition is not met, Nebula Graph does nothing.
Consider the following example:
nebula> INSERT VERTEX player(name, age) VALUES "player111":("Ben Simmons", 22); -- Insert a new vertex.
nebula> UPSERT VERTEX "player111" SET player.name = "Dwight Howard", player.age = $^.player.age + 11 WHEN $^.player.name == "Ben Simmons" AND $^.player.age > 20 YIELD $^.player.name AS Name, $^.player.age AS Age; -- Do an upsert operation on the vertex.
+-----------------+-----+
| Name | Age |
+-----------------+-----+
| "Dwight Howard" | 33 |
+-----------------+-----+
// An empty set is returned. Because vertex "player123" does not exist.
nebula> FETCH PROP ON * "player123";
Empty set (Time spent: 3.069/4.382 ms)
nebula> UPSERT VERTEX "player123" SET player.age = $^.player.age + 1;
If the vertex "player123" does not exist and the default value of age is NULL, the player.age of vertex "player123" is NULL. If player.age has a default value, the player.age of vertex "player123" is the default value plus one.
nebula> CREATE TAG person(followers int, age int DEFAULT 0); -- Create example tag person
nebula> UPSERT VERTEX "300" SET person.followers = $^.person.age + 1, person.age = 8; -- the number of followers is 1, age is 8
nebula> UPSERT VERTEX "300" SET person.age = 8, person.followers = $^.person.age + 1; -- the number of followers is 9, age is 8
Last update: February 4, 2021