- Data Models >
- Data Model Examples and Patterns >
- Model Relationships Between Documents >
- Model One-to-One Relationships with Embedded Documents
Model One-to-One Relationships with Embedded Documents¶
On this page
Overview¶
This page describes a data model that uses embedded documents to describe a one-to-one relationship between connected data. Embedding connected data in a single document can reduce the number of read operations required to obtain data. In general, you should structure your schema so your application receives all of its required information in a single read operation.
Embedded Document Pattern¶
Consider the following example that maps patron and address
relationships. The example illustrates the advantage of embedding over
referencing if you need to view one data entity in context of the
other. In this one-to-one relationship between patron
and
address
data, the address
belongs to the patron
.
In the normalized data model, the address
document contains a
reference to the patron
document.
If the address
data is frequently retrieved with the name
information, then with referencing, your application needs to issue
multiple queries to resolve the reference. The better data model would
be to embed the address
data 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.
Subset Pattern¶
A potential problem with the embedded document pattern is that it can lead to large documents that contain fields that the application does not need. This unnecessary data can cause extra load on your server and slow down read operations. Instead, you can use the subset pattern to retrieve the subset of data which is accessed the most frequently in a single database call.
Consider an application that shows information on movies. The
database contains a movie
collection with the following
schema:
Currently, the movie
collection contains several fields that the
application does not need to show a simple overview of a movie, such as
fullplot
and rating information. Instead of storing all of the
movie data in a single collection, you can split the collection into
two collections:
The
movie
collection contains basic information on a movie. This is the data that the application loads by default:The
movie_details
collection contains additional, less frequently-accessed data for each movie:
This method improves read performance because it requires the application to read less data to fulfill its most common request. The application can make an additional database call to fetch the less-frequently accessed data if needed.
Tip
When considering where to split your data, the most frequently-accessed portion of the data should go in the collection that the application loads first.
See also
To learn how to use the subset pattern to model one-to-many relationships between collections, see Model One-to-Many Relationships with Embedded Documents.
Trade-Offs of the Subset Pattern¶
Using smaller documents containing more frequently-accessed data reduces the overall size of the working set. These smaller documents result in improved read performance and make more memory available for the application.
However, it is important to understand your application and the way it
loads data. If you split your data into multiple collections improperly,
your application will often need to make multiple trips to the database
and rely on JOIN
operations to retrieve all of the data that it
needs.
In addition, splitting your data into many small collections may increase required database maintenance, as it may become difficult to track what data is stored in which collection.