Lesson 22 of 50 – Embedded Documents in MongoDB
44%

Embedded Documents in MongoDB

Embedded documents allow us to store one document inside another document. They are useful when related information belongs together, such as a student's address, an employee's contact details, or a product's specifications.

1. What Is an Embedded Document?

An embedded document is a document stored as the value of a field inside another document. It uses curly braces {} to represent its fields.

Example
{
    name: "Rahul",
    age: 20,
    address: {
        city: "Patna",
        state: "Bihar",
        pincode: 800001
    }
}

Here, address is an embedded document containing the fields city, state, and pincode.

The main document stores the student's name and age, while the embedded document stores the address details.

2. Why Use Embedded Documents?

Embedded documents are useful when related information should be stored together.

  • They group related fields into one document.
  • They can reduce the need for separate collections and extra queries.
  • They allow related information to be retrieved with the parent document.
  • They support nested structures.
  • They are useful for addresses, contact details, and product specifications.
Example: Employee Information
{
    name: "Amit",
    department: "IT",
    contact: {
        email: "amit@example.com",
        phone: "9876543210"
    }
}

The employee's contact information is grouped inside the contact field.

3. Insert Documents with Embedded Documents

We can insert embedded documents using insertOne() or insertMany().

Example: Insert a Student
db.embedded_students.insertOne({
    name: "Rahul",
    age: 20,
    course: "Java",
    address: {
        city: "Patna",
        state: "Bihar",
        pincode: 800001
    }
})
Example: Insert Multiple Students
db.embedded_students.insertMany([
    {
        name: "Priya",
        course: "Python",
        address: {
            city: "Gaya",
            state: "Bihar"
        }
    },
    {
        name: "Aman",
        course: "JavaScript",
        address: {
            city: "Aurangabad",
            state: "Bihar"
        }
    }
])

Each student document can contain its own embedded address document.

4. Access Embedded Fields Using Dot Notation

MongoDB uses dot notation to access fields inside an embedded document. Put the complete field path inside quotation marks in a query.

Example: Find Students from Patna
db.embedded_students.find({
    "address.city": "Patna"
})

This finds documents whose embedded address has the city value "Patna".

Example: Find Students from Bihar
db.embedded_students.find({
    "address.state": "Bihar"
})
Example: Find Students with a Specific Pincode
db.embedded_students.find({
    "address.pincode": 800001
})

Dot notation lets us query a nested field without matching the entire embedded document.

5. Match an Entire Embedded Document

MongoDB can match an entire embedded document when the complete embedded value matches, including its field values and field order.

Example Document
{
    name: "Rahul",
    address: {
        city: "Patna",
        state: "Bihar"
    }
}
Example Query
db.embedded_students.find({
    address: {
        city: "Patna",
        state: "Bihar"
    }
})

This matches the specified embedded document as a whole. If the address contains additional fields, or the fields appear in a different order, this exact embedded-document match may not succeed.

When you only need to check particular nested fields, dot notation is generally more flexible.

6. Update Fields Inside an Embedded Document

We can update a nested field using dot notation with the $set operator.

Example: Change a Student's City
db.embedded_students.updateOne(
    { name: "Rahul" },
    {
        $set: {
            "address.city": "Delhi"
        }
    }
)

This changes the city field without replacing the entire address document.

Example: Add a Landmark Field
db.embedded_students.updateOne(
    { name: "Rahul" },
    {
        $set: {
            "address.landmark": "Main Market"
        }
    }
)

If the address document exists, this adds the landmark field to it.

Verify the Updated Document
db.embedded_students.findOne({
    name: "Rahul"
})

7. Projection with Embedded Documents

Projection allows us to display selected nested fields from an embedded document.

Example: Display Name and City
db.embedded_students.find(
    {},
    {
        _id: 0,
        name: 1,
        "address.city": 1
    }
)

This displays each student's name and city while excluding the _id field.

Example: Display Only the Address
db.embedded_students.find(
    {},
    {
        _id: 0,
        address: 1
    }
)

This returns the complete embedded address document without the other top-level fields.

8. Embedded Documents Inside Arrays

MongoDB can store arrays of embedded documents. This is useful when one record contains multiple related items, such as marks for different subjects.

Example
{
    name: "Priya",
    results: [
        {
            subject: "Java",
            marks: 85
        },
        {
            subject: "Python",
            marks: 92
        }
    ]
}

The results field is an array, and each element is an embedded document.

Find Students with Python Marks of 90 or More
db.embedded_students.find({
    results: {
        $elemMatch: {
            subject: "Python",
            marks: { $gte: 90 }
        }
    }
})

The $elemMatch operator ensures that both conditions match the same element in the results array.

9. Embedded Documents vs Separate Collections

Embedded Documents Separate Collections
Related data is stored inside a parent document. Related data is stored in separate documents or collections.
Often convenient for data read together. Useful when data is independently managed or shared.
Can reduce the need for additional queries. May require additional queries or aggregation with $lookup.
Suitable for bounded, closely related information. Suitable for large or independently growing related data.

Choose the design based on how the application reads, updates, and manages the data. Embedding is not always the best choice, especially for very large or unbounded arrays.

10. Important Points to Remember

  • An embedded document is stored inside another document.
  • Use curly braces {} for embedded documents.
  • Use dot notation, such as "address.city", to query nested fields.
  • Use $set with dot notation to update a nested field.
  • Projection can select nested fields.
  • Arrays can contain embedded documents.
  • $elemMatch can require multiple conditions to match the same array element.
  • Choose between embedding and separate collections according to your application's data needs.

11. Practical Assignment

Create a collection named practice_embedded and insert these documents:

db.practice_embedded.insertMany([
    {
        _id: 1,
        name: "Rahul",
        age: 22,
        address: {
            city: "Patna",
            state: "Bihar"
        },
        course: "Java"
    },
    {
        _id: 2,
        name: "Priya",
        age: 19,
        address: {
            city: "Gaya",
            state: "Bihar"
        },
        course: "Python"
    },
    {
        _id: 3,
        name: "Aman",
        age: 21,
        address: {
            city: "Delhi",
            state: "Delhi"
        },
        course: "JavaScript"
    }
])
Complete these tasks:
  1. Find students who live in Patna.
  2. Find students whose address state is Bihar.
  3. Display only each student's name and city.
  4. Change Rahul's city from Patna to Delhi.
  5. Add a landmark field to Priya's address.
  6. Display only the address field for every student.
  7. Insert a new student with an embedded address document.
  8. Explain the difference between dot notation and matching an entire embedded document.

Run the insertion command only once, or clear the test collection before inserting the same sample data again.

🧠 Quick Quiz

1. What is an embedded document?




2. Which notation queries the city field inside address?




3. Which operator updates a nested field?