MongoDB — Comparison Operators
Learn how to filter data using MongoDB comparison operators: $eq, $ne, $gt, $gte, $lt, $lte, $in, and $nin.
1Comparison Operators Syntax Reference
| Operator | Meaning | Example Code |
|---|---|---|
$eq | Equal to | db.products.find({ price: { $eq: 99.99 } }) |
$ne | Not equal to | db.users.find({ status: { $ne: "banned" } }) |
$gt | Greater than | db.products.find({ price: { $gt: 100 } }) |
$gte | Greater than or equal | db.users.find({ age: { $gte: 18 } }) |
$lt | Less than | db.products.find({ price: { $lt: 50 } }) |
$lte | Less than or equal | db.scores.find({ score: { $lte: 35 } }) |
$in | Matches any value in array | db.users.find({ role: { $in: ["admin", "editor"] } }) |
$nin | Matches none of values in array | db.users.find({ role: { $nin: ["guest", "blocked"] } }) |
2Combining Comparison Operators for Ranges
Range Query Example
// Find products with price between $50 and $200 (inclusive)
db.products.find({
price: { $gte: 50, $lte: 200 }
});