Video Transcript (English)
Welcome back! Now you know what an entity is in a database and the relationships that can exist among entities.
In this video, we’ll explore how to model these relationships by using approaches called embedding and referencing.
Let’s get started.
The choice between embedding and referencing depends on the relationships in your data.
Let’s start with a One-to-One relationship, which is when one entity is related to exactly one other entity.
For One-to-One relationships, we can embed the related data directly within the parent document. This keeps related data together and simplifies queries.
As you will recall, in our movies database, the relationship between a movie and a movie studio is a one-to-one relationship.
We can model this relationship by embedding the field-value pair containing the studio information directly within a movie document.
This means that all related information, including the studio, is stored in a single document, which means it can be retrieved in a single query.
Next, let’s look at One-to-Many relationships, where one entity is related to multiple other entities.
For One-to-Many relationships, embedding is the preferred approach to keep data that is accessed together stored together.
But we might want to use references when the related dataset is large, or when the data is often accessed independently, meaning we may want to query or update the referenced information by itself regularly.
Remember our one-to-many relationship from the last lesson? We know that a movie typically has multiple cast members. We can easily model this relationship in our document by providing an array of values for the cast field, as seen here.
However, if we maintain a large portion of data related to each cast member in a separate collection of documents, we could reference the cast members with an array of referenced IDs that point to the appropriate document in the performers collection.
Finally, let’s see how to model Many-to-Many relationships, where multiple entities are related to multiple other entities.
For Many-to-Many relationships, you can either embed or reference depending on your requirements. It’s also possible to use a combination of embedding and referencing but this is out of scope for this video.
Let's go back to our example from the previous video. A movie may be shown in many theaters. And a theater typically shows many different movies at a time.
In this case, both movies and theaters could have arrays with embedded information. In this example, we’ve embedded an array of theaters in the movie document, and an embedded array of movies in a theater's document.
However, for both movies and theaters, we hold a lot of data in a single document and will regularly want to access each independently of the other. Because of this, we would probably want to use references instead of embedding.
To do that, we can replace the embedded arrays of literal values in the movies and theaters documents with an embedded array of references to the other documents.
Both movies and theaters reference each other, maintaining flexibility and allowing easy access to either side of the relationship.
It’s worth mentioning that we typically advise against bidirectional referencing due to maintenance overhead, but for our movie app this approach makes the most sense.
Also, while referencing helps reduce the document size, it increases complexity of queries needed to retrieve related information.
Besides relationship type, there are other key factors to consider when deciding between embedding and referencing.
Embedding is ideal when we need to access data frequently as a single unit. It also works best for small datasets that don’t change often.
It’s also the ideal choice when you want to minimize the number of database operations and joins required to retrieve related data.
On the other hand, referencing is preferable when we need to manage large or complex datasets independently from the current document.
This approach works best for documents that are large or likely to grow.
Something else to think about when deciding between embedding and referencing is the limitations on MongoDB documents.
You’ll recall that documents have a maximum size of 16 megabytes and a limit of 100 levels of nesting.
When we approach these limits, we need to consider flattening the data structure or using references to simplify the model. This will keep the document size manageable and maintain the efficiency of our queries.
Nice work! In this lesson, you learned some foundational principles of data modeling for MongoDB!
Specifically, we defined the types of relationships that exist among them in our data: One-to-one, One-to-many, and Many-to-many.
We also discussed the differences between embedding and referencing, and how to choose the right approach for your use case.
Now you have the foundation you need to design efficient and scalable data models, which will be valuable for you to know as you progress through this course and gain more experience with MongoDB.
Until next time, keep learning!