With anDocumentation Index
Fetch the complete documentation index at: https://docs.ditto.live/llms.txt
Use this file to discover all available pages before exploring further.
UPDATE statement, you can update specific fields within the documents based on specified conditions:
DQL
your_collection_nameis the name of the collection in which you want to update data.APPLYclause is used for counter operations (optional)SETclause specifies fields to be updated and their corresponding new values (optional)UNSETclause specifies fields to be deleted (optional)WHEREclause filters which documents to update (optional)ORDER BYclause controls the order in which documents are updated (optional)LIMITclause restricts the number of documents updated (optional)OFFSETclause skips a number of documents before updating (optional)
At least one of
APPLY, SET, or UNSET must be specified in an UPDATE statement.Basic UPDATE
Here is an example of a basic UPDATE operation:DQL
UPDATE Multiple Fields
The following snippet shows an example of using UPDATE to set multiple fields:DQL
UPDATE with Nested Fields
When updating fields nested in aMAP, specify the field-value pairs you want to update.
For MAP syntax, see Ditto Query Language > Types and Definitions > Map Operations.
UPDATE with deserialize_json
Starting with SDK 4.8, you can use thedeserialize_json() function in UPDATE statements to set fields from JSON-serialized strings. This is useful when you receive data as JSON strings (for example, from an API response) and want to update specific fields on existing documents.
Update a single field from a JSON string
You can deserialize a JSON string and use it to set a field value:Update multiple fields from a JSON string
You can also combinedeserialize_json() with other SET assignments in the same UPDATE:
Deleting Fields
- When unsetting a
MAP, all children data types are iteratively unset. - Fields that are unset are ignored during subsequent DQL statements.
- Calling
UNSETon a large number of dynamically generated fields (for example, dynamically created keys in a CRDT map) may cause performance to degrade due to metadata accumulation over time. Benchmarks for this will vary depending on your dataset size and query cardinality. You can mitigate this accumulation by callingUNSETon a parent field (or deleting the document itself).
APPLY Clause for Counters
TheAPPLY clause is used to perform counter operations on COUNTER or PN_COUNTER fields:
DQL
INCREMENT BY
Increment or decrement a counter (use negative values to decrement):DQL
RESTART (COUNTER only)
Reset a counter to zero or set it to a specific value:DQL
Combining APPLY with SET
You can combine counter operations with regular field updates:DQL
ORDER BY, LIMIT, and OFFSET
UPDATE statements supportORDER BY, LIMIT, and OFFSET clauses to control which documents are updated and in what order.
ORDER BY
Control the order in which documents are processed for update:DQL
LIMIT
Restrict the number of documents updated:DQL
OFFSET
Skip a number of documents before updating:DQL
Combining ORDER BY, LIMIT, and OFFSET
These clauses work together to provide fine-grained control:DQL
ORDER BY, LIMIT, and OFFSET are evaluated after the WHERE clause filters documents. This means:- Documents are first filtered by the WHERE condition
- Results are then ordered by ORDER BY
- OFFSET skips documents from the ordered results
- LIMIT restricts how many documents are updated