All academies
Roadmap
LEVEL 4 · QUERY OPERATORS

Comparison & Logical Operators

12 min read

Comparison operators go inside the field's value: $eq, $ne, $gt, $gte, $lt, $lte, $in, $nin.

mongosh
db.products.find({ price: { $gte: 30, $lte: 100 } })

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

db.orders.find({ status: { $ne: "cancelled" } })

Logical operators

$and, $or, $nor take arrays of conditions; $not negates one condition. Conditions on *different* fields are implicitly ANDed, so you only need $and when combining conditions on the *same* field.

mongosh
db.products.find({
  $or: [
    { price: { $lt: 20 } },
    { rating: { $gte: 4.8 } }
  ]
})

// implicit AND
db.products.find({ category: "electronics", stock: { $gt: 50 } })

Try it yourself

db.products.find({ price: { $gte: 30, $lte: 100 } })
Run in Shell

Check your understanding

Q1.

Which query finds products priced between 10 and 50 inclusive?

Q2.

Docs: {a:1,b:1}, {a:1,b:2}, {a:2,b:2}. What matches?

code
db.c.find({ a: 1, b: 2 })
Q3.

$in can match any value in an array of candidates, e.g. { status: { $in: ['a', 'b'] } }.