Introduction
MongoDB allows you to store one document inside another document. This is called an embedded document or nested document. Embedded documents are useful when related information belongs together, such as a student’s address, an employee’s department, or a product’s specifications. In this chapter, you will practice creating, finding, updating, and projecting embedded documents using practical MongoDB queries. MongoDB Embedded Documents practice questions with solutions to help you understand the concepts.
Q1. Insert a Student with an Embedded Address
Problem Statement:
Create a student document containing an embedded address document with city and pincode.
MongoDB Query:
db.students.insertOne({
name: "Rahul",
age: 16,
course: "Python",
address: {
city: "Delhi",
pincode: 110075
}
})
Expected Output:
{
acknowledged: true,
insertedId: ObjectId("...")
}
Explanation:
The address field contains another document:
{
city: "Delhi",
pincode: 110075
}
This is an embedded document.
Q2. Insert a Product with Embedded Specifications
Problem Statement:
Insert a product with an embedded specifications document containing brand, RAM, and storage.
MongoDB Query:
db.products.insertOne({
name: "Laptop",
price: 55000,
specifications: {
brand: "Dell",
ram: "8GB",
storage: "512GB SSD"
}
})
Expected Output:
{
acknowledged: true,
insertedId: ObjectId("...")
}
Explanation:
The specifications field contains related product information inside the main product document.
Q3. Find Students from Delhi
Problem Statement:
Find all students whose embedded address.city is "Delhi".
MongoDB Query:
db.students.find({
"address.city": "Delhi"
})
Expected Output:
{
name: "Rahul",
age: 16,
course: "Python",
address: {
city: "Delhi",
pincode: 110075
}
}
Explanation:
MongoDB uses dot notation to access fields inside embedded documents.
address → city
becomes:
"address.city"
Q4. Find Products with 8GB RAM
Problem Statement:
Find all products whose embedded specifications contain 8GB RAM.
MongoDB Query:
db.products.find({
"specifications.ram": "8GB"
})
Expected Output:
{
name: "Laptop",
price: 55000,
specifications: {
brand: "Dell",
ram: "8GB",
storage: "512GB SSD"
}
}
Explanation:
The query accesses the ram field inside the embedded specifications document.
Q5. Find Employees in the IT Department
Problem Statement:
Suppose an employee has an embedded department document. Find employees whose department name is "IT".
Example document:
{
name: "Neha",
salary: 45000,
department: {
name: "IT",
floor: 3
}
}
MongoDB Query:
db.employees.find({
"department.name": "IT"
})
Expected Output:
{
name: "Neha",
salary: 45000,
department: {
name: "IT",
floor: 3
}
}
Explanation:department.name accesses the name field inside the embedded department document.
Q6. Update a Field Inside an Embedded Document
Problem Statement:
Change Rahul’s address pincode from 110075 to 110076.
MongoDB Query:
db.students.updateOne(
{ name: "Rahul" },
{
$set: {
"address.pincode": 110076
}
}
)
Expected Output:
{
acknowledged: true,
matchedCount: 1,
modifiedCount: 1
}
Explanation:
Dot notation can also be used when updating embedded documents.
"address.pincode": 110076
changes only the pincode field without replacing the complete address document.
Q7. Add a New Field to an Embedded Document
Problem Statement:
Add a state field with the value "Delhi" to Rahul’s embedded address document.
MongoDB Query:
db.students.updateOne(
{ name: "Rahul" },
{
$set: {
"address.state": "Delhi"
}
}
)
Expected Output:
{
acknowledged: true,
matchedCount: 1,
modifiedCount: 1
}
Explanation:
If the nested field does not already exist, $set creates it.
The address can become:
address: {
city: "Delhi",
pincode: 110076,
state: "Delhi"
}
Q8. Display Only the Embedded Address
Problem Statement:
Display the student’s name and complete address document while hiding all other fields.
MongoDB Query:
db.students.find(
{},
{
_id: 0,
name: 1,
address: 1
}
)
Expected Output:
{
name: "Rahul",
address: {
city: "Delhi",
pincode: 110076,
state: "Delhi"
}
}
Explanation:
Projection is used to return only the required fields.
Here:
name: 1
includes the student’s name, while:
address: 1
includes the complete embedded document.
Q9. Find Products with Storage Greater Than a Specific Value
Problem Statement:
Suppose storageGB is stored as a number inside an embedded specifications document. Find products with more than 256GB of storage.
Example:
{
name: "Laptop",
specifications: {
brand: "Dell",
ramGB: 8,
storageGB: 512
}
}
MongoDB Query:
db.products.find({
"specifications.storageGB": {
$gt: 256
}
})
Expected Output:
{
name: "Laptop",
specifications: {
brand: "Dell",
ramGB: 8,
storageGB: 512
}
}
Explanation:
Comparison operators such as $gt can be used with fields inside embedded documents.
Q10. Find and Update an Embedded Document Using Multiple Conditions
Problem Statement:
Find Rahul’s document where the embedded address city is Delhi, then update its pincode to 110077.
MongoDB Query:
db.students.updateOne(
{
name: "Rahul",
"address.city": "Delhi"
},
{
$set: {
"address.pincode": 110077
}
}
)
Expected Output:
{
acknowledged: true,
matchedCount: 1,
modifiedCount: 1
}
Explanation:
The filter checks two conditions:
name: "Rahul"
and:
"address.city": "Delhi"
Then $set updates only the nested pincode.
Key Takeaways
- An embedded document is a document stored inside another MongoDB document.
- Embedded documents are also commonly called nested documents.
- Dot notation is used to access embedded fields.
- Example:
"address.city". - Embedded documents can contain multiple levels of nesting.
- You can query embedded fields using operators such as
$gt,$gte,$in, and$or. $setcan update individual fields inside an embedded document.$setcan also add a new nested field.- Projection can return an entire embedded document or selected nested fields.
- Embedding is useful when related information naturally belongs to the same document.
FAQs
1. What is an embedded document in MongoDB?
An embedded document is a document stored as a field inside another MongoDB document.
Example:
{
name: "Rahul",
address: {
city: "Delhi",
pincode: 110075
}
}
Here, address is an embedded document.
2. What is the difference between an embedded document and a normal field?
A normal field usually stores a single value:
{
name: "Rahul"
}
An embedded document stores another object:
{
name: "Rahul",
address: {
city: "Delhi"
}
}
3. How do you query an embedded document in MongoDB?
Use dot notation:
db.students.find({
"address.city": "Delhi"
})
This searches the city field inside the address document.
4. Can you update an embedded document in MongoDB?
Yes. You can update a specific nested field using $set:
db.students.updateOne(
{ name: "Rahul" },
{
$set: {
"address.city": "Mumbai"
}
}
)
5. Can an embedded document contain another embedded document?
Yes. MongoDB supports multiple levels of nesting.
For example:
{
address: {
location: {
country: "India"
}
}
}
You can query it using:
"address.location.country"
6. Can comparison operators be used with embedded documents?
Yes. For example:
db.products.find({
"specifications.storageGB": {
$gt: 256
}
})
7. Why are embedded documents useful in MongoDB?
Embedded documents keep related information together in a single document. This can make common queries simpler and can allow related data to be retrieved together without requiring a separate collection lookup.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
