Model One-to-Many Relationships with Embedded Documents
Data in MongoDB has a flexible schema. Collections do not enforce document structure. Decisions that affect how you model data can affect application performance and database capacity. See Data Modeling Concepts for a full high level overview of data modeling in MongoDB.
This document describes a data model that uses embedded documents to describe relationships between connected data.
Consider the following example that maps patron and multiple address relationships. The example illustrates the advantage of embedding over referencing if you need to view many data entities in context of another. In this one-to-many relationship between
address data, the
patron has multiple
In the normalized data model, the
address documents contain a reference to the
If your application frequently retrieves the
address data with the
name information, then your application needs to issue multiple queries to resolve the references. A more optimal schema would be to embed the
address data entities in the
patron data, as in the following document:
With the embedded data model, your application can retrieve the complete patron information with one query.