Platform Manual
Document Model
Relationships
you can model relationships between your data using docid 1cexfnswhjeqa r6yyl 8 and docid 1cexfnswhjeqa r6yyl 8 by way of embedded maps and arrays for more information about ditto's advanced types, see docid\ yxemkh1com3csuumqcnlf avoid using arrays in ditto due to potential merge conflicts when offline peers reconnect to the mesh and attempt to sync their updates, especially when multiple peers make concurrent updates to the same item within the array 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 relationship , as follows for an example demonstrating both the deeply embedded and flat models, see docid 8knpxrwcdkfknw8zeaijr 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 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 map list items in an array 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 map create references between documents in different collections foreign key relationship 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 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 relationship 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 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