Documents and IDs (Legacy)
Ditto is a NoSQL database that organizes JSON-like documents into collections. Unlike JSON, with Ditto you can apply updates directly to the document, ensuring sync across all connected peers.
Additionally, it offers support for various data types. For an overview of the data types you can use in Ditto, see the Platform Manual > Data Structures and Types.
A document serves as a schema-flexible unit of data. If collections are compared to tables, a document can be compared to a row. Each document at its core is a map capable of holding fields and embedded key-value pairs.
Every document is assigned a primary key, referred to as a document ID.
For more information about Ditto documents, see Platform Manual > Document Model.
Primary Key: Document ID
When referencing the document ID in your queries for the HTTP API, pass id instead of the reserved _id parameter. However, if not making API calls, pass the reserved _id instead.
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 provide a custom document ID, when invoking upsert to create a new document, include the id parameter with a string set to the value you want to use as the document's ID.
For example, the following snippet demonstrates creating a new document in the people collection with the custom ID set to abc123, along with the specified name and age values:
To form a composite key, when calling upsert to create a new document, pass the fields you want to combine to form the new primary key in an embedded map structure under the id field.
For example, the following snippet shows an upsert operation on the people collection forming a composite key made up of two parts:
- string
- integer
Ditto compares document IDs based on the combination of keys and their corresponding values, not by literal order in which the key-value pairs are defined.
Therefore, if two document IDs have the same key-value pairs, they will be considered equal. For example: {a: "foo", b: 1} == { b: 1, a: "foo" }