Lesson 7 / 22

Query Operators

Comparison and membership operators for precise filters.

The common operators

$eq, $ne, $gt, $gte, $lt, $lte compare values; $in / $nin check membership in a list.

$gt and $in in action

Operators go inside an object next to the field they apply to.

db.products.find({ price: { $gt: 500 } })
db.products.find({ category: { $in: ["books", "toys"] } })

Combining conditions

Multiple fields in one filter are implicitly ANDed. Use $or explicitly for alternatives.

db.products.find({ price: { $lt: 100 }, inStock: true })
db.products.find({ $or: [{ price: { $lt: 100 } }, { featured: true }] })

Quick check: Which operator checks if a field's value is in a given list?

  • $eq
  • $in
  • $gt
Answer

$in — $in matches if the field's value equals any item in the array.