This content is for SDK V4. For the latest version, see the V5 documentation.
Creating Documents
To insert a document, call theEXECUTE API method against the ditto.store object and include an INSERT INTO query that specifies the document to be inserted.
Embed related data within a single document — typically as a map keyed by ID — for atomic single-document sync. See Denormalized Documents for the recommended pattern, or use a foreign-key relationship across collections when sub-entities need their own permission scope or independent access.
Inserting Multiple Documents
To efficiently create multiple documents, use the INSERT INTO operation and specify multiple query parameters, as follows:Identifying Documents
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 serves as the primary key for the document.
Retrieving Document IDs
To access the IDs of the documents affected by the INSERT INTO operation, call themutatedDocumentIDs method on the result object after the insertion like this:
Supplying String IDs
When creating a document, you can assign it a custom ID. This custom ID can be generated using a single string value or a combination of two or more string values. This flexibility in structuring document identifiers allows you to customize document IDs to your specific requirements, use cases, or standard naming conventions. The following snippet demonstrates a new document assigned the custom ID “123”.Forming Composite Keys
The following demonstrates combining thevin and make fields to form a composite key:
Creating MAPs
There are two ways to create a MAP structure within a document:- In an INSERT operation — Create a new document and nest it with this set of fields.
- In an UPDATE operation — If the MAP does not exist, create it. (See UPDATE)
The flat model is a simple, non‑embedded structure in which you spread your data across multiple, separate documents.
Inserting to Create a MAP
When inserting a new document in Ditto, you can define a field as a MAP and include the structure of key-value pairs nested within it — a two-in-one approach. To do this, first disable strict mode before starting synchronization. Read moreDQl