V5 - Java Server DocumentationThese docs are for Java Server V5 Only. For all other languages and platforms, continue using SDK v4 until V5 support is available.
See Document Model for more information on documents in Ditto.
There are two ways to modify documents in DQL:
UPDATEWhen 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)INSERTWhile 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:
// try-with-resources block auto-closes DittoQueryResult
try (DittoQueryResult result = ditto.getStore().execute(
"UPDATE cars SET color = 'blue' WHERE _id = '123'"
).toCompletableFuture().join()) {
...
}
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.
try (DittoQueryResult result = ditto.getStore().execute((
"UPDATE cars SET color = 'blue' WHERE color = 'red'").toCompletableFuture().join()) {
for (DittoCborSerializable id : result.getMutatedDocumentIds()) {
System.out.println(id.asString());
}
}
Inserting with MERGE
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 MERGE 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.
import static com.ditto.java.serialization.DittoCborSerializable.buildDictionary;
var document = buildDictionary()
.put("_id", "123")
.put("color", "red")
.build();
try (DittoQueryResult result = ditto.getStore().execute(
"INSERT INTO cars VALUES (:document) ON ID CONFLICT DO MERGE",
buildDictionary().put("document", document).build()
).toCompletableFuture().join()) {
...
}
Nested MAPs
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.
In 4.11+ with DQL_STRICT_MODE=false, collection definitions are no longer required.
Read more about strict mode.
ALTER SYSTEM SET DQL_STRICT_MODE=false
Once Strict Mode is disabled, you can use the INSERT operation to add a new document with a nested map structure.
import static com.ditto.java.serialization.DittoCborSerializable.buildDictionary;
var nestedMap = buildDictionary()
.put("color", "blue")
.build();
var topMap = buildDictionary()
.put("nestedMap", nestedMap)
.build();
var newDocument = buildDictionary()
.put("_id", "123")
.put("topMap", topMap)
.build();
try (DittoQueryResult result = ditto.getStore().execute(
"INSERT INTO your_collection_name VALUES (:newDocument)",
buildDictionary().put("newDocument", newDocument).build()
).toCompletableFuture().join()) {
...
}