MongoDB comparison operators: $gt, $lt, $in
$gt, $lt, $gte, $lte, $ne, $in, $nin, $mod. Β· 9 practice questions Β· Cheat sheet β
Comparison operators turn equality matches into ranges and sets. $gt, $gte, $lt and $lte compare numbers, strings and dates; $ne excludes one value; $in and $nin match against a list; $mod picks every nth value. They sit inside the field's own object, so a $gt $lt query example for a range reads { seats: { $gte: 30, $lte: 60 } }: both bounds in one object, not two separate seats keys, because a second key would silently replace the first.
Two things catch people. Comparisons only match values of the same type, so the string "50" is never greater than the number 49. And $ne and $nin also match documents where the field is missing entirely, which is usually more than you wanted.
MongoDB comparison operator practice questions
- Find restaurants with more than 50
seats. easy restaurants - Find restaurants whose
seatsare between 30 and 60 inclusive. easy restaurants - Find restaurants whose
priceis not 1. easy restaurants - Find restaurants whose
cuisineis Indian, Chinese or Japanese. easy restaurants - Find restaurants that are NOT in Manhattan, Brooklyn or Queens. medium restaurants
- Find restaurants with no more than 20
seats. easy restaurants - Find restaurants whose
seatscount divides evenly by 10 (remainder 0). medium restaurants - Find movies released after 2015 with an
imdb.ratingof at least 7.5. medium movies - Find products priced under 100 that still have stock (
stockgreater than 0). Returnsku,priceandstockwithout_id. medium products
Each question is answered in a live mongo-shell editor on this page; the result is checked against a reference solution.
$gt, $lt, $in and $ne cheat sheet
The eight you actually use
| Operator | Meaning | Example |
|---|---|---|
$eq | equals (implicit) | { price: 2 } |
$ne | not equal | { cuisine: { $ne: "Pizza" } } |
$gt / $gte | greater / or equal | { seats: { $gt: 50 } } |
$lt / $lte | less / or equal | { seats: { $lte: 20 } } |
$in | matches any in list | { borough: { $in: ["Bronx","Queens"] } } |
$nin | matches none in list | { borough: { $nin: ["Bronx"] } } |
$mod | remainder test | { seats: { $mod: [10, 0] } } |
Ranges live in one object
// seats between 30 and 60, inclusive
db.restaurants.find({ seats: { $gte: 30, $lte: 60 } })
Two operators on the same field go in the same braces. Writing them as
two separate keys silently keeps only the last one.
Gotchas
$neand$ninalso match documents where the field is missing.- Comparisons are type-aware:
"5"is not5. - On an array field, the condition matches if any element satisfies it.
Common questions
How do I query a range in MongoDB?
{ price: { $gte: 2, $lte: 4 } }. Writing { price: { $gte: 2 }, price: { $lte: 4 } } keeps only the last key, because a JavaScript object cannot hold the same key twice, so the lower bound silently disappears.Why does $ne match documents where the field is missing?
$ne means "not equal to this value", and a missing field is not equal to anything, so it matches. To require the field as well, combine it with $exists: { coupon: { $ne: null, $exists: true } }. The same applies to $nin.What's the difference between $in and $or?
$in tests one field against a list of values and is shorter and faster: { borough: { $in: ["Bronx", "Queens"] } }. $or combines arbitrary conditions, possibly on different fields. Reach for $or only when $in cannot express the condition.Continue:β Finding & ProjectionLogical Operators β
MongoDB topics:Finding & ProjectionLogical OperatorsElement & TypePattern MatchingSort, Limit & SkipArray QueriesEmbedded DocumentsAggregation BasicsGrouping & Accumulators$unwind & Array PipelinesJoins & Advanced StagesFull cheat sheet