MongoDB Querying Nested Documents Practice Questions with Solutions

Introduction

MongoDB allows you to store documents inside other documents. These are called nested documents or embedded documents. Querying nested documents means finding data inside these embedded objects. In this chapter, you will practice accessing nested fields using dot notation, matching complete nested objects, and combining nested-field queries with comparison and logical operators. MongoDB Querying Nested Documents Practice questions with solutions to help you understand the concepts.

Q1. Find Students Living in Delhi

Problem Statement:
Suppose each student has an address nested document. Find all students whose city is Delhi.

MongoDB Query:

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

Expected Output:

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

Explanation:
Use dot notation "address.city" to access the city field inside the nested address document.


Q2. Find Students from a Specific Pincode

Problem Statement:
Find all students whose address has the pincode 110075.

MongoDB Query:

db.students.find({
  "address.pincode": 110075
})

Expected Output:

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

Explanation:
address.pincode accesses the nested pincode field.


Q3. Find Students Whose Address is in Delhi and Age is Above 15

Problem Statement:
Find students who live in Delhi and are older than 15.

MongoDB Query:

db.students.find({
  "address.city": "Delhi",
  age: { $gt: 15 }
})

Expected Output:

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

Explanation:
Multiple conditions can be used together. MongoDB treats these conditions as an AND operation.


Q4. Find Students Using a Nested Field with $gte

Problem Statement:
Find students whose address pincode is greater than or equal to 110075.

MongoDB Query:

db.students.find({
  "address.pincode": { $gte: 110075 }
})

Expected Output:

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

Explanation:
The $gte operator can also be used with nested fields.


Q5. Find Students from Delhi or Mumbai

Problem Statement:
Find students whose nested address.city is either Delhi or Mumbai.

MongoDB Query:

db.students.find({
  $or: [
    { "address.city": "Delhi" },
    { "address.city": "Mumbai" }
  ]
})

Expected Output:

{
  name: "Rahul",
  age: 16,
  address: {
    city: "Delhi",
    pincode: 110075
  }
}
{
  name: "Priya",
  age: 17,
  address: {
    city: "Mumbai",
    pincode: 400001
  }
}

Explanation:
$or allows you to check multiple possible values of a nested field.


Q6. Find Students Whose Nested Country is India

Problem Statement:
Suppose the address document contains another nested document called location. Find students whose country is India.

Example document:

{
  name: "Aman",
  address: {
    city: "Delhi",
    location: {
      country: "India"
    }
  }
}

MongoDB Query:

db.students.find({
  "address.location.country": "India"
})

Expected Output:

{
  name: "Aman",
  address: {
    city: "Delhi",
    location: {
      country: "India"
    }
  }
}

Explanation:
For multiple levels of nesting, continue using dot notation:

address → location → country

So the query becomes:

"address.location.country"

Q7. Find Employees from the IT Department

Problem Statement:
Suppose an employee document contains a nested department document. Find employees whose department name is IT.

Example:

{
  name: "Neha",
  department: {
    name: "IT",
    floor: 3
  }
}

MongoDB Query:

db.employees.find({
  "department.name": "IT"
})

Expected Output:

{
  name: "Neha",
  department: {
    name: "IT",
    floor: 3
  }
}

Explanation:
The query accesses the name field inside the department document.


Q8. Find Products with Nested Price Greater Than 1000

Problem Statement:
Suppose a product has a nested details document containing its price. Find products where the price is greater than 1000.

Example:

{
  name: "Laptop",
  details: {
    brand: "Dell",
    price: 55000
  }
}

MongoDB Query:

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

Expected Output:

{
  name: "Laptop",
  details: {
    brand: "Dell",
    price: 55000
  }
}

Explanation:
details.price accesses the nested price field, and $gt checks whether its value is greater than 1000.


Q9. Match a Complete Nested Document

Problem Statement:
Find students whose complete address document matches:

{
  city: "Delhi",
  pincode: 110075
}

MongoDB Query:

db.students.find({
  address: {
    city: "Delhi",
    pincode: 110075
  }
})

Expected Output:

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

Explanation:
Here, MongoDB is matching the nested document itself, rather than querying only one field inside it.

This is different from:

{ "address.city": "Delhi" }

The complete nested-document match can be sensitive to the contents and structure of the embedded document.


Q10. Find Students from Delhi and Show Only Their Name and Nested City

Problem Statement:
Find students whose nested city is Delhi, but display only their name and address.city.

MongoDB Query:

db.students.find(
  { "address.city": "Delhi" },
  {
    _id: 0,
    name: 1,
    "address.city": 1
  }
)

Expected Output:

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

Explanation:
The first object is the filter:

{ "address.city": "Delhi" }

The second object is the projection:

{
  _id: 0,
  name: 1,
  "address.city": 1
}

This lets you return selected fields from a nested document.

Key Takeaways

  • MongoDB supports nested/embedded documents.
  • Use dot notation to access nested fields.
  • Example: "address.city" accesses city inside address.
  • Multiple levels can be accessed with "address.location.country".
  • Nested fields can be used with operators such as $gt, $gte, $in, and $or.
  • You can query a complete nested document directly.
  • Projection can be used to display specific nested fields.
  • Nested documents are useful for keeping related information together.

FAQs

1. What is a nested document in MongoDB?

A nested document is a document stored inside another MongoDB document. For example:

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

Here, address is a nested document.

2. How do you query a nested document in MongoDB?

MongoDB commonly uses dot notation:

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

3. What is dot notation in MongoDB?

Dot notation is a way to access fields inside nested documents.

"address.city"

means access the city field inside the address document.

4. Can MongoDB query multiple levels of nested documents?

Yes. You can use multiple dots.

db.students.find({
  "address.location.country": "India"
})

5. Can comparison operators be used with nested fields?

Yes. For example:

db.students.find({
  "address.pincode": { $gte: 110075 }
})

6. Can I use $or with nested fields?

Yes. For example:

db.students.find({
  $or: [
    { "address.city": "Delhi" },
    { "address.city": "Mumbai" }
  ]
})

7. What is the difference between querying a nested field and matching a complete nested document?

A nested-field query checks a particular field:

{ "address.city": "Delhi" }

A complete nested-document query attempts to match the address document itself:

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

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

Scroll to Top