TExcelTAD
Home › MongoDB › MongoDB Query Operators

MongoDB Query Operators

Equality filters only get you so far. Query operators let you compare, combine conditions and test values. They always start with $ and sit inside the value of the field they apply to: { field: { $operator: value } }.

Comparison operators

Operator Matches values that are…
$eq equal to the value (the same as a plain { field: value })
$ne not equal (also matches documents without the field)
$gt / $gte greater than / greater than or equal
$lt / $lte less than / less than or equal
$in equal to any value in an array
$nin equal to none of the values in an array
mongosh · try it yourself
const names = (cursor) => cursor.toArray().map(d => d.name);

print(names(db.products.find({ price: { $gt: 50 } })));
print(names(db.products.find({ price: { $gte: 10, $lte: 12 } })));
print(names(db.products.find({ category: { $in: ['merch', 'snacks'] }, stock: { $ne: 0 } })));
[ 'Burr Grinder', 'Gooseneck Kettle' ]
[ 'Green Sencha', 'Masala Chai' ]
[
  'Almond Biscotti',
  'Dark Chocolate Bar',
  'Salted Pretzels',
  'Travel Mug'
]

Putting two operators on the same field, as in { $gte: 10, $lte: 12 }, gives you a range.

Dates compare the same way. Here are the orders placed in May 2026:

mongosh · try it yourself
const may = db.orders.find({
  date: { $gte: ISODate('2026-05-01T00:00:00Z'), $lt: ISODate('2026-06-01T00:00:00Z') }
}, { date: 1, total: 1 }).toArray();
may.forEach(o => print(o._id, o.date.toISOString().slice(0, 10), o.total));
1014 2026-05-02 45.5
1015 2026-05-07 121
1016 2026-05-13 73.5
1017 2026-05-21 30
1018 2026-05-26 83

Tip: For date ranges use an inclusive start and an exclusive end ($gte first day, $lt first day of the next period). You never have to worry about the last second of the month.

Logical operators

Operator Meaning
$and: [ … ] all conditions are true (usually implicit)
$or: [ … ] at least one condition is true
$nor: [ … ] none of the conditions is true
$not: { … } inverts an operator expression on one field

Fields listed side by side in a filter are already ANDed. You need $or for alternatives:

mongosh · try it yourself
const cheapOrTopRated = db.products.find({
  $or: [{ price: { $lt: 5 } }, { rating: { $gte: 4.8 } }]
}, { _id: 0, name: 1, price: 1, rating: 1 }).toArray();
print(cheapOrTopRated);
[
  { name: 'Ethiopia Yirgacheffe', price: 27, rating: 4.9 },
  { name: 'Pour-Over Dripper', price: 29, rating: 4.8 },
  { name: 'Dark Chocolate Bar', price: 4.5, rating: 4.8 },
  { name: 'Salted Pretzels', price: 3.5, rating: 3.8 }
]

Explicit $and is needed when you combine two $or clauses, because a JavaScript object can't have the key $or twice:

mongosh · try it yourself
const filter = {
  $and: [
    { $or: [{ tier: 'gold' }, { tier: 'silver' }] },
    { $or: [{ 'address.country': 'UK' }, { 'address.city': 'Boston' }] }
  ]
};
print(db.customers.find(filter).toArray().map(c => c.name));
[
  'Ada Lovelace',
  'Alan Turing',
  'Margaret Hamilton',
  'Barbara Liskov'
]

Element operators

$exists checks whether a field is present; $type checks its BSON type:

mongosh · try it yourself
print(db.customers.find({ email: { $exists: false } }).toArray().map(c => c.name));
print(db.products.countDocuments({ specs: { $exists: true } }));
[ 'Linus Torvalds', 'Donald Knuth', 'Radia Perlman' ]
5

Note: { email: null } matches documents where email is null or missing. Use $exists when you need to tell the two apart.

Evaluation operators

Operator Purpose
$regex match strings against a regular expression
$expr use aggregation expressions, for example to compare two fields
$mod [divisor, remainder] arithmetic test
$text full-text search (Text Indexes)
$jsonSchema validate against a JSON Schema (Schema Validation)
mongosh · try it yourself
// names starting with "C", case-insensitive
print(db.products.find({ name: { $regex: '^c', $options: 'i' } }).toArray().map(p => p.name));
// JavaScript regex literals work too
print(db.customers.countDocuments({ email: /@example\.com$/ }));
// $expr compares two fields of the same document: fewer units in stock than the price in dollars
print(db.products.find({ $expr: { $lt: ['$stock', '$price'] } }, { _id: 0, name: 1, stock: 1, price: 1 }).toArray());
[ 'Colombia Supremo', 'Chamomile Calm' ]
9
[
  { name: 'Ethiopia Yirgacheffe', price: 27, stock: 12 },
  { name: 'Colombia Supremo', price: 21, stock: 0 },
  { name: 'Chamomile Calm', price: 8, stock: 0 },
  { name: 'Pour-Over Dripper', price: 29, stock: 15 },
  { name: 'French Press', price: 35, stock: 9 },
  { name: 'Burr Grinder', price: 89, stock: 6 },
  { name: 'Gooseneck Kettle', price: 59, stock: 4 },
  { name: 'Milk Frother', price: 25, stock: 0 },
  { name: 'Oat Cookies', price: 5, stock: 0 }
]

$not and $nin

$not wraps an operator expression for one field. Like $ne and $nin, it also matches documents where the field is missing:

mongosh · try it yourself
print(db.products.find({ price: { $not: { $gte: 10 } } }).toArray().map(p => p.name));
print(db.orders.find({ status: { $nin: ['delivered', 'cancelled'] } }).toArray().map(o => o._id));
[
  'Earl Grey',
  'Chamomile Calm',
  'Paper Filters (100)',
  'Almond Biscotti',
  'Dark Chocolate Bar',
  'Oat Cookies',
  'Salted Pretzels'
]
[
  1010, 1012, 1013,
  1015, 1016, 1018,
  1019, 1020
]

Test yourself

Which filter finds products priced from 10 up to, but not including, 20?

  1. { price: { $gt: 10, $lt: 20 } }
  2. { price: { $gte: 10, $lt: 20 } }
  3. { price: { $in: [10, 20] } }
  4. { $or: [{ price: 10 }, { price: 20 }] }

Why does { email: { $ne: 'ada@example.com' } } return customers without any email?

  1. It's a bug
  2. $ne also matches documents where the field is missing
  3. Missing fields are treated as empty strings
  4. It doesn't

Which operator lets you compare two fields of the same document?

  1. $where
  2. $cmp
  3. $expr
  4. $eq