Cheat Sheet: Query Operators
Filters, comparisons, logical predicates, field existence/type checks, array predicates, and expression-based queries.
$eq / $ne
Reference group: Query filter operators
What it does
Equality and inequality; direct equality syntax is often shorter.
Syntax
{ field: { $eq: value } } / { field: { $ne: value } }
Use case
Filter exact values or exclude a value.
db.products.find({ status: { $ne: "deleted" } })- Equality and inequality; direct equality syntax is often shorter.
- Typical use: Filter exact values or exclude a value.
$gt / $gte / $lt / $lte
Reference group: Query filter operators
What it does
Range comparisons use BSON comparison semantics.
Syntax
{ field: { $gte: value } }
Use case
Filter prices, dates, or thresholds.
db.orders.find({ total: { $gte: 1000, $lt: 5000 } })- Range comparisons use BSON comparison semantics.
- Typical use: Filter prices, dates, or thresholds.
$and /$or /$nor
Reference group: Query filter operators
What it does
Combines predicates; separate fields in one object are implicitly ANDed.
Syntax
{$or: [{...}, {...}]}
Use case
Express alternatives.
db.users.find({ $or: [{ role: "admin" }, { permissions: "manage" }] })- Combines predicates; separate fields in one object are implicitly ANDed.
- Typical use: Express alternatives.
$exists
Reference group: Query filter operators
What it does
Tests field presence even when its value is null.
Syntax
{ field: { $exists: true } }
Use case
Distinguish missing from null.
db.users.find({ middleName: { $exists: false } })- Tests field presence even when its value is null.
- Typical use: Distinguish missing from null.
$type
Reference group: Query filter operators
What it does
Matches a specified BSON type.
Syntax
{ field: { $type: "string" } }
Use case
Audit type drift.
db.products.find({ price: { $type: "string" } })- Matches a specified BSON type.
- Typical use: Audit type drift.
$regex
Reference group: Query filter operators
What it does
Matches strings using regular expressions.
Syntax
{ field: { $regex: pattern, $options: flags } }
Use case
Pattern matching and cleanup.
Important notes
Unanchored/case-insensitive regex may be inefficient.
db.users.find({ email: { $regex: "@example\\.com$", $options: "i" } })- Matches strings using regular expressions.
- Typical use: Pattern matching and cleanup.
$expr
Reference group: Query filter operators
What it does
Allows aggregation expressions inside query filters.
Syntax
{$expr: expression}
Use case
Compare fields in one document.
db.inventory.find({ $expr: { $lt: ["$available", "$reorderPoint"] } })- Allows aggregation expressions inside query filters.
- Typical use: Compare fields in one document.
$mod
Reference group: Query filter operators
What it does
Matches numeric values with a given remainder.
Syntax
{ field: { $mod: [divisor, remainder] } }
Use case
Select even/odd counters.
db.jobs.find({ attempt: { $mod: [2, 0] } })- Matches numeric values with a given remainder.
- Typical use: Select even/odd counters.
$elemMatch
Reference group: Query filter operators
What it does
Requires one array element to satisfy all inner conditions.
Syntax
{ array: { $elemMatch: { ... } } }
Use case
Filter embedded items.
db.orders.find({ items: { $elemMatch: { sku: "KB-1", quantity: { $gte: 2 } } } })- Requires one array element to satisfy all inner conditions.
- Typical use: Filter embedded items.
$all
Reference group: Query filter operators
What it does
Requires all listed values in an array.
Syntax
{ array: { $all: [values] } }
Use case
Find records with all labels.
db.articles.find({ tags: { $all: ["mongodb", "indexing"] } })- Requires all listed values in an array.
- Typical use: Find records with all labels.