Introduction

Welcome to the third lesson of our "Schemas and Relations" course! In our previous lessons, we covered MongoDB's flexible schema design and explored one-to-one relations. In this lesson, we will dive into the realm of one-to-many relations in MongoDB, examining how to model these relationships using both embedded documents and references.

What are One-to-Many Relations?

In a one-to-many relation, a single entity is related to multiple other entities. For example, consider a book that has multiple reviews. This establishes a one-to-many relation between a book and its reviews. Similarly to one-to-one relations, we can represent one-to-many relationship in two primary ways:

  1. Embedded Documents: The related data is included as an embedded array within the parent document.
  2. References: The _id of the parent document can be stored in multiple related documents, or an array of related document identifiers can be stored in the parent document.
Representing One-to-Many Relations Using Embedded Documents

Embedded documents in MongoDB allow you to store related data within a parent document, making it easy to retrieve all the information in a single query. This method is suitable when the "many" side of the relation has a manageable number of elements.

Here's an example representing a book and its reviews using embedded documents:

In this code snippet:

  1. We insert a book document into the books collection using insertOne().
  2. The document contains title, author, and reviews fields. The reviews field is an array of embedded documents, each representing a review with fields like reviewer, rating, comment, and review_date.

This setup efficiently represents the one-to-many relationship between a book and its reviews using embedded documents.

Representing One-to-Many Relations Using References

Using references becomes beneficial when the "many" side of the relation grows significantly or when multiple documents need to link to the same data. References store the _id value of one document in another, maintaining normalized data and avoiding duplicity.

Here's an example using references to represent a book and its reviews:

In this code snippet:

  1. We create a unique ObjectId for the book and store it in bookId.
  2. Using insertOne(), we add a book document to the books collection with _id set to bookId, title to "1984", and author to "George Orwell".
  3. Using insertMany(), we insert multiple review documents into the reviews collection. Each review document includes reviewer, rating, comment, review_date, and book_id fields, the latter of which references the _id of the book document.

There are several ways to maintain the link between the book and its reviews. You can store the book_id in the reviews collection, as shown above, or alternatively, you can store an array of review_ids in the books collection. Additionally, you can store references in both collections. The choice depends on your specific use case and how you intend to retrieve and manage the related data.

Summary

In this lesson, you learned about one-to-many relations in MongoDB and explored two methods to implement them: embedded documents and references. Use embedded documents when the related data isn't extensive and does not require linking from multiple documents. When the data volume is larger or needs to be referenced by multiple documents, opting for references is more appropriate. Up next, you'll get hands-on practice to solidify your understanding of these concepts. Keep coding and enjoy your MongoDB journey!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal