- Data Models >
- Data Model Examples and Patterns >
- Model Relationships Between Documents >
- Model One-to-Many Relationships with Document References
Model One-to-Many Relationships with Document References¶
Overview¶
This page describes a data model that uses references between documents to describe one-to-many relationships between connected data.
Pattern¶
Consider the following example that maps publisher and book relationships. The example illustrates the advantage of referencing over embedding to avoid repetition of the publisher information.
Embedding the publisher document inside the book document would lead to repetition of the publisher data, as the following documents show:
To avoid repetition of the publisher data, use references and keep the publisher information in a separate collection from the book collection.
When using references, the growth of the relationships determine where to store the reference. If the number of books per publisher is small with limited growth, storing the book reference inside the publisher document may sometimes be useful. Otherwise, if the number of books per publisher is unbounded, this data model would lead to mutable, growing arrays, as in the following example:
To avoid mutable, growing arrays, store the publisher reference inside the book document: