MongoDB Schema Design Practice Questions with Solutions

Introduction

MongoDB Schema Design is the process of deciding how data should be structured, stored, and connected inside MongoDB collections. Unlike traditional relational databases, MongoDB does not require a fixed schema, giving developers flexibility in how documents are designed. Good schema design focuses on application requirements, data relationships, read and write patterns, and document size. In this chapter, you will practice designing MongoDB documents using embedding, references, arrays, and practical database structures. MongoDB Schema Design practice questions with solutions to help you understand the concepts.

Q1. Design a Basic User Schema

Problem Statement

Create a users collection where each document stores a user’s basic information such as name, email, age, and city.

MongoDB Command / Query

db.users.insertOne({
  userId: 101,
  name: "Rahul Sharma",
  email: "rahul@example.com",
  age: 25,
  city: "Delhi"
})

Expected Output

{
  userId: 101,
  name: "Rahul Sharma",
  email: "rahul@example.com",
  age: 25,
  city: "Delhi"
}

Explanation

This is a simple document schema where related user information is stored together in one document.


Q2. Design an Embedded Address Schema

Problem Statement

Store a user’s address inside the user document because the address belongs directly to that user.

MongoDB Command / Query

db.users.insertOne({
  userId: 102,
  name: "Priya Verma",
  email: "priya@example.com",
  address: {
    street: "A-45 Sector 7",
    city: "Delhi",
    state: "Delhi",
    pincode: "110075"
  }
})

Expected Output

{
  userId: 102,
  name: "Priya Verma",
  email: "priya@example.com",
  address: {
    street: "A-45 Sector 7",
    city: "Delhi",
    state: "Delhi",
    pincode: "110075"
  }
}

Explanation

An embedded document is useful when the related information is usually accessed together with the parent document.


Q3. Design a Product with Embedded Reviews

Problem Statement

Create a product document that stores multiple customer reviews inside the product document.

MongoDB Command / Query

db.products.insertOne({
  productId: 201,
  name: "Laptop",
  price: 55000,
  reviews: [
    {
      user: "Rahul",
      rating: 5,
      comment: "Excellent laptop"
    },
    {
      user: "Priya",
      rating: 4,
      comment: "Good performance"
    }
  ]
})

Expected Output

{
  productId: 201,
  name: "Laptop",
  price: 55000,
  reviews: [
    {
      user: "Rahul",
      rating: 5,
      comment: "Excellent laptop"
    },
    {
      user: "Priya",
      rating: 4,
      comment: "Good performance"
    }
  ]
}

Explanation

Reviews can be embedded when they are closely related to the product and are normally displayed with the product.


Q4. Store Multiple Phone Numbers Using an Array

Problem Statement

Design a user schema that allows a user to have multiple phone numbers.

MongoDB Command / Query

db.users.insertOne({
  userId: 103,
  name: "Amit Kumar",
  phoneNumbers: [
    "9876543210",
    "9123456780"
  ]
})

Expected Output

{
  userId: 103,
  name: "Amit Kumar",
  phoneNumbers: [
    "9876543210",
    "9123456780"
  ]
}

Explanation

An array is suitable when a document has a small collection of related values.


Q5. Design an Order with Embedded Products

Problem Statement

Create an order schema containing customer information and multiple purchased products.

MongoDB Command / Query

db.orders.insertOne({
  orderId: 5001,
  customerId: 101,
  orderDate: "2026-09-15",
  products: [
    {
      productId: 201,
      name: "Laptop",
      quantity: 1,
      price: 55000
    },
    {
      productId: 202,
      name: "Mouse",
      quantity: 2,
      price: 800
    }
  ],
  totalAmount: 56600
})

Expected Output

{
  orderId: 5001,
  customerId: 101,
  products: [
    {
      productId: 201,
      name: "Laptop",
      quantity: 1,
      price: 55000
    },
    {
      productId: 202,
      name: "Mouse",
      quantity: 2,
      price: 800
    }
  ],
  totalAmount: 56600
}

Explanation

The products are embedded because an order normally needs to show the products purchased as part of that order.


Q6. Design a Schema Using References

Problem Statement

Create a students collection that stores course IDs instead of embedding complete course documents.

MongoDB Command / Query

db.students.insertOne({
  studentId: 101,
  name: "Rahul Sharma",
  courses: [1, 2]
})

db.courses.insertMany([
  {
    courseId: 1,
    courseName: "MongoDB"
  },
  {
    courseId: 2,
    courseName: "Python"
  }
])

Expected Output

Student document:

{
  studentId: 101,
  name: "Rahul Sharma",
  courses: [1, 2]
}

Course documents:

{
  courseId: 1,
  courseName: "MongoDB"
}

{
  courseId: 2,
  courseName: "Python"
}

Explanation

Instead of duplicating course information inside every student document, the student stores references to the courses.


Q7. Query an Embedded Field

Problem Statement

Find users whose address city is "Delhi".

MongoDB Command / Query

db.users.find({
  "address.city": "Delhi"
})

Expected Output

{
  userId: 102,
  name: "Priya Verma",
  address: {
    street: "A-45 Sector 7",
    city: "Delhi",
    state: "Delhi",
    pincode: "110075"
  }
}

Explanation

MongoDB uses dot notation to access fields inside embedded documents.


Q8. Decide Between Embedding and Referencing

Problem Statement

Suppose a company has employees and departments. Each employee belongs to one department, and department information is shared by many employees. Design the schema using references.

MongoDB Command / Query

db.departments.insertMany([
  {
    departmentId: 1,
    name: "IT"
  },
  {
    departmentId: 2,
    name: "HR"
  }
])

db.employees.insertMany([
  {
    employeeId: 101,
    name: "Rahul",
    departmentId: 1
  },
  {
    employeeId: 102,
    name: "Priya",
    departmentId: 2
  }
])

Expected Output

{
  employeeId: 101,
  name: "Rahul",
  departmentId: 1
}

Explanation

References are useful here because many employees can belong to the same department. Storing the department details inside every employee document would duplicate data.


Q9. Design a Blog Schema

Problem Statement

Create a blog post schema containing the title, author, content, tags, and comments.

MongoDB Command / Query

db.posts.insertOne({
  postId: 301,
  title: "MongoDB Schema Design",
  author: "Rahul",
  content: "MongoDB provides flexible document-based schema design.",
  tags: [
    "MongoDB",
    "Database",
    "NoSQL"
  ],
  comments: [
    {
      user: "Priya",
      comment: "Very useful article"
    },
    {
      user: "Amit",
      comment: "Good explanation"
    }
  ]
})

Expected Output

{
  postId: 301,
  title: "MongoDB Schema Design",
  author: "Rahul",
  content: "MongoDB provides flexible document-based schema design.",
  tags: [
    "MongoDB",
    "Database",
    "NoSQL"
  ],
  comments: [
    {
      user: "Priya",
      comment: "Very useful article"
    },
    {
      user: "Amit",
      comment: "Good explanation"
    }
  ]
}

Explanation

Tags and small numbers of comments can be embedded because they are closely connected to the post.


Q10. Design a Schema for a Large Collection of Comments

Problem Statement

A blog can receive thousands or millions of comments. Design the schema so comments are stored separately rather than embedding an unlimited number of comments inside the post document.

MongoDB Command / Query

db.posts.insertOne({
  postId: 401,
  title: "Learning MongoDB",
  author: "Rahul"
})

db.comments.insertOne({
  commentId: 9001,
  postId: 401,
  user: "Priya",
  comment: "Great tutorial",
  createdAt: "2026-09-15"
})

Expected Output

posts:

{
  postId: 401,
  title: "Learning MongoDB",
  author: "Rahul"
}

comments:

{
  commentId: 9001,
  postId: 401,
  user: "Priya",
  comment: "Great tutorial",
  createdAt: "2026-09-15"
}

Explanation

Embedding a very large or continuously growing array can make documents unnecessarily large. In such cases, storing comments in a separate collection and referencing the post is a better schema design.

Key Takeaways

  • MongoDB uses a flexible document-based schema.
  • Schema design should be based on how the application uses the data.
  • Embedding stores related information inside the parent document.
  • Referencing stores IDs that point to documents in another collection.
  • Arrays are useful for storing multiple related values.
  • Embedded documents can be queried using dot notation.
  • Embedding is useful when related data is usually accessed together.
  • References are useful when related data is shared by many documents.
  • Avoid unlimited or continuously growing arrays inside a document.
  • Good schema design should consider read patterns, write patterns, relationships, duplication, and document size.

FAQs

1. What is Schema Design in MongoDB?

Schema Design is the process of deciding how documents, fields, arrays, embedded documents, and references should be organized in MongoDB collections.

2. Does MongoDB require a fixed schema?

No. MongoDB has a flexible schema, so documents in the same collection can have different fields. However, a well-designed and consistent schema is still important for reliable applications.

3. What is embedding in MongoDB?

Embedding means storing related information directly inside a document.

For example:

{
  name: "Rahul",
  address: {
    city: "Delhi",
    pincode: "110075"
  }
}

4. What is referencing in MongoDB?

Referencing means storing the ID of a related document rather than storing the complete related document.

{
  name: "Rahul",
  departmentId: 1
}

5. When should you use embedding in MongoDB?

Embedding is useful when related data is small, closely connected to the parent document, and usually accessed together with that document.

6. When should you use references in MongoDB?

References are useful when data is shared between many documents, independently updated, or potentially very large or continuously growing.

7. Which is better in MongoDB: embedding or referencing?

Neither is always better. The choice depends on the application’s read and write patterns, relationship between the data, duplication requirements, and document size.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top