Filter queries

Filter queries

A query filter describes conditions a document must satisfy. Multiple ordinary field conditions are combined with AND; $or lets any listed condition match.

In MongoDB, filtering documents means finding only those records that match certain criteria. This is done using the find() method along with query operators like $eq, $gt, $lt, $in, $and, and $or.

MongoDB's filtering power lies in these operators, which allow you to match values, ranges, conditions, and complex logical patterns.

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

    Build filters using comparisons, membership tests and logical conditions.

Inserting Sample Data

Let's start by inserting some documents into a students collection:


    db.students.insertMany([
      { name: "Alice", age: 22, grade: "A" },
      { name: "Bob", age: 25, grade: "B" },
      { name: "Charlie", age: 20, grade: "A" },
      { name: "David", age: 23, grade: "C" },
      { name: "Eva", age: 21, grade: "B" }
    ]);
    {
      acknowledged: true,
      insertedIds: [ObjectId("..."), ObjectId("..."), ...]
    }

1. Filtering with $eq (Equal)

The $eq operator matches documents where the field value is equal to a specified value.


    db.students.find({ grade: { $eq: "A" } });
    { name: "Alice", age: 22, grade: "A" }
    { name: "Charlie", age: 20, grade: "A" }

Explanation: This query finds students whose grade is exactly A.

2. Filtering with $gt and $lt (Greater Than / Less Than)

Example data reference: the following setup creates the records used below. If you already inserted them earlier in this lesson, do not insert them again.


    db.students.insertMany([
      { name: "Alice", age: 22, grade: "A" },
      { name: "Bob", age: 25, grade: "B" },
      { name: "Charlie", age: 20, grade: "A" },
      { name: "David", age: 23, grade: "C" },
      { name: "Eva", age: 21, grade: "B" }
    ]);

Use $gt (greater than) and $lt (less than) for numeric comparisons.


    db.students.find({ age: { $gt: 21 } });
    { name: "Alice", age: 22, grade: "A" }
    { name: "Bob", age: 25, grade: "B" }
    { name: "David", age: 23, grade: "C" }

Explanation: This query returns students who are older than 21.

Q: Can we combine $gt and $lt to find values in a range?

A: Yes! Use both inside the same object:


    db.students.find({ age: { $gt: 20, $lt: 24 } });
    { name: "Alice", age: 22, grade: "A" }
    { name: "David", age: 23, grade: "C" }

3. Filtering with $in (Match Any of Multiple Values)

Example data reference: the following setup creates the records used below. If you already inserted them earlier in this lesson, do not insert them again.


    db.students.insertMany([
      { name: "Alice", age: 22, grade: "A" },
      { name: "Bob", age: 25, grade: "B" },
      { name: "Charlie", age: 20, grade: "A" },
      { name: "David", age: 23, grade: "C" },
      { name: "Eva", age: 21, grade: "B" }
    ]);

The $in operator matches any value in a specified array.


    db.students.find({ grade: { $in: ["A", "B"] } });
    { name: "Alice", age: 22, grade: "A" }
    { name: "Bob", age: 25, grade: "B" }
    { name: "Charlie", age: 20, grade: "A" }
    { name: "Eva", age: 21, grade: "B" }

Explanation: This query finds all students whose grade is either A or B.

4. Using $and (All Conditions Must Match)

The $and operator allows combining multiple conditions.


    db.students.find({
      $and: [
        { grade: "A" },
        { age: { $lt: 22 } }
      ]
    });
    { name: "Charlie", age: 20, grade: "A" }

Explanation: This query returns students who have grade A and are younger than 22.

5. Using $or (Any One Condition Must Match)

Example data reference: the following setup creates the records used below. If you already inserted them earlier in this lesson, do not insert them again.


    db.students.insertMany([
      { name: "Alice", age: 22, grade: "A" },
      { name: "Bob", age: 25, grade: "B" },
      { name: "Charlie", age: 20, grade: "A" },
      { name: "David", age: 23, grade: "C" },
      { name: "Eva", age: 21, grade: "B" }
    ]);

The $or operator returns documents that match at least one condition.


    db.students.find({
      $or: [
        { age: { $lt: 21 } },
        { grade: "C" }
      ]
    });
    { name: "Charlie", age: 20, grade: "A" }
    { name: "Eva", age: 21, grade: "B" }
    { name: "David", age: 23, grade: "C" }

Explanation: This query returns students who are either younger than 21 or have grade C.

Combining Operators in a Query

You can combine $and, $or, and comparison operators for complex filtering.


    db.students.find({
      $and: [
        { grade: { $in: ["A", "B"] } },
        { age: { $gt: 21 } }
      ]
    });
    { name: "Alice", age: 22, grade: "A" }
    { name: "Bob", age: 25, grade: "B" }

Check your understanding

Which ages match { age: { $gt: 18, $lt: 21 } }?
Choose one answer. You can try again.

Answer and explanation

19 and 20

Both strict comparisons must hold, so the endpoints are excluded.

What to remember

A query filter describes conditions a document must satisfy. Multiple ordinary field conditions are combined with AND; $or lets any listed condition match.

Summary

  • $eq: Equal to
  • $gt / $lt: Greater than / Less than
  • $in: Value is in a list
  • $and: All conditions must match
  • $or: At least one condition must match

By combining these operators, you can write powerful and flexible queries to filter data just the way you need.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Update documents →