Identifiers: Default and Custom
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. For more information, see Primary key, as follows.
- Customizable Primary Key: If preferred, you can customize the primary key as a string or JSON blob object. For more information, see Custom Configuration.
- Retrieval by _id: Using the Find By ID method, you can fetch a document by its primary key. For more information, see Fetching a Document by _id.
For more information, see Custom Configurations , as follows.
The following snippet demonstrates creating a new document assigned the string value 123abc.
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, 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.
The following snippet demonstrates combining the user_id and work_id fields to form the 123abc780 composite key: