CRUD Operations
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 > docid\ yexrv69kde e xykjr8mv here is an example of a basic update operation on the cars collection try await ditto store execute(""" update cars set color = 'blue' where id = '123' """);ditto store execute(""" update cars set color = 'blue' where id = '123' """)await ditto store execute(` update cars set color = 'blue' where id = '123'`)dittoqueryresult result = (dittoqueryresult) ditto store execute( "update cars set color = 'blue' where id = '123'", new continuation<>() { @nonnull @override public coroutinecontext getcontext() { return emptycoroutinecontext instance; } @override public void resumewith(@nonnull object o) { if (o instanceof result failure) { // handle failure } } } );await ditto store executeasync( "update cars set color = 'blue' where id = '123'");ditto get store() execute( "update cars set color = 'blue' where id = '123'") get();ditto store() execute( "update cars set color = 'blue' where id = '123'", none); 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 docid\ yswoinf8jdl zdf6ovbgw // 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] )// updating a ditto document with an attachment // fetch the document "123" with a new attachment val query = "select from collection cars where id = \ id" val arguments = mapof("id" to "123") // assuming the document id is "123" val selectqueryresult = runblocking { ditto store execute(query, arguments) } // get the existing document val existingdocument = selectqueryresult items firstornull() if (existingdocument != null) { // modify the document, including updating the attachment reference val updateddocument = existingdocument value tomutablemap() updateddocument\["my attachment"] = newattachment // assuming `newattachment` is the updated attachment // save the updated document back to the collection val updatequery = "update collection cars documents (\ document) where id = \ id" val updatearguments = mapof("document" to updateddocument, "id" to "123") // assuming the document id is "123" runblocking { ditto store execute(updatequery, updatearguments) } } else { println("document not found ") }// updating a ditto document with an attachment // update the document "123" with a new attachment await ditto store execute(` update collection cars (my attachment attachment) set my attachment = \ newattachment where id = "123"`, { newattachment })// updating a ditto document with an attachment // fetch the document "123" with a new attachment val query = "select from collection cars where id = \ id" val arguments = mapof("id" to "123") // assuming the document id is "123" val selectqueryresult = runblocking { ditto store execute(query, arguments) } // get the existing document val existingdocument = selectqueryresult items firstornull() if (existingdocument != null) { // modify the document, including updating the attachment reference val updateddocument = existingdocument value tomutablemap() updateddocument\["my attachment"] = newattachment // assuming `newattachment` is the updated attachment // save the updated document back to the collection val updatequery = "update collection cars documents (\ document) where id = \ id" val updatearguments = mapof("document" to updateddocument, "id" to "123") // assuming the document id is "123" runblocking { ditto store execute(updatequery, updatearguments) } } else { println("document not found ") }// updating a ditto document with an attachment // update the document "123" with a new attachment await ditto store executeasync( query @" update collection cars (my attachment attachment) set my attachment = \ newattachment where id = '123' ", arguments new dictionary\<string, object>() { { "newattachment", newattachment } } );// updating a ditto document with an attachment // (update the document "123" with a new attachment) peer a store() execute( r#" update collection cars (my attachment attachment) set my attachment = \ new attachment where id = "123" "#, some( json!({ "new attachment" new attachment, }) into(), ), ) await?; wait for id to be present in(peer b, "123") await?; map data type to add a map to a document, use the o perator syntax > ( ) , allowing you to edit multiple child fields within a single map dql 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 let result = try await ditto store execute( "update cars set color = 'blue' where id = '123'"); result mutateddocumentids foreach() { print($0) }var result = ditto store execute(""" update cars set color = 'blue' where id = '123' """) result mutateddocumentids() foreach { id > println(id) }const result = await ditto store execute(` update cars set color = 'blue' where color = 'red'`) console log(result mutateddocumentids())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); }const result = await ditto store executeasync( "update cars set color = 'blue' where color = 'red'"); result mutateddocumentids foreach(id => console writeline(id));ditto get store() execute( "update cars set color = 'blue' where color = 'red'") get();ditto store() execute( "update cars set color = 'blue' where color = 'red'", none); 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 > docid 0fqmubshimf8dwqp xpa3 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 var document = \[ " id" "123", "color" "red", ]; try await ditto store execute( query """ insert into cars documents (\ document) on id conflict do update """, arguments \[ "document" document ]);ditto store execute( "insert into cars documents (\ car)", mapof("car" to mapof( " id" to "123", "color" to "red" )))const document = { id "123", color "red", } await ditto store execute(` insert into cars documents (\ document) on id conflict do update`, { document })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 } } } );var args = new dictionary\<string, object> { "document", new { id = "123" , color = "red" } }; await ditto store executeasync( "insert into cars" \+ " documents (\ document) on id conflict do update", args);std map\<std string, std map\<std string, std string>> args; args\["document"] = {{" id", "123"},{"color", "red"}}; ditto get store() execute( "insert into cars documents (\ document) on id conflict do update", args) get();struct args { document car, } struct car { id string, color string } // let args = args { document car { id "123" to string(), color "red" to string() }, }; ditto store() execute( "insert into cars documents (\ document)", args); 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 let arguments \[string any] = \[ "newdocument" \[ " id" "123", "top map" \[ "nested map" \[ "color" "blue" ] ] } ]; await ditto store execute( query """ insert into collection your collection name (top map map(nested map map)) documents (\ newdocument) """, arguments arguments);val arguments = mapof( "newdocument" to mapof( " id" to "123", "top map" to mapof( "nested map" to mapof( "color" to "blue" ) ) ) ) ditto store execute(""" insert into collection your collection name (top map map(nested map map)) documents (\ newdocument) """, arguments)const newdocument = { id "123", top map { nested map { color "blue" } } } await ditto store execute(` insert into collection your collection name (top map map(nested map map)) documents (\ newdocument)`, { newdocument });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 } } } );var nestedmap = new { color = "blue" }; var topmap = new { nestedmap = nestedmap }; var args = new dictionary\<string, object>(); args add("newdocument", new { id = newid, topmap = topmap }) var result = await ditto store executeasync( "insert into collection your collection name (top map map(nested map map))" \+ " documents (\ newdocument)", args);struct nestedmap { std string color; }; struct topmap { nestedmap nestedmap; }; struct document { std string id; topmap topmap; }; // std map\<std string, document> args; args\["newdocument"] = {"123", topmap{nestedmap{"blue"}}}; ditto get store() execute( "insert into collection your collection name (properties map)" \+ " documents (\ newdocument)", args);struct args { newdocument document, } struct nestedmap { color string } struct topmap { nestedmap nestedmap } struct document { id string, topmap topmap } // let args = args { newdocument document { id "123" to string(), topmap topmap { nestedmap nestedmap { sobfield "blue" to string() } } }, }; await ditto store() execute(" "insert into collection your collection name (properties map)" \+ " documents (\ newdocument)", args);