πŸƒDBQuestpractice MongoDB queries
0 / 110

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

  1. Find restaurants with more than 50 seats. easy restaurants
  2. Find restaurants whose seats are between 30 and 60 inclusive. easy restaurants
  3. Find restaurants whose price is not 1. easy restaurants
  4. Find restaurants whose cuisine is Indian, Chinese or Japanese. easy restaurants
  5. Find restaurants that are NOT in Manhattan, Brooklyn or Queens. medium restaurants
  6. Find restaurants with no more than 20 seats. easy restaurants
  7. Find restaurants whose seats count divides evenly by 10 (remainder 0). medium restaurants
  8. Find movies released after 2015 with an imdb.rating of at least 7.5. medium movies
  9. Find products priced under 100 that still have stock (stock greater than 0). Return sku, price and stock without _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

OperatorMeaningExample
$eqequals (implicit){ price: 2 }
$nenot equal{ cuisine: { $ne: "Pizza" } }
$gt / $gtegreater / or equal{ seats: { $gt: 50 } }
$lt / $lteless / or equal{ seats: { $lte: 20 } }
$inmatches any in list{ borough: { $in: ["Bronx","Queens"] } }
$ninmatches none in list{ borough: { $nin: ["Bronx"] } }
$modremainder 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

  • $ne and $nin also match documents where the field is missing.
  • Comparisons are type-aware: "5" is not 5.
  • On an array field, the condition matches if any element satisfies it.

Common questions

How do I query a range in MongoDB?

Put both bounds in the same object: { 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