Lesson 7 / 23

Query Operators

Filter precisely with comparison and membership operators such as $gt, $in and $or.

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
  • $gt
  • $exists
  • $in
Answer

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