MongoDB Schema Design Practice: Embedding vs Referencing (Part 6)

🧩 MongoDB Practice Series – Real-World Schema Design Challenges (Part 6)

Welcome to Part 6 of the MongoDB Practice Series.

In this lesson, you will practice real-world schema design problems.

Instead of writing queries, you will decide:

  • Should we Embed data?
  • Or should we Reference it?

These decisions are very important for building scalable applications.


🟢 Exercise 1: Blog System Design

Problem:
Design a database for a blog where:

  • Each user can create many posts
  • Each post can have many comments

Question:
Which data should be embedded and which should be referenced?

Solution:


// Users Collection
{
  _id: 1,
  name: "Amit"
}

// Posts Collection
{
  _id: 101,
  user_id: 1,
  title: "My First Post",
  content: "Hello World"
}

// Comments Collection
{
  _id: 5001,
  post_id: 101,
  text: "Great post!"
}

Explanation:
Use referencing because posts and comments can grow very large. Keeping them separate improves scalability.


🟡 Exercise 2: E-Commerce Product Design

Problem:
Design a schema for an online store where:

  • Each product belongs to one category
  • Each product has multiple reviews

Solution:


// Products Collection
{
  _id: 1,
  name: "Laptop",
  category: "Electronics"
}

// Reviews Collection
{
  _id: 9001,
  product_id: 1,
  rating: 5,
  comment: "Excellent!"
}

Explanation:
Reviews should be referenced because they can grow indefinitely.


🔵 Exercise 3: When to Use Embedding

Problem:
Design a schema for storing:

  • User profile
  • Address

Each user has only one address.

Solution:


{
  _id: 1,
  name: "Sara",
  email: "sara@example.com",
  address: {
    city: "Delhi",
    state: "Delhi",
    pincode: "110001"
  }
}

Explanation:
Since each user has only one address, embedding is the best choice.


🔴 Exercise 4: Large Data Decision

Problem:
Design a system for storing:

  • Users
  • Millions of orders

Question:
Should orders be embedded inside users?

Answer:

No. Orders should be stored in a separate collection.

Reason:
Embedding millions of orders would make documents too large and slow.


🚀 Mini Challenge

Design a database for a:

  • School Management System

It should include:

  • Students
  • Teachers
  • Classes
  • Subjects

Decide:

  • What to embed?
  • What to reference?
  • Why?

Write your design before checking any solution.


🎯 What’s Next?

In Part 7 of the MongoDB Practice Series, we will practice:

  • Indexing challenges
  • Performance optimization
  • Fixing slow queries

Stay tuned for the next advanced level!


MongoDB Aggregation Practice Challenges with Solutions (Part - 5)

🧩 MongoDB Practice Series – Aggregation Challenges with Solutions (Part 5)

Welcome to Part 5 of the MongoDB Practice Series.

In this lesson, you will practice Aggregation in MongoDB.

Aggregation helps you perform calculations like:

  • Total
  • Average
  • Count
  • Grouping data

These skills are very useful for reports and analytics.


🟢 Exercise 1: Count Total Documents

Problem:
Count the total number of students in the collection.

Solution:


db.students.countDocuments()

Learning Goal: Learn how to count records.


🟡 Exercise 2: Calculate Average Value

Problem:
Find the average price of all products.

Solution:


db.products.aggregate([
  {
    $group: {
      _id: null,
      averagePrice: { $avg: "$price" }
    }
  }
])

Learning Goal: Use $group and $avg.


🟡 Exercise 3: Calculate Total Sum

Problem:
Find the total revenue from all orders.

Solution:


db.orders.aggregate([
  {
    $group: {
      _id: null,
      totalRevenue: { $sum: "$totalAmount" }
    }
  }
])

Learning Goal: Use $sum to calculate totals.


🔵 Exercise 4: Group Data by Category

Problem:
Count how many products exist in each category.

Solution:


db.products.aggregate([
  {
    $group: {
      _id: "$category",
      totalProducts: { $sum: 1 }
    }
  }
])

Learning Goal: Understand grouping by field value.


🔵 Exercise 5: Find Maximum Value

Problem:
Find the highest product price.

Solution:


db.products.aggregate([
  {
    $group: {
      _id: null,
      highestPrice: { $max: "$price" }
    }
  }
])

Learning Goal: Use $max.


🔴 Exercise 6: Group and Filter Results

Problem:
Find categories that have more than 5 products.

Solution:


db.products.aggregate([
  {
    $group: {
      _id: "$category",
      count: { $sum: 1 }
    }
  },
  {
    $match: {
      count: { $gt: 5 }
    }
  }
])

Learning Goal: Combine $group with $match.


🚀 Mini Challenge

Create a collection called sales with fields:

  • productName
  • category
  • quantity
  • price

Then write queries to:

  • Calculate total revenue (quantity × price)
  • Find total sales per category
  • Find the category with highest total sales

Try solving step by step.


🎯 What’s Next?

In Part 6 of the MongoDB Practice Series, we will practice:

  • Real-world schema design scenarios
  • Embedding vs referencing decisions
  • Designing scalable databases

Stay tuned for the next level!


Featured Post

MongoDB Schema Design Practice: Embedding vs Referencing (Part 6)

🧩 MongoDB Practice Series – Real-World Schema Design Challenges (Part 6) ← Part 5 MongoDB Tutorial Next → Welcome to Part 6 of...

Popular Posts