Document Model
Identifiers
every document has an identifier field id the identifier field serves as the primary key for the document and can be either auto generated or custom assigned following are the main features and qualities associated with the id field document identification the document id field serves as the primary key identifying the document in ditto customizable identifier if preferred, you can customize the id as a string or json‑object (see /#custom document identifier ) retrieval by id you can fetch a document by its identifier for more information, see fetching a document by id auto generated document identifier when inserting a document, unless manually supplied, ditto automatically generates and assigns the new document a 128‑bit universally unique identifier (uuid) to the id 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()) custom document identifier if supplying your own document id, you can encode your value in a string or, if forming a composite key, a json object you can configure a custom document id only 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 following are a few key principles for handling ids in ditto do not include personally identifiable information (pii) in ids ids should be immutable; that is, unchangeable once assigned and permanent if an id is formed by multiple fields, referred to as a composite key , those fields are not individually indexed and therefore searchable supplying a custom string identifier the following snippet demonstrates creating a new document assigned the string value 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()) forming a composite key identifier the decision to opt for a composite key depends on your specific use case following are typical use cases for forming a composite key to implement additional logic to handle (or prevent) duplicate writes to simplify queries and enhance efficiency in the querying process to form a composite key, set the id to a json object when inserting a new document the following snippet demonstrates combining the vin and make fields to form a 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();struct args { newcar car, } struct carid { vin string, make string } 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)", args); // "{vin "123", make "toyota"}" println!("{}", result mutated document ids()\[0] to string()) fetching a document by id retrieve a document by its identifier ( id ) dql select from cars where id = '123' if querying a composite key identifier, use dot ( ) notation dql select from cars where id model = 'toyota' for more information, see docid 7kvjcanf0gijil86o84qi and docid\ sw8k jghbnpexshzo77lu in ditto query language