UPSERT
- reference
UPSERT is used to insert a new record or update an existing one. If the document doesn’t exist it will be created. UPSERT is a combination of INSERT and UPDATE.
Prerequisites
RBAC Privileges
User executing the UPSERT statement must have the Query Update and Query Insert privileges on the target keyspace. If the statement has any RETURNING clauses, then the Query Select privilege is also required on the keyspaces referred in the respective clauses. For more details about user roles, see Authorization.
Please note that the examples below will alter the data in your sample buckets.
To restore your sample data, remove and reinstall the travel-sample bucket.
Refer to Sample Buckets for details.
|
For example,
-
To execute the following statement, you must have the Query Update and Query Insert privileges on
`travel-sample`.inventory.hotel
.UPSERT INTO `travel-sample`.inventory.hotel (KEY, VALUE) VALUES ("key1", { "type" : "hotel", "name" : "new hotel" });
-
To execute the following statement, you must have the Query Update and Query Insert privileges on
`travel-sample`.inventory.hotel
and the Query Select privilege on`travel-sample`.inventory.hotel
also (for RETURNING clause).UPSERT INTO `travel-sample`.inventory.hotel (KEY, VALUE) VALUES ("key1", { "type" : "hotel", "name" : "new hotel" }) RETURNING *;
Result:[ { "hotel": { "name": "new hotel", "type": "hotel" } } ]
-
To execute the following statement, you must have the Query Update and Query Insert privileges on
`travel-sample`.inventory.landmark
and Query Select privilege on`beer-sample`
.UPSERT INTO `travel-sample`.inventory.landmark (KEY foo, VALUE bar) SELECT META(doc).id AS foo, doc AS bar FROM `beer-sample` AS doc WHERE type = "brewery";
A user with the Data Writer privilege may set documents to expire. When the document expires, the data service deletes the document, even though the user may not have the Query Delete privilege. |
Syntax
upsert ::= UPSERT INTO insert-target ( insert-values | insert-select ) [ returning-clause ]
Insert Target
insert-target ::= keyspace-ref [ [ AS ] alias ]
Specifies the keyspace into which to upsert documents.
Keyspace Reference
keyspace-ref ::= keyspace-path | keyspace-partial
keyspace-path ::= [ namespace ':' ] bucket [ '.' scope '.' collection ]
keyspace-partial ::= collection
Keyspace reference for the insert target. For more details, refer to Keyspace Reference.
AS Alias
Assigns another name to the keyspace reference. For details, refer to AS Clause.
- alias
-
String to assign an alias.
Assigning an alias to the keyspace reference is optional.
If you assign an alias to the keyspace reference, the AS
keyword may be omitted.
Insert Values
insert-values ::= [ "(" [ PRIMARY ] KEY "," VALUE [ "," OPTIONS ] ")" ] values-clause
Specifies one or more documents to be upserted using the VALUES clause. For details, refer to Insert Values.
VALUES Clause
values-clause ::= VALUES "(" key "," value [ "," options ] ")" [ "," [ VALUES ] "(" key "," value [ "," options ] ")" ]*
Specify the values as well-formed JSON. Also enables you to set the expiration of the upserted documents. For details, refer to VALUES Clause.
Insert Select
insert-select ::= "(" [ PRIMARY ] KEY key [ "," VALUE value ] [ "," OPTIONS options ] ")" select
Specifies the documents to be upserted as a SELECT statement. Also enables you to set the expiration of the upserted documents. For details, refer to Insert Select.
SELECT Statement
SELECT statements let you retrieve data from specified keyspaces. For details, refer to SELECT Syntax.
RETURNING Clause
returning-clause ::= RETURNING ( result-expr [ "," result-expr ]* | ( RAW | ELEMENT | VALUE ) expr )
Specifies the fields that must be returned as part of the results object.
Result Expression
result-expr ::= ( [ path "." ] "*" | expr [ [ AS ] alias ] )
Specifies an expression on the data you upserted, to be returned as output. For details, refer to Result Expression.
Example
The following statement upserts documents with type landmark-pub
into the landmark
keyspace.
UPSERT INTO `travel-sample`.inventory.landmark (KEY, VALUE)
VALUES ("upsert-1", { "name": "The Minster Inn", "type": "landmark-pub"}),
("upsert-2", {"name": "The Black Swan", "type": "landmark-pub"})
RETURNING VALUE name;
[
"The Minster Inn",
"The Black Swan"
]