Skip to main content
MongoDB
CHAPTER 13 Beginner

MongoDB Schema Design | One-to-One, One-to-Many Relationships

Updated: May 16, 2026
15 min read

# 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.

javascript
12345678910
// A User document with an Embedded 1:1 Badge relationship
{
  "_id": ObjectId("..."),
  "name": "Sarah Connor",
  "email": "sarah@example.com",
  "badge": {
    "badge_number": "A-1234",
    "access_level": 5
  }
}

*(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.

javascript
123456789
// A User document with an Embedded 1:N Addresses relationship
{
  "_id": ObjectId("..."),
  "name": "John Smith",
  "addresses": [
    { "type": "Home", "city": "Seattle", "zip": "98101" },
    { "type": "Office", "city": "Redmond", "zip": "98052" }
  ]
}

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 the cities 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).

javascript
123456789
// The City Document (Collection: cities)
{ "_id": ObjectId("City_NYC"), "name": "New York" }

// The Citizen Document (Collection: citizens)
{ 
  "_id": ObjectId("User_John"), 
  "name": "John",
  "city_id": ObjectId("City_NYC") // The Reference Link!
}

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!

javascript
12345678910111213
// Student Document
{
  "_id": ObjectId("Student_Bob"),
  "name": "Bob",
  "enrolled_courses": [ ObjectId("Course_Math"), ObjectId("Course_Art") ]
}

// Course Document
{
  "_id": ObjectId("Course_Math"),
  "title": "Calculus 101",
  "enrolled_students": [ ObjectId("Student_Bob"), ObjectId("Student_Alice") ]
}

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. 1. What is the MongoDB Golden Rule of schema design regarding how data is accessed?
  1. 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 a book document that has a 1:1 relationship with an author (Embedded), and a 1:N relationship with reviews (Embedded Array).
json
123456789
{
  "_id": 1,
  "title": "Learning NoSQL",
  "author": { "name": "Alice", "country": "UK" },
  "reviews": [
    { "user": "Bob", "stars": 5 },
    { "user": "Charlie", "stars": 4 }
  ]
}

12. MCQ Quiz with Answers

Question 1

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)?

Question 2

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.

14. FAQs

Q: Can I enforce schema rules in MongoDB if my boss demands it? A: Yes! While the default is dynamic, you can apply "JSON Schema Validation" to a collection. This forces MongoDB to strictly reject inserts that don't match your exact defined rules (e.g., "Age must be an integer > 18"). We cover this in Chapter 18.

15. Summary

Schema design in MongoDB is an art form. By prioritizing the needs of your application's user interface, you can strategically use Embedding for blazing-fast 1:1 and 1:N reads, while deploying Referencing (Linking) to safely handle infinitely growing, unbounded datasets without breaching document size limits.

16. Next Chapter Recommendation

The eternal debate among NoSQL architects is "Should I embed this, or reference it?" In Chapter 14: Embedding vs Referencing Documents, we will dive deeper into the architectural trade-offs, data duplication, and real-world system design.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·