Relationships
You can model relationships between your data using foreign‑key relationships and key‑value pair relationships by way of embedded maps and arrays. For more information about Ditto's advanced types, see Data Types.
Ditto does not support nesting documents within documents. If you need to create relationships between documents, use a foreign-key relationship by referencing the document ID. For more information, see Foreign-Key Relationships, as follows.
For an example demonstrating both the deeply embedded and flat models, see Complex Structures.
The following table provides a complete overview of the different relationships you can form in Ditto, as well as a brief description, list of possible approaches you can take, and links to related content:
Relationship | Description | Approaches |
---|---|---|
One-to-many | Associates a parent element with children elements to establish a hierarchy. |
|
Many-to-many | Associates multiple entities in one collection with multiple entities in another collection. |
|
Many-to-one | Associates two or more collections, where one collection refers to the primary key of another collection to create a meaningful relationship between the datasets. |
|
You can represent a one-to-many or many-to-one relationship between two or more collections, in which one collection refers to the primary key of another collection to create a meaningful relationship between the datasets.
For example, the following snippet demonstrates a foreign-key relationship between documents in the cars and people collections, in which the reference to susanId serves as the foreign key establishing a relationship between cars and people:
A key-value relationship establishes a parent-child hierarchy between embedded data elements where the key serves as the parent and the associated values for the children to link related data items as well as organize them in a structure that enhances the efficiency of lookup.