INSERT
when using dql's \<font color="#db2777">insert\</font> command, you can add new documents using json objects dql insert into your collection name documents (\[document1]),(\[document2]), (\[document3]), \[on id conflict \[fail | do nothing | do update]] \<font color="#db2777">insert into\</font> is the name of the collection from which you want to retrieve the data documents (\[document1]), (\[document2]), (\[document3]), represent the documents being inserted \[ on id conflict \[do fail | do nothing | do update (\[policy])]] is an optional clause that allows for defining a policy if the id already exists in the local data store the default is to throw an error ( fail ) in ditto, excluding fields from your payload doesn't remove the existing data from the system to remove a specific field from a document, use an explicit \<font color="#db2777">update\</font> statement and tombstone that field (see update docid vxfd7 jjfftzbyivqoxh ) insert document await ditto store execute( query "insert into cars documents (\ newcar)", arguments \["newcar" \["color" "blue"]]);ditto store execute( "insert into cars documents (\ newcar)", mapof("newcar" to mapof("color" to "blue")))await ditto store execute( "insert into cars documents (\ newcar)", { newcar { color "blue" } } );dittoqueryresult result = (dittoqueryresult) ditto store execute( "insert into cars documents (\ newcar)", collections singletonmap("newcar", collections singletonmap("color", "blue")), );var args = new dictionary\<string, object>(); args add("newcar", new { color = "blue" }); await ditto store executeasync( "insert into cars documents (\ newcar)", args);std map\<std string, std map\<std string, std string>> args; args\["newcar"] = {{"color", "blue"}}; ditto get store() execute( "insert into cars documents (\ newcar)", args);let query result = ditto store() execute( "insert into cars documents (\ newcar)", some(serde json json!({ "newcar" { "color" "blue" } }) into()), ) await?;await ditto store execute( "insert into cars documents (\ newcar)", arguments {"newcar" {"color" "blue"}}, ); insert with multiple documents await ditto store execute( query "insert into cars documents (\ car1), (\ car2)", arguments \[ "car1" \["color" "blue"], "car2" \["color" "red"] ]);ditto store execute( "insert into cars documents (\ car1),(\ car2)", mapof( "car1" to mapof("color" to "blue"), "car2" to mapof("color" to "red") ))await ditto store execute( "insert into cars documents (\ car1),(\ car2)", { car1 { color 'blue' }, car2 { color 'red' } });map\<string, map\<string, string>> args = new hashmap<>(); args put("car1", collections singletonmap("color", "blue")); args put("car2", collections singletonmap("color", "red")); dittoqueryresult result = (dittoqueryresult) ditto store execute( "insert into cars documents (\ car1),(\ car2)", args, 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>(); args add("car1", new { color = "blue" }); args add("car2", new { color = "red" }); await ditto store executeasync( "insert into cars documents (\ car1),(\ car2)", args);std map\<std string, std map\<std string, std string>> args; args\["car1"] = {{"color", "blue"}}; args\["car2"] = {{"color", "red"}}; auto result = ditto get store() execute( "insert into cars documents (\ car1),(\ car2)", args) get();let query result = ditto store() execute( "insert into cars documents (\ car1), (\ car2)", some(serde json json!({ "car1" { "color" "blue" }, "car2" { "color" "red" } }) into()), ) await?;await ditto store execute( "insert into cars documents (\ car1),(\ car2)", arguments { "car1" { "color" "blue" }, "car2" { "color" "red" }, }, ); insert document with map type for full map syntax, see map docid\ nl2t9nozi 1ajwdjnrmmu let arguments \[string any] = \[ "newcar" \[ " id" "123", "properties" \[ "color" "blue", "mileage" 3000 ] } ]; await ditto store execute( query "insert into collection cars (properties map) documents (\ newcar)", arguments arguments);val arguments = mapof( "newcar" to mapof( " id" to "123", "properties" to mapof( "color" to "blue", "mileage" to 3000 ) ) ) ) ditto store execute(""" insert into collection cars (properties map) documents (\ newcar) """, arguments)const newcar = { id "123", properties { color 'blue', mileage 3000 } } await ditto store execute(` insert into collection cars (properties map) documents (\ newcar)`, { newcar });map\<string, object> properties = new hashmap<>(); properties put("color", "blue"); properties put("mileage", 3000); map\<string, object> newcar = new hashmap<>(); newcar put(" id", "123"); newcar put("properties", properties); dittoqueryresult result = (dittoqueryresult) ditto store execute( "insert into collection cars (properties map) documents (\ newcar)", collections singletonmap("newcar", newcar), 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 submap = new { color = "blue", mileage = 3000 }; var insertargs = new dictionary\<string, object>(); insertargs add("newcar", new { id = newid, properties = submap }) var result = await ditto store executeasync( "insert into collection cars (properties map) documents (\ newcar)", insertargs);struct properties { std string color; int mileage; }; struct car { std string id; properties properties; }; // std map\<std string, car> args; args\["newcar"] = {"123", {"blue", 3000}}; ditto get store() execute( "insert into collection your collection name (properties map)" \+ " documents (\ newcar)", args) get();let query result = ditto store() execute( "insert into collection cars (properties map) documents (\ newcar)", some(serde json json!({ "newcar" { " id" "123", "properties" { "color" "blue", "mileage" 3000 } } }) into()), ) await?;final newcar = { " id" "123", "properties" { "color" 'blue', "mileage" 3000, }, }; await ditto store execute(""" insert into collection cars (properties map) documents (\ newcar)""", arguments {"newcar" newcar}, ); insert json serialized document from sdk 4 8, you can insert json serialized documents into ditto directly using the deserialize json() function await ditto store execute( query """ insert into cars documents (deserialize json(\ jsondata)) on id conflict do update """, arguments \[ "jsondata" "{\\" id\\" \\"123\\",\\"color\\" \\"blue\\"}" ])ditto store execute(""" insert into cars documents (deserialize json(\ jsondata)) """, mapof("jsondata", "{\\" id\\" \\"123\\",\\"color\\" \\"blue\\"}"))await ditto store execute(` insert into cars documents (deserialize json(\ jsondata))`, { jsondata "{\\" id\\" \\"123\\",\\"color\\" \\"blue\\"}" });dittoqueryresult result = (dittoqueryresult) ditto store execute( "insert into cars documents (deserialize json(\ jsondata))", collections singletonmap("jsondata", "{\\" id\\" \\"123\\",\\"color\\" \\"blue\\"}"), );var args = new dictionary\<string, string>(); args add("jsondata", "{\\" id\\" \\"123\\",\\"color\\" \\"blue\\"}") var result = await ditto store executeasync( "insert into cars" \+ " documents (deserialize json(\ jsondata))", args);std map\<std string, string> args; args\["jsondata"] = "{\\" id\\" \\"123\\",\\"color\\" \\"blue\\"}"; auto result = ditto get store() execute( "insert into cars documents (deserialize json(\ jsondata))", args) get();let json string = r#"{" id" "123", "color" "blue"}"# to string(); let query result = ditto store() execute( "insert into cars documents (deserialize json(\ jsondata))", some(serde json json!({ "jsondata" "{\\" id\\" \\"123\\",\\"color\\" \\"blue\\"}" }) into()), ) await; final newcar = '{" id" "123", "color" "blue"}'; await ditto execute(""" insert into cars documents (deserialize json(\ jsondata))""", queryargs {"jsondata" "{\\" id\\" \\"123\\",\\"color\\" \\"blue\\"}"}, ); insert with id conflict handling by default, the \<font color="#db2777">insert\</font> operation throws an error if an existing document with the same id exists in the local ditto store however, ditto allows some flexibility by allowing you to choose between ignoring the conflict ( do nothing ) or updating existing documents ( do update ) when a conflict occurs during an \<font color="#db2777">insert\</font> operation dql on id conflict \[do fail | do nothing | do update] in this syntax do fail (default) will cause an error to be thrown if a document with the same id currently exists in the local data store do nothing will make the statement succeed with no action taken do update will perform a value update on every field in the provided document, even if the value is the same (this will result in all fields provided being replicated) for example, inserting or updating a car — if there is a conflict ( \<font color="#db2777">on id conflict)\</font> , execute the \<font color="#db2777">do update\</font> conflict resolution policy let newcar = \[ " id" "123", "color" "blue" ] await ditto store execute( query """ insert into cars documents (\ newcar) on id conflict do update """, arguments \[ "newcar" newcar ])var newcar = mapof( " id" to "123", "color" to "blue" ) ditto store execute(""" insert into cars documents (\ newcar) on id conflict do update """, mapof("newcar", newcar))const newcar = { id "123", color "blue", }; await ditto store execute(` insert into cars documents (\ newcar) on id conflict do update`, { newcar });dittoqueryresult result = (dittoqueryresult) ditto store execute( "insert into cars documents (\ newcar) on id conflict do update", collections singletonmap("newcar", collections singletonmap("color", "blue")), );var args = new dictionary\<string, object>(); args add("newcar", new { id = "123", color = "blue" }) var result = await ditto store executeasync( "insert into cars" \+ " documents (\ newcar) on id conflict do update", args);struct car { std string id; std string color; }; // std map\<std string, car> args; args\["newcar"] = {"123", "blue"}; auto result = ditto get store() execute( "insert into cars documents (\ newcar) on id conflict do update", args) get();let query result = ditto store() execute( "insert into cars documents (\ newcar) \\ on id conflict do update", some(serde json json!({ "newcar" { " id" "123", "color" "blue" } }) into()), ) await?;final newcar = { " id" "123", "color" "blue", }; await ditto execute(""" insert into cars documents (\ newcar) on id conflict do update""", queryargs {"newcar" newcar}, ); insert with initial documents \<font color="#db2777">insert\</font> allows you to set specific documents as default data using the \<font color="#db2777">initial documents\</font> action initial documents are the documents inserted at the beginning of time and are viewed by all peers as the same \<font color="#db2777">insert\</font> operation this allows multiple peers to independently initialize the same default data safely , so regardless of the individual peer's starting point when inserting, the initial documents \<font color="#db2777">do nothing\</font> if the document id already exists in the local ditto store the \<font color="#db2777">on id conflict\</font> policy cannot change this behavior dql insert into your collection name initial documents (\[document]) in this syntax your collection name is the name of the collection from which you want to retrieve the data \[document] represents the document for example, setting up default data by inserting the given car details as an initial document let newcar = \[ " id" "123", "color" "blue" ] await ditto store execute( query """ insert into cars initial documents (\ newcar) """, arguments \[ "newcar" newcar ])var newcar = mapof( " id" to "123", "color" to "blue" ) ditto store execute(""" insert into cars initial documents (\ newcar) """, mapof("newcar", newcar))const newcar = { id "123" color "blue", }; await ditto store execute(` insert into cars initial documents (\ newcar)`, { newcar });dittoqueryresult result = (dittoqueryresult) ditto store execute( "insert into cars initial documents (\ newcar)", collections singletonmap("newcar", collections singletonmap("color", "blue")), );var args = new dictionary\<string, object>(); args add("newcar", new { id = "123", field1 = 0 }) await ditto store executeasync( "insert into cars initial documents (\ newcar)", args);struct car { std string id; std string color; }; // std map\<std string, car> args; args\["newcar"] = {"123", "blue"}; auto result = ditto get store() execute( "insert into cars initial documents (\ newcar)", args) get();let query result = ditto store() execute( "insert into cars initial documents (\ newcar)", some(serde json json!({ "newcar" { " id" "123", "color" "blue" } }) into()) ) await?; const newcar = { " id" "123" "color" "blue", }; await ditto execute(""" insert into cars initial documents (\ newcar)""", {"newcar" newcar}, );