Query nested fields

Query nested fields

Quoted dot notation follows fields inside documents. Use $elemMatch when several conditions must apply to the same element of an array of documents.

In MongoDB, documents can contain nested objects (also called embedded documents). To query such data, you use a concept called dot notation.

This allows you to target specific fields inside a nested document or even elements inside an array of documents.

Before working through the examples, make sure you can connect with mongosh and select your database. Use a practice database for writes and deletions.

  1. By the end of this lesson

    Write a nested-field filter and distinguish cross-element matches from same-element matches.

Understanding Nested Fields

Let’s say you have a collection called employees and each document contains an embedded address object:


    db.employees.insertMany([
      {
        name: "Alice",
        age: 28,
        address: {
          city: "New York",
          zip: "10001"
        }
      },
      {
        name: "Bob",
        age: 35,
        address: {
          city: "San Francisco",
          zip: "94105"
        }
      }
    ]);

Q: How can we find all employees living in New York?

A: We can use dot notation to query the nested address.city field.

Querying a Nested Field


    db.employees.find({ "address.city": "New York" });
    {
      _id: ObjectId("..."),
      name: "Alice",
      age: 28,
      address: { city: "New York", zip: "10001" }
    }

Explanation: Using "address.city", we told MongoDB to look inside the address object and match the city field value.

Querying Nested Fields with Conditions

Let’s query for employees whose ZIP code is greater than "90000":


    db.employees.find({ "address.zip": { $gt: "90000" } });
    {
      _id: ObjectId("..."),
      name: "Bob",
      age: 35,
      address: { city: "San Francisco", zip: "94105" }
    }

Q: Can I use comparison operators like $gt and $lt on nested fields?

A: Yes! You can use them just like on top-level fields.

Querying Arrays of Nested Documents

Consider a new collection called students, where each student has an array of grades, each with a subject and score:


    db.students.insertMany([
      {
        name: "John",
        grades: [
          { subject: "Math", score: 85 },
          { subject: "English", score: 78 }
        ]
      },
      {
        name: "Sara",
        grades: [
          { subject: "Math", score: 92 },
          { subject: "Science", score: 88 }
        ]
      }
    ]);

Query a Nested Field Inside an Array

Let’s find students who scored more than 90 in Math:


    db.students.find({ "grades.subject": "Math", "grades.score": { $gt: 90 } });
    {
      _id: ObjectId("..."),
      name: "Sara",
      grades: [
        { subject: "Math", score: 92 },
        { subject: "Science", score: 88 }
      ]
    }

Explanation: This query checks all grades documents inside the array to find one where subject is "Math" and score is greater than 90. MongoDB automatically traverses the array for matching objects.

Using $elemMatch for Precision

The previous query may return a document where subject and score match in different array elements. To ensure both conditions apply to the same array element, use $elemMatch:


    db.students.find({
      grades: { $elemMatch: { subject: "Math", score: { $gt: 90 } } }
    });
    {
      _id: ObjectId("..."),
      name: "Sara",
      grades: [
        { subject: "Math", score: 92 },
        { subject: "Science", score: 88 }
      ]
    }

Explanation: $elemMatch ensures that both subject: "Math" and score: { $gt: 90 } are part of the same object inside the grades array.

Check your understanding

A student has scores [{subject:"Math",mark:40},{subject:"Art",mark:90}]. How do you require Math and a mark above 80 in the same element?
Choose one answer. You can try again.

Answer and explanation

Use $elemMatch on scores with both conditions.

$elemMatch requires one array element to meet all its contained conditions.

What to remember

Quoted dot notation follows fields inside documents. Use $elemMatch when several conditions must apply to the same element of an array of documents.

Recap: Dot Notation and Nested Queries

  • Use dot notation (e.g., "address.city") to access nested fields.
  • Dot notation works on objects and arrays of objects.
  • Use $elemMatch for matching multiple conditions in a single object within an array.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Pagination →