Comparison Operators
MongoDB query operators let you express complex conditions in the filter document. Comparison operators compare a field's value against a specified value. They all follow the syntax: { field: { $operator: value } }.
Overview of Comparison Operators
Operator | Meaning | SQL Equivalent |
|---|---|---|
$eq | Equal to | field = value |
$ne | Not equal to | field != value |
$gt | Greater than | field > value |
$gte | Greater than or equal to | field >= value |
$lt | Less than | field < value |
$lte | Less than or equal to | field <= value |
$in | Matches any value in array | field IN (...) |
$nin | Matches none of the values in array | field NOT IN (...) |
$eq — Equal To
$eq matches documents where the field equals the specified value. The shorthand { field: value } is equivalent and preferred for readability.
$eq examples
const users = db.collection('users');
// Explicit $eq form
await users.find({ role: { $eq: 'admin' } }).toArray();
// Shorthand form — identical result, preferred style
await users.find({ role: 'admin' }).toArray();
// String match
await users.find({ status: { $eq: 'active' } }).toArray();
// Boolean match
await users.find({ verified: { $eq: true } }).toArray();
// Shorthand:
await users.find({ verified: true }).toArray();
// Match by ObjectId
const { ObjectId } = require('mongodb');
await users.find({ _id: { $eq: new ObjectId('64a1f2c3d4e5f6a7b8c9d0e1') } }).toArray();$ne — Not Equal To
$ne matches documents where the field value does not equal the specified value.
$ne examples
// Find all users who are not admins
await users.find({ role: { $ne: 'admin' } }).toArray();
// Find products that are not out of stock
await db.collection('products').find({ status: { $ne: 'outOfStock' } }).toArray();
// Find documents where a field is not null
await users.find({ profilePhoto: { $ne: null } }).toArray();$gt and $gte — Greater Than
$gt matches values strictly greater than the specified value. $gte includes the boundary value (greater than or equal).
$gt and $gte examples
// Products priced above $100
await db.collection('products').find({ price: { $gt: 100 } }).toArray();
// Users who are 18 or older
await users.find({ age: { $gte: 18 } }).toArray();
// Documents created after a specific date
const launchDate = new Date('2024-01-01');
await db.collection('posts').find({ createdAt: { $gt: launchDate } }).toArray();
// Scores strictly better than the passing threshold
await db.collection('results').find({ score: { $gt: 59 } }).toArray();$lt and $lte — Less Than
$lt matches values strictly less than the specified value. $lte includes the boundary (less than or equal).
$lt and $lte examples
// Products with fewer than 10 items in stock (low stock warning)
await db.collection('products').find({ stock: { $lt: 10 } }).toArray();
// Users under 18
await users.find({ age: { $lt: 18 } }).toArray();
// Documents created before a certain date
const archiveDate = new Date('2020-01-01');
await db.collection('posts').find({
createdAt: { $lte: archiveDate },
}).toArray();Combining Range Operators
Combine $gt/$gte with $lt/$lte on the same field to express range queries.
Range queries
// Users aged between 25 and 35 (inclusive)
await users.find({
age: { $gte: 25, $lte: 35 },
}).toArray();
// Products priced between $10 and $50
await db.collection('products').find({
price: { $gte: 10, $lte: 50 },
}).toArray();
// Events happening this week
const now = new Date();
const weekFromNow = new Date(Date.now() + 7 * 24 * 60 * 60 * 1000);
await db.collection('events').find({
startDate: { $gte: now, $lte: weekFromNow },
}).toArray();$in — Match Any in List
$in selects documents where the field value equals any value in the specified array. Equivalent to SQL's IN (...). It also matches documents where the field is an array containing at least one of the listed values.
$in examples
// Find users with role 'admin' or 'moderator'
await users.find({
role: { $in: ['admin', 'moderator'] },
}).toArray();
// Find products with one of several statuses
await db.collection('products').find({
status: { $in: ['active', 'featured', 'new'] },
}).toArray();
// Find documents by multiple IDs
const { ObjectId } = require('mongodb');
const ids = ['64a1...', '64b2...', '64c3...'].map(id => new ObjectId(id));
await users.find({ _id: { $in: ids } }).toArray();
// $in on an array field — matches if the array contains ANY of the values
await db.collection('articles').find({
tags: { $in: ['mongodb', 'nosql'] },
}).toArray();$nin — Not in List
$nin selects documents where the field value is not in the specified array. Also matches documents where the field does not exist.
$nin examples
// Exclude banned and suspended users
await users.find({
status: { $nin: ['banned', 'suspended'] },
}).toArray();
// Find products not in specific categories
await db.collection('products').find({
category: { $nin: ['archived', 'discontinued', 'draft'] },
}).toArray();Comparing Dates
MongoDB stores dates as BSON Date objects. All comparison operators work with dates — just pass JavaScript Date objects as the comparison value.
Date comparison
// Find documents created in the last 7 days
const sevenDaysAgo = new Date(Date.now() - 7 * 24 * 60 * 60 * 1000);
await db.collection('posts').find({
createdAt: { $gte: sevenDaysAgo },
}).toArray();
// Find documents within a specific date range
await db.collection('orders').find({
placedAt: {
$gte: new Date('2024-01-01'),
$lt: new Date('2024-02-01'),
},
}).toArray();
// Find events that have already passed
await db.collection('events').find({
endDate: { $lt: new Date() },
}).toArray();Comparing Null and Missing Fields
In MongoDB, null and a missing field are treated similarly in queries. Understanding this distinction is important for writing precise filters.
Null comparisons
// Matches documents where field is null OR field does not exist
await users.find({ profilePhoto: null }).toArray();
// Equivalent: { profilePhoto: { $eq: null } }
// Match null OR missing using $in
await users.find({ profilePhoto: { $in: [null] } }).toArray();
// Match ONLY documents where field exists and is explicitly null
await users.find({
profilePhoto: { $exists: true, $eq: null },
}).toArray();
// Match documents where field exists and is NOT null
await users.find({
profilePhoto: { $exists: true, $ne: null },
}).toArray();