MongoDB Schema Design | One-to-One, One-to-Many Relationships
# CHAPTER 13
Relationships and Schema Design
1. Introduction
In a relational database (SQL), schema design is purely an exercise in normalization: you chop data into dozens of tiny tables and connect them with Foreign Keys. In MongoDB, schema design is entirely different. It is an exercise in Application Driven Design. You design the schema based on *how your application reads the data*. In this chapter, we will learn how to map real-world relationships—One-to-One, One-to-Many, and Many-to-Many—into flexible NoSQL structures.2. Learning Objectives
By the end of this chapter, you will be able to:- Understand the MongoDB philosophy of "Application Driven Design".
- Model One-to-One (1:1) relationships.
- Model One-to-Many (1:N) relationships.
- Model Many-to-Many (N:M) relationships.
- Understand when to embed data vs. when to link data.
3. Application Driven Design
In SQL, you ask: *"What is the data?"* In MongoDB, you ask: *"What data does the User Interface need to render this specific screen?"*The Golden Rule of MongoDB: Data that is accessed together, should be stored together. If your mobile app screen shows a User Profile AND their Address at the exact same time, you should store them in the exact same document.
4. One-to-One (1:1) Relationships
A One-to-One relationship means Entity A has exactly one Entity B. *Example: A User has exactly one Employee ID Badge.*How to model it: You almost *always* Embed the data directly inside the main document as a nested object.
*(Retrieving Sarah's data requires 1 query. In SQL, it would require 2 queries or a JOIN).*
5. One-to-Many (1:N) Relationships
A One-to-Many relationship means Entity A has many Entity Bs. *Example: A User has many Shipping Addresses.*How to model it: You Embed the data as an Array of Objects.
6. The "One-to-Squillions" (1:N) Problem
Embedding is great for small arrays (like 3 addresses). But what if the relationship is a City has many Citizens? New York City has 8 million citizens. If you embed 8 million citizens into an array inside thecities document, the document will vastly exceed the 16MB size limit and crash the database.
How to model it: If the array is unbounded (can grow infinitely), you MUST switch to Referencing (Linking). You put the Citizens in their own collection, and put the City's ObjectId on them (exactly like a SQL Foreign Key).
7. Many-to-Many (N:M) Relationships
Entity A has many Entity Bs, AND Entity B has many Entity As. *Example: Students enroll in Many Courses, and Courses have Many Students.*How to model it: You use Two-Way Referencing (Arrays of ObjectIds). Unlike SQL, which requires a complex third "Pivot Table", MongoDB just stores an array of IDs on both sides!
8. Common Mistakes
- Applying SQL Normalization to MongoDB: The worst schema you can design in MongoDB is one with 20 different collections linked by ObjectIds that requires you to do 5 queries just to load a webpage. Embrace embedding. Data duplication (Denormalization) is highly acceptable in MongoDB if it speeds up read performance!
9. Best Practices
- Favor Reads over Writes: Usually, data is read 1,000 times more often than it is written (Think of a Tweet: written once, read by millions). Therefore, design your documents so that the Read query is instantaneous, even if it makes the Update query slightly more complex.
10. Exercises
- 1. What is the MongoDB Golden Rule of schema design regarding how data is accessed?
- 2. If a blog post has 3 "Tags" (e.g., "tech", "nosql", "coding"), should you put the Tags in their own collection and link them, or embed them as an array of strings directly in the blog post document?
11. MongoDB Challenges
Design the JSON structure for abook document that has a 1:1 relationship with an author (Embedded), and a 1:N relationship with reviews (Embedded Array).
12. MCQ Quiz with Answers
When designing a MongoDB schema, how should you model a "One-to-Squillions" relationship (e.g., A system logging billions of sensor readings for a single IoT machine)?
Unlike Relational SQL databases, MongoDB does not require a third "Pivot Table" to manage Many-to-Many relationships. How does MongoDB handle Many-to-Many?
13. Interview Questions
- Q: Explain the core philosophical difference between SQL Database Schema design (Normalization) and MongoDB Schema design (Application Driven Design).
- Q: Describe a scenario where data duplication (Denormalization) is considered an architectural best practice in MongoDB.