Back to Interview PrepGet interview ready with AILearn each topic the way interviewers ask it, then answer like you would in the interview.
Course progress0/12 chapters · 0%

Chapter 4 of 13

~13 min

Query Operators

MongoDB query filters go far beyond simple equality matches. Comparison, logical, element, and array operators let you express rich conditions directly inside the filter object, without writing a separate query language string.

Comparison Operators

Operators like `$gt` (greater than), `$gte` (greater than or equal), `$lt` (less than), `$lte` (less than or equal), `$ne` (not equal), and `$in` (matches any value in a given array) let you express ranges and set membership directly. They are placed as the value for a field, replacing a plain equality check.

queries.js
db.products.find({ price: { $gte: 10, $lte: 50 } })

db.products.find({ category: { $in: ["electronics", "books"] } })

db.products.find({ stock: { $ne: 0 } })
Range and set-membership queries

Logical Operators

`$and`, `$or`, and `$nor` combine multiple conditions, taking an array of query clauses. `$not` inverts a single condition. Note that MongoDB implicitly ANDs top-level fields already (listing two fields in one filter object means both must match), so `$and` is mainly needed when you must repeat the same field name with different conditions, or want to nest logical operators.

queries.js
db.products.find({
  $or: [
    { category: "electronics" },
    { price: { $lt: 20 } }
  ]
})

db.products.find({
  $and: [
    { price: { $gt: 10 } },
    { price: { $lt: 100 } }
  ]
})
Combining conditions with $or and $and

Element Operators

`$exists` checks whether a field is present (or absent) on a document at all, regardless of its value - useful for the flexible-schema scenarios where not every document has every field. `$type` checks that a field's BSON type matches (or does not match) a given type, which is handy when a field has been stored inconsistently across documents.

queries.js
db.users.find({ phone: { $exists: true } })

db.users.find({ age: { $type: "number" } })
Checking for field presence and type

Array Operators: $all and $elemMatch

`$all` matches documents where an array field contains every value in the given list (in any order), while a plain equality check on an array only matches an exact array. `$elemMatch` matches documents where at least one element of an array field satisfies all of the specified conditions simultaneously - critical when an array holds sub-documents and you need multiple conditions to apply to the same element, not just to the array as a whole.

queries.js
db.posts.find({ tags: { $all: ["mongodb", "nosql"] } })

// Without $elemMatch, this would match a document even if
// one review has score 5 and a DIFFERENT review has "great",
// which is not what we usually want.
db.posts.find({
  reviews: {
    $elemMatch: { score: { $gte: 4 }, comment: "great" }
  }
})
Matching within array fields

Coding challenge

Given a "products" collection, write a query that finds documents where the category is either "books" or "toys", the price is less than 25, and the "discontinued" field does not exist.

// Write your solution here

Answer every question below to mark this chapter as complete.

Quiz Yourself

Score: 0/4

1. What does { price: { $in: [10, 20, 30] } } match?

2. Why would you need $and explicitly, given that MongoDB already ANDs top-level fields in a filter?

3. What is the key difference $elemMatch adds when querying an array of sub-documents?

4. What does $exists: false check for?

You're viewing a free lesson. Sign up free to track your progress and unlock the full course.Sign up freeSign in