3 - Relationships in Data
3 - Relationships in Data
3 - Relationships in Data
▪ Example
▪ In zips collection, which contain zip codes, we create an array of
stores where each element in the array is a store ID value that
identifies documents in the store's collection.
One-to-Many: reference, in the “one” side
2. Reference
a. Array of reference in one “many” side
b. Array of references in the other “many” side
Many-to-Many: embed, in the main side
▪ Example:
▪ Let's use the carts and items from our product catalog
▪ The main entity is the cart in which we want to find the items
▪ We embed the items in the cart, because we always retrieve this
information together.
▪ Having copies of items in the carts period is usually fine, because
they represent the state of those items at the time they were added to
the cart.
▪ The same applies to addresses and orders.
Many-to-Many: embed, in the main side
▪
Many-to-Many: embed, in the main side
▪ Example
▪ The address used for that order at the time of the order creation
should be duplicated
▪ This requirement of keeping a source of input applies only to this
specific representation in the many-to-many relationship
▪ An item may exist without being in any carts
▪ The address used for that order at the time of the order creation
should be duplicated.
Many-to-Many: embed, in the main side
▪ Example
▪ We need to keep a collection for the items.
▪ There will be several other access patterns in your application that
utilize items without or needing the information on orders they
have been added to.
▪ Item documents have different life cycle than cart documents.
▪ An item may exist without being in any carts,
▪ This requirement of keeping a source of input applies only to this
specific representation in the many-to-many relationship.
Many-to-Many: embed, in the main side
Many-to-Many: reference, in the seconday side
▪ Example:
Recap for the Many-to-Many Relationships