Document Structure
Ditto documents are composed of field-and-value pairs and have the following structure:_id:STRINGname:STRINGage:NUMBERemail:STRINGaddress:OBJECTis_active:BOOLEANcreated_at:DATE
Identifying Documents
In Ditto, each document stored in a collection requires a unique_id field that acts as a primary key. If an inserted document omits the _id field, Ditto automatically generates a unique identifier for the _id field.
Once set for a given document, the _id field cannot be changed. The same document contents inserted with a different _id will be treated as a new document within Ditto.
The _id is required for all documents and can be any JSON data type.
While generating a unique identifier is the default behavior, typically you will provide your own _id values represented as a more complex object (acting as a composite key).
For example, the following document includes a _id field with a complex object:
_id object or by breaking it down into its individual components.
DQL
DQL
_id field when designing your data model, as this is used for authorization rules within Ditto. For more information, see Authorization.
Document Fields
Documents are composed of fields, which are key-value pairs.Field Names
Similar to most document-oriented databases, you can only usestrings to encode field names in documents.
For complete naming rules, see IDs, Paths, Strings, and Keywords.
Field Values
Field values can be encoded using various data types, including scalar types, providing flexibility in representing a wide range of information. Each value of a field is stored as a specific CRDT type, for example aMAP or REGISTER.
You can read more about CRDTs in Syncing Data.
Relationships
There are several methods for linking related data items and organizing them for easy lookup:- Field referencing another document by
_id. - Embedded JSON object within the document.
Foreign-Key Relationships
To create a foreign-key relationship, store the primary key to one document within another document. A foreign-key relationship establishes a link between two or more datasets. For example, if you have two collections,cars and owners, where each car has a corresponding owner, every document in the cars collection would include a field containing the ID of a document in the owners collection.
For example:
Car
Owner