Questions
How do you do data modeling in MongoDB?
The Scenario
You are a backend engineer at an e-commerce company. You are designing a new service that will store information about products and their reviews.
You have decided to use MongoDB for this service, but you are not sure how to model the data.
The Challenge
Explain how you would do data modeling in MongoDB. What are the different data modeling patterns that you would use, and what are the trade-offs between them?
A junior engineer might try to model the data in the same way that they would in a relational database. This would be a mistake, because MongoDB is a document-oriented database, and it has a different data modeling paradigm.
A senior engineer would know that data modeling in MongoDB is all about embedding and linking. They would be able to explain the different data modeling patterns and would have a clear plan for how to model the data for this use case.
Step 1: Understand the Key Concepts
| Concept | Description |
|---|---|
| Embedding | Storing related data in a single document. |
| Linking | Storing a reference to another document. |
Step 2: Choose the Right Pattern for the Job
| Use Case | Recommended Pattern |
|---|---|
| One-to-one relationships | Embedding |
| One-to-many relationships | Embedding or linking, depending on the use case. |
| Many-to-many relationships | Linking |
For our use case, we have a one-to-many relationship between products and reviews. A product can have many reviews, and a review belongs to a single product.
We could model this in two ways:
- Embed the reviews in the product document: This would be a good choice if we always want to retrieve the reviews along with the product.
- Link the reviews to the product document: This would be a good choice if we want to be able to retrieve the reviews independently of the product.
In this case, we will choose to embed the reviews in the product document. This is because we will almost always want to retrieve the reviews along with the product.
Step 3: Code Examples
Here’s how we can model the data with embedded reviews:
{
"_id": "product123",
"name": "My Awesome Product",
"reviews": [
{
"review_id": "review456",
"user_id": "user123",
"content": "This product is great!"
},
{
"review_id": "review789",
"user_id": "user456",
"content": "This product is not so great."
}
]
} Practice Question
You are building a social media application and need to model the relationship between users and their followers. Which of the following would be the most appropriate?