Platform Manual
Document Model
as a datastore that leverages conflict free replicated data type (crdt) technology to enable advanced sync capabilities, the foundation of each document is represented as a crdt map that is, when you invoke the upsert method and create a new document object, you form a top level map object at its root for example, the following snippet of a basic json like document object actually represents a single map object { " id" "123abc", "name" "sam", "age" 45, "isonline" false } if you need to represent a highly complex dataset in a document, you can embed a map within another map for more information, see /#embedding map structures , as follows avoid using arrays in ditto due to potential merge conflicts when offline peers reconnect to the mesh and attempt to sync their updates, especially when multiple peers make concurrent updates to the same item within the array embedding map structures embedding a map provides a way for you to structure and organize related data within a single document to create a complex structure with multiple levels of hierarchy as in, you can embed a map within a map , within another map , within another map , and so on you can model relationships between your data using foreign key and key value relationships by way of embedded maps and arrays for more information, see docid\ lqgzvvhtqzybx ald5b8j for example, the following snippet shows three levels of embedded maps details , engine , interior , and features { " id" "123abc", "make" "toyota", "model" "corolla", "year" 2022, "details" { "engine" { "type" "gasoline", "displacement" "1 8l" }, "interior" { "seats" 5, "color" "black" }, "features" { "safety" { "airbags" 6, "antilockbrakes" true }, "technology" { "infotainment" "touchscreen", "navigation" true } } } } each level contains its own key value pairs and, if used, children level maps you can represent key values using a register , counter , array , or another map for more information, see docid\ yxemkh1com3csuumqcnlf benefits of embedding maps embedding maps is beneficial in scenarios where you need to manage a collection of items and continuously modify that collection over time; that is you want to link multiple data items with a single unique string identifier, but you anticipate that these data items are subject to concurrent edits over time as an example, the following snippet demonstrates a basic point of sale (pos) system where you need to keep track of the customer orders collection and, since multiple users can add and remove orders within the collection, you embed a map to represent the ordered items, where each key denotes an item id and the linked value indicates the quantity ordered pseudocode const order = { customername 'john doe', orderdate '2023 08 15', items { 'item123' 2, // item id quantity ordered 'item456' 5, 'item789' 1 } }; // inserting the order into the ditto collection await ditto store collection('orders') upsert(order); adding or updating a map using the upsert method, do the following specify the document collection where you want to embed the map identify the parent document field property (object) that will contain the map define the key value pairs that will form the map structure (object) pseudocode await ditto store collection('collection name') upsert({ property { key 'value' }, }); the following snippet demonstrates embedding a map object with two key‑value pairs in all documents in the cars collection that have toyota as a field property pseudocode await ditto store collection('cars') upsert({ toyota { engine 'automatic', camera false } });