Video Transcript (English)
Welcome! Another relationship we frequently encounter is the one to many.
Examples of one-to-many relationships include: Customer and purchases, or auto maker and car models.
In this video, we’ll look at the one to many relationship in our bookstore app using a printed book and its reviews.
In this relationship, printed books represent the one side of the relationship and the reviews represent the many side of the relationship.
Let’s look at embedding first.
The most common way to embed a one-to-many relationship in MongoDB is to embed the “many” side as an array of subdocuments in the "one" side of the relationship.
Here we’ve created an array called “reviews”. This array contains subdocuments of reviews for one book.
The second way to embed this relationship is by creating one subdocument within the parent document.
In this example, within the book document we have one subdocument for reviews which contains subdocuments for each review. An attribute from each review, in this case a user id, becomes the key for each subdocument.
An advantage of embedding a one-to-many relationship is that there is no duplication of information.
It is also a good option when the information on the "many" side cannot exist by itself, like a "review” without a “book”.
Now, let's look at ways to model references.
The first way to reference a one-to-many relationship is to use an array of references within the parent document.
In this example, each entry in the "reviews" array is a reference to a separate document for the given review.
We can use the reference value in the reviews array to look up the details for a given review.
The second way is to reference the parent in each of the many child documents.
As you see in the example, each review document has a field for "book_id" that points back to the parent document.
We could use a query for a specific book_id to return all relevant review documents without a join.
You can also have bi-directional references. The book document includes a reference to its review documents, while each review document contains a reference to its book.
When considering whether to embed or reference for a one-to-many relationship, embedding an array of subdocuments is usually the preferred option.
By using arrays to hold related data, our application can retrieve information for a document without $lookup operations or indexes on other collections.
However, we also have to remember the cardinality guideline.
We aim to have 1 billion reviews in our app in three years. There is a strong possibility that within a book document, the array of subdocuments containing the reviews could become an unbounded array, especially for popular books.
You may recall that sometimes particular guidelines can become a priority when we are deciding whether to embed or reference.
If an array is unbounded or very large, like in the case of the relationship between printed books and reviews, those arrays may ultimately degrade performance.
In this case, because an unbounded array within the book document is a possibility whether we embed or add a reference, we should reference the book id in each review instead.
Let’s recap what you learned in this video.
When embedding a one-to-many relationship we have 2 main options: We can use an array of subdocuments, or we can use a single subdocument with multiple key value pairs where each value is a subdocument and the key is simply a unique value.
Using an array of subdocuments is the preferred option.
However, if an array is unbounded or very large, like in the case of the relationship between printed books and reviews, those arrays may ultimately degrade performance so referencing would be a better option.
When referencing a one-to-many relationship we have a few options: Use an array of references to the child documents in the parent document, use a reference to the parent document in the child documents, or have it both ways and create bidirectional references.
