SQL - UPDATE
Update one or more records in the current database. Remember: OrientDB can work in schema-less mode, so you can create any field on-the-fly. Furthermore, the command also supports extensions to work on collections.
Syntax:
UPDATE <class>|CLUSTER:<cluster>|<recordID>
[SET|REMOVE <field-name> = <field-value>[,]*]|[CONTENT|MERGE <JSON>]
[UPSERT]
[RETURN <returning> [<returning-expression>]]
[WHERE <conditions>]
[LOCK default|record]
[LIMIT <max-records>] [TIMEOUT <timeout>]
-
SETDefines the fields to update. -
REMOVERemoves an item in collection and map fields. -
CONTENTReplaces the record content with a JSON document. -
MERGEMerges the record content with a JSON document. -
LOCKSpecifies how to lock the record between the load and update. You can use one of the following lock strategies:DEFAULTNo lock. Use in the event of concurrent updates, the MVCC throws an exception.RECORDLocks the record during the update.
-
UPSERTUpdates a record if it exists or inserts a new record if it doesn't. This avoids the need to execute two commands, (one for each condition, inserting and updating).UPSERTrequires aWHEREclause and a class target. There are further limitations onUPSERT, explained below. -
RETURNSpecifies an expression to return instead of the record and what to do with the result-set returned by the expression. The available return operators are:COUNTReturns the number of updated records. This is the default return operator.BEFOREReturns the records before the update.AFTERReturn the records after the update.
-
LIMITDefines the maximum number of records to update. -
TIMEOUTDefines the time you want to allow the update run before it times out.
NOTE: The Record ID must have a
#prefix. For instance,#12:3.
Examples:
-
Update to change the value of a field:
orientdb>
UPDATE Profile SET nick = 'Luca' WHERE nick IS NULLUpdated 2 record(s) in 0.008000 sec(s). -
Update to remove a field from all records:
orientdb>
UPDATE Profile REMOVE nick -
Update to remove a value from a collection, if you know the exact value that you want to remove:
Remove an element from a link list or set:
orientdb>
UPDATE Account REMOVE address = #12:0Remove an element from a list or set of strings:
orientdb>
UPDATE Account REMOVE addresses = 'Foo' -
Update to remove a value, filtering on value attributes.
Remove addresses based in the city of Rome:
orientdb>
UPDATE Account REMOVE addresses = addresses[city = 'Rome'] -
Update to remove a value, filtering based on position in the collection.
orientdb>
UPDATE Account REMOVE addresses = addresses[1]This remove the second element from a list, (position numbers start from
0, soaddresses[1]is the second elelment). -
Update to remove a value from a map
orientdb>
UPDATE Account REMOVE addresses = 'Luca' -
Update an embedded document. The
UPDATEcommand can take JSON as a value to update.orientdb>
UPDATE Account SET address={ "street": "Melrose Avenue", "city": { "name": "Beverly Hills" } } -
Update the first twenty records that satisfy a condition:
orientdb>
UPDATE Profile SET nick = 'Luca' WHERE nick IS NULL LIMIT 20 -
Update a record or insert if it doesn't already exist:
orientdb>
UPDATE Profile SET nick = 'Luca' UPSERT WHERE nick = 'Luca' -
Updates using the
RETURNkeyword:orientdb>
UPDATE ♯7:0 SET gender='male' RETURN AFTER @ridorientdb>UPDATE ♯7:0 SET gender='male' RETURN AFTER @versionorientdb>UPDATE ♯7:0 SET gender='male' RETURN AFTER @thisorientdb>UPDATE ♯7:0 SET gender='male' RETURN AFTER $current.exclude( "really_big_field")
In the event that a single field is returned, OrientDB wraps the result-set in a record storing the value in the field result. This avoids introducing a new serialization, as there is no primitive values collection serialization in the binary protocol. Additionally, it provides useful fields like version and rid from the original record in corresponding fields. The new syntax allows for optimization of client-server network traffic.
For more information on SQL syntax, see SELECT.
Limitations of the UPSERT Clause
The UPSERT clause only guarantees atomicity when you use a UNIQUE index and perform the look-up on the index through the WHERE condition.
orientdb> UPDATE Client SET id = 23 UPSERT WHERE id = 23
Here, you must have a unique index on Client.id to guarantee uniqueness on concurrent operations.