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|INCREMENT|ADD|REMOVE|PUT <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.INCREMENTIncrements the field by the value.For instance, record at
10withINCREMENT value = 3sets the new value to13. You may find this useful in atomic updates of counters. Use negative numbers to decrement. Additionally, you can useINCREMENTto implement sequences and auto-increment.ADDAdds a new item in collection fields.REMOVERemoves an item in collection and map fields.PUTPuts an entry into a map field.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.
WHERELIMITDefines 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 nickUpdate to add a value into a collection:
orientdb>UPDATE Account ADD address=#12:0NOTE: Beginning with version 2.0.5, the OrientDB server generates a server error if there is no space between
#and the=. You must write the command as:orientdb>UPDATE Account ADD address = #12:0Update 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 put a map entry into the map:
orientdb>UPDATE Account PUT addresses = 'Luca', #12:0Update 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 20Update a record or insert if it doesn't already exist:
orientdb>UPDATE Profile SET nick = 'Luca' UPSERT WHERE nick = 'Luca'Update a web counter, avoiding concurrent accesses:
orientdb>UPDATE Counter INCREMENT views = 1 WHERE pages = '/downloads/' LOCK RECORDUpdates 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 INCREMENT Counter = 123 RETURN BEFORE $current.Counterorientdb>UPDATE ♯7:0 SET gender='male' RETURN AFTER $current.exclude( "really_big_field")orientdb>UPDATE ♯7:0 ADD out_Edge = ♯12:1 RETURN AFTER $current.outE("Edge")
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.