Platform Manual
...
Complex Structures
Relationships
there are several methods for linking related data items and organizing them for easy lookup field referencing another document by id (see /#foreign key relationships ) embedded json object that acts as a register type or an embedded map (see /#key value relationships ) for an example demonstrating both the deeply embedded and flat models, see docid\ grjc1h2c70dadhhtpdhys overview 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 embed a json object ( register ) embed a map reference a field to a document reference a document to a collection many to many associates multiple entities in one collection with multiple entities in another collection embed a json object ( register ) embed a map create references between documents in different collections 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 embed a json object ( register ) embed a map create references between documents in different collections foreign key relationships to create a foreign key relationship , store the primary key to one document in another document a foreign key relationship establishes a link between two or more 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 pseudocode const results = await ditto store write(async (transaction) => { // create a person named susan in the "people" collection const cars = transaction scoped('cars') const people = transaction scoped('people') // create a car document in the "cars" collection const susanid = await people upsert({ name 'susan', }) await cars upsert({ make 'hyundai', color 'red', owner susanid, // set the owner field to susan's id }) // evict the susan document from the "people" collection await people findbyid(susanid) evict() }) key value relationships a key value relationship establishes a parent child hierarchy between embedded data elements in this hierarchy, the key functions as the parent, and its encapsulated values, represented as a set of key value pairs, serve as children when managing data that requires unique identifiers and relationships, instead of using an array to encode your data, use a map with unique string keys and object values instead