Skip to main content
With an UPDATE statement, you can update specific fields within the documents based on specified conditions:
DQL
UPDATE Syntax Diagram In this syntax:
  • your_collection_name is the name of the collection in which you want to update data.
  • APPLY clause is used for counter operations (optional)
  • SET clause specifies fields to be updated and their corresponding new values (optional)
  • UNSET clause specifies fields to be deleted (optional)
  • WHERE clause filters which documents to update (optional)
  • ORDER BY clause controls the order in which documents are updated (optional)
  • LIMIT clause restricts the number of documents updated (optional)
  • OFFSET clause 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
SET Clause Syntax Diagram

UPDATE with Nested Fields

Dot notation is available in 4.11 and later, with DQL_STRICT_MODE=false. Read more
When updating fields nested in a MAP, 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 the deserialize_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 combine deserialize_json() with other SET assignments in the same UPDATE:

Deleting Fields

UNSET is available in 4.11 and later.
UNSET Clause Syntax Diagram In Ditto, fields need to be marked as โ€œdeletedโ€ for other peers to know the field has been removed.
  • When unsetting a MAP, all children data types are iteratively unset.
  • Fields that are unset are ignored during subsequent DQL statements.
  • Calling UNSET on 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 calling UNSET on a parent field (or deleting the document itself).

APPLY Clause for Counters

Counters are available in SDK 4.11+ (PN_COUNTER) and 4.13+ (COUNTER). See Counter Types for details.
The APPLY clause is used to perform counter operations on COUNTER or PN_COUNTER fields:
DQL
APPLY Clause Syntax Diagram

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 support ORDER 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:
  1. Documents are first filtered by the WHERE condition
  2. Results are then ordered by ORDER BY
  3. OFFSET skips documents from the ordered results
  4. LIMIT restricts how many documents are updated