CRUD Operations
CREATE
this article provides an overview of creating documents using the insert dql operation all modification operations in ditto are performed using the execute api method against the ditto store let result = try await ditto store execute(query / query /, arguments / arguments /);var result = ditto store execute(/ query /, / arguments /)const result = await ditto store execute(/ query /, / arguments /)dittoqueryresult result = (dittoqueryresult) ditto store execute( / query /, / arguments /, / continuation /);var result = await ditto store executeasync(/ query /, / arguments /);auto result = ditto get store() execute(/ query /, / arguments /) get();let result = ditto store() execute(/ query /, / arguments /); when writing updates by way of the execute api method with insert operation if the document does not exist locally, ditto creates it if the document already exists in the local store, ditto throws an error if the document is inserted by two or more peers at the same time, the documents that were inserted after the first document will be treated as having performed an update operation for all fields creating a new 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"}}; auto result = ditto get store() execute( "insert into cars documents (\ newcar)", args) get();use serde serialize; \#\[derive(serialize)] struct args { newcar car, } \#\[derive(serialize)] struct car { color string } // let args = args { newcar car { color "blue" to string() }, }; ditto store() execute( "insert into cars documents (\ newcar)", some(args into())); for complete dql syntax, see ditto query language (dql) > docid\ pokcnkex oxyejkoaupti creating multiple documents multiple documents can be created at the same time using the insert operation 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();use serde serialize; \#\[derive(serialize)] struct args { newcar car, } \#\[derive(serialize)] struct car { color string } // let args = args { car1 car { color "blue" to string() }, car2 car { color "red" to string() }, }; ditto store() execute( "insert into cars documents (\ car1),(\ car2)", some(args into())); new document identifiers when creating a document, unless manually supplied, ditto automatically generates and assigns the new document a 128‑bit universally unique identifier (uuid) the document identifier is represented as id and is the primary key for the document automatic id if an identifier isn't provided ditto will automatically generate one this identifier can be accessed on the result object using the mutateddocumentids field let result = await ditto store execute( query "insert into cars documents (\ newcar)", arguments \[ newcar \["color" "blue"] ]); // "507f191e810c19729de860ea" print(result mutateddocumentids()\[0])var result = ditto store execute( "insert into cars documents (\ newcar)", mapof("newcar" to mapof("color" to "blue"))) // "507f191e810c19729de860ea" println(result mutateddocumentids() first())const result = await ditto store execute( "insert into cars documents (\ newcar)", { newcar { color 'blue' } }); // "507f191e810c19729de860ea" console log(result mutateddocumentids()\[0])dittoqueryresult result = (dittoqueryresult) ditto store execute( "insert into cars documents (\ newcar)", collections singletonmap("newcar", collections singletonmap("color", "blue")), new continuation<>() { @nonnull @override public coroutinecontext getcontext() { return emptycoroutinecontext instance; } @override public void resumewith(@nonnull object o) { if (o instanceof result failure) { // handle failure } } } ); // "507f191e810c19729de860ea" system out println(result mutateddocumentids()\[0]);var insertargs = new dictionary\<string, object>(); insertargs add("newcar", new { color = "blue" }); var result = await ditto store executeasync( "insert into cars documents (\ newcar)", insertargs); // "507f191e810c19729de860ea" result mutateddocumentids foreach(id => console writeline(id));std map\<std string, std map\<std string, std string>> args; args\["newcar"] = {{"color", "blue"}}; auto result = ditto get store() execute( "insert into cars documents (\ newcar)", args) get(); // "507f191e810c19729de860ea" std cout << result mutated document ids()\[0] to string();use serde serialize; \#\[derive(serialize)] struct args { newcar car, } \#\[derive(serialize)] struct car { color string } // let args = args { newcar car { color "blue" to string() }, }; let result = ditto store() execute( "insert into cars documents (\ newcar)", some(args into())); // "507f191e810c19729de860ea" println!("{}", result mutated document ids()\[0] to string()) supplying a custom id a custom document identifier can be either a string or a json object json object keys are referred to as composite identifiers because multiple sub fields together represent the identifier document identifiers are immutable and you can only configure it at the time of document creation once a document is created, to ensure consistency and uniqueness throughout the platform, the unique identifier that either ditto automatically generated and assigned or you manually assigned becomes permanent and cannot be changed at a later time for more information about document ids, see platform manual > docid\ cw7ajztsb92tgeo1o8hmv string id for example, the following snippet demonstrates a new document assigned the custom id "123" let result = await ditto store execute( query "insert into cars documents (\ newcar)", arguments \[ newcar \[ " id" "123", "color" "blue"] ]); // "123" print(result mutateddocumentids()\[0])var result = ditto store execute( "insert into cars documents (\ newcar)", mapof("newcar" to mapof(" id" to "123", "color" to "blue"))) // "507f191e810c19729de860ea" println(result mutateddocumentids() first())const newcar = { id "123", color "blue" } const result = await ditto store execute(` insert into cars documents (\ newcar)`, { newcar }); // "123" console log(result mutateddocumentids()\[0])map\<string, string> newcar = new hashmap<>(); newcar put(" id", "123"); newcar put("color", "blue"); dittoqueryresult result = (dittoqueryresult) ditto store execute( "insert into cars 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 } } } ); // "123" system out println(result mutateddocumentids()\[0]);var insertargs = new dictionary\<string, object>(); insertargs add("newcar", new { id = "123", color = "blue" }); var result = await ditto store executeasync( "insert into your collection name documents (\ newcar)", insertargs); // "123" result mutateddocumentids foreach(id => console writeline(id));std map\<std string, std map\<std string, std string>> args; args\["newcar"] = {{" id", "123"},{"color", "blue"}}; auto result = ditto get store() execute( "insert into cars documents (\ newcar)", args) get(); // "123" std cout << result mutated document ids()\[0] to string();use serde serialize; \#\[derive(serialize)] struct args { newcar car, } \#\[derive(serialize)] struct car { id string, color string } // let args = args { newcar car { id "123" to string(), color "blue" to string() }, }; let result = ditto store() execute( "insert into cars documents (\ newcar)", some(args into())); // "123" println!("{}", result mutated document ids()\[0] to string()) following is the new 123 document that results ditto document { " id" "123", "color" "blue" } composite id the following snippet demonstrates combining the vin and make fields to form the composite key let arguments = \[ newcar \[ " id" \[vin "123", make "toyota"], "color" "blue"] ]; let result = await ditto store execute( query "insert into cars documents (\ newcar)", arguments arguments); // "{vin "123", make "toyota"}" print(result mutateddocumentids()\[0])var result = ditto store execute( "insert into cars documents (\ newcar)", mapof( "newcar" to mapof( " id" to mapof( "vin" to "123", "make" to "toyota" ), "color" to "blue" ))) // "{vin "123", make "toyota"}" println(result mutateddocumentids() first())const newcar = { id { vin "123", make "toyota" }, color "blue" } const result = await ditto store execute(` insert into cars documents (\ newcar)`, { newcar }); // "{vin "123", make "toyota"}" console log(result mutateddocumentids()\[0])map\<string, string> newcarid = new hashmap<>(); newcarid put("vin", "123"); newcarid put("make", "toyota"); map\<string, object> newcar = new hashmap<>(); newcar put(" id", newcarid); newcar put("color", "blue"); dittoqueryresult result = (dittoqueryresult) ditto store execute( "insert into cars 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 } } } ); // "{vin "123", make "toyota"}" system out println(result mutateddocumentids()\[0]);var newid = new { vin "123", make "toyota"}; var insertargs = new dictionary\<string, object>(); insertargs add("newcar", new { id = newid, color = "blue" }); var result = await ditto store executeasync( "insert into your collection name documents (\ newcar)", insertargs); // "{vin "123", make "toyota"}" result mutateddocumentids foreach(id => console writeline( system text json jsonserializer serialize(id)));struct carid { std string vin; std string make; }; struct car { carid id; std color string; }; std map\<std string, car> args; args\["newcar"] = {{"123", "toyota"}, "blue"}; auto result = ditto get store() execute( "insert into cars documents (\ newcar)", args) get(); // "{vin "123", make "toyota"}" std cout << result mutated document ids()\[0] to string();use serde serialize; \#\[derive(serialize)] struct args { newcar car, } \#\[derive(serialize)] struct carid { vin string, make string } \#\[derive(serialize)] struct car { id carid, color string } // let args = args { newcar car { id carid { vin "123" to string(), make "toyota" to string() }, color "blue" to string() }, }; let result = ditto store() execute( "insert into cars documents (\ newcar)", some(args into())); // "{vin "123", make "toyota"}" println!("{}", result mutated document ids()\[0] to string())