UPDATE
There are two ways to modify documents in DQL:
UPDATE
When executing UPDATE to apply changes, only the minimum data necessary to enforce all peers converge on one view of the data sync across the mesh. (Updating)
INSERT
While INSERT operations modify all provided fields, even if they remain unchanged. (Inserting to Update)
Updating
To optimize performance and reduce unnecessary overhead, apply most updates in your app through the UPDATE method instead.
For the full DQL syntax, see Ditto Query Language > UPDATE.
Here is an example of a basic UPDATE operation on the cars collection:
await ditto.store.execute(`
UPDATE cars
SET color = 'blue'
WHERE _id = '123'`)Attachment Token
Once an ATTACHMENT object is created and linked to a document, it becomes immutable, meaning its contents cannot be changed.
However, you can indirectly update attachments by performing an UPDATE operation on the document to replace the existing attachment token with a different one. The attachment token is the reference pointer linking the document to the attachment's binary data stored separately.
Before you can access an attachment in your app, you must actively fetch it, unlike a document, which is always readily accessible. For instructions, see READ.
// Updating a Ditto Document with an attachment
// Update the document "123" with a new attachment
try await ditto.store.execute(
query: """
UPDATE COLLECTION cars (my_attachment ATTACHMENT)
SET my_attachment = :newAttachment
WHERE _id = "123"
""",
arguments: ["newAttachment": newAttachment]
)MAP Data Type
To add a MAP to a document, use the operator syntax -> (...), allowing you to edit multiple child fields within a single MAP:
UPDATE COLLECTION cars (properties MAP)
SET
properties -> (
color = 'red',
mileage = 3001
)
WHERE
_id = '123' Multiple Documents
Modify multiple documents simultaneously based on a specified condition.
For example, here is a snippet demonstrating the UPDATE operation modifying all documents in the cars collection that are currently red, and changing their color to blue.
After the update, you can reference the documents modified by the mutatedDocumentIDs method on the result of the update.
DittoQueryResult result = (DittoQueryResult) ditto.store.execute(
"UPDATE cars SET color = 'blue' WHERE color = 'red'",
new Continuation<>() {
@NonNull
@Override
public CoroutineContext getContext() {
return EmptyCoroutineContext.INSTANCE;
}
@Override
public void resumeWith(@NonNull Object o) {
if (o instanceof Result.Failure) {
// Handle failure
}
}
}
);
for (String id : result.mutatedDocumentIDs()) {
System.out.println(id);
}Inserting to Update
The INSERT operation provides conflict policy options to override default behavior if a document with the same ID already exists.
For the full DQL syntax, see Ditto Query Language > INSERT.
By using the ON ID CONFLICT DO UPDATE policy, inserted documents automatically apply updates for all provided fields.
To optimize performance and reduce unnecessary overhead, apply most data modifications in your app through the UPDATE method.
Map<String, String> document = new HashMap<>();
newCar.put("_id", "123");
newCar.put("color", "red");
DittoQueryResult result = (DittoQueryResult) ditto.store.execute(
"INSERT INTO cars DOCUMENTS (:document) ON ID CONFLICT DO UPDATE",
Collections.singletonMap("document", document),
new Continuation<>() {
@NonNull
@Override
public CoroutineContext getContext() {
return EmptyCoroutineContext.INSTANCE;
}
@Override
public void resumeWith(@NonNull Object o) {
if (o instanceof Result.Failure) {
// Handle failure
}
}
}
);MAP in a MAP
If you need to represent and organize highly complex data in a hierarchical structure, consider embedding a MAP within another MAP to establish a parent-child relationship within a document as follows:
Map<String, Object> nestedMap = new HashMap<>();
nestedMap.put("color", "blue");
Map<String, Object> topMap = new HashMap<>();
topMap.put("nestedMap", nestedMap);
Map<String, Object> newDocument = new HashMap<>();
newDocument.put("_id", "123");
newDocument.put("topMap", topMap);
DittoQueryResult result = (DittoQueryResult) ditto.store.execute(
"INSERT INTO COLLECTION your_collection_name (top_map MAP(nested_mapMAP)) DOCUMENTS (:newDocument)",
Collections.singletonMap("newDocument", newDocument),
new Continuation<>() {
@NonNull
@Override
public CoroutineContext getContext() {
return EmptyCoroutineContext.INSTANCE;
}
@Override
public void resumeWith(@NonNull Object o) {
if (o instanceof Result.Failure) {
// Handle failure
}
}
}
);