MongoDB Indexing and Performance Optimization Practice


🧩 MongoDB Practice Series – Indexing & Performance Challenges (Part 7)

Welcome to Part 7 of the MongoDB Practice Series.

In this lesson, you will learn how to improve query performance using indexes.

Indexes help MongoDB find data faster, especially in large collections.


🟢 Exercise 1: Create a Simple Index

Problem:
Create an index on the email field in the users collection.

Solution:


db.users.createIndex({ email: 1 })

Learning Goal: Improve search performance on email queries.


🟡 Exercise 2: Find All Indexes

Problem:
Check which indexes exist in the products collection.

Solution:


db.products.getIndexes()

Learning Goal: Understand existing indexing structure.


🟡 Exercise 3: Remove an Index

Problem:
Remove the index on the price field.

Solution:


db.products.dropIndex({ price: 1 })

Learning Goal: Learn how to manage indexes properly.


🔵 Exercise 4: Create a Compound Index

Problem:
Create an index on both category and price fields.

Solution:


db.products.createIndex({ category: 1, price: -1 })

Learning Goal: Understand compound indexes for multi-field queries.



🔵 Exercise 5: Optimize a Slow Query

Problem:
The following query is slow:


db.orders.find({ customerName: "Amit" })

Task:
Improve its performance.

Solution:


db.orders.createIndex({ customerName: 1 })

Learning Goal: Use indexing to speed up frequently searched fields.


🔴 Exercise 6: Use Explain Plan

Problem:
Check how MongoDB executes a query.

Solution:


db.products.find({ price: { $gt: 1000 } }).explain("executionStats")

Learning Goal: Analyze query performance using explain.


🚀 Mini Challenge

You have a large users collection with millions of records.

Design indexes for the following queries:

  • Search users by email
  • Filter users by country
  • Sort users by registration date

Think carefully:

  • Should you use single-field or compound indexes?
  • Which field is searched most frequently?

Try designing the indexing strategy before checking any solution.


🎯 What’s Next?

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

  • Real-world mini project challenges
  • Complete database design scenarios
  • Combining CRUD + Aggregation + Indexing

Stay tuned for the final advanced practice level!


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!


Featured Post

MongoDB Indexing and Performance Optimization Practice

🧩 MongoDB Practice Series – Indexing & Performance Challenges (Part 7) ← Previous Next → Welcome to Part 7 of the MongoDB P...

Popular Posts