11 Topics
Chapter•22 Minutes

Cheat Sheet: Query Operators

Filters, comparisons, logical predicates, field existence/type checks, array predicates, and expression-based queries.

$eq / $ne

Reference type:Cheat sheetFormat:Syntax + example + use case

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.

$eq / $ne: practical example
db.products.find({ status: { $ne: "deleted" } })
💡 Explanation Equality and inequality; direct equality syntax is often shorter. When to use it Filter exact values or exclude a value.
Key Takeaways
  • Equality and inequality; direct equality syntax is often shorter.
  • Typical use: Filter exact values or exclude a value.

$gt / $gte / $lt / $lte

Reference type:Cheat sheetFormat:Syntax + example + use case

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.

$gt /$gte /$lt /$lte: practical example
db.orders.find({ total: { $gte: 1000, $lt: 5000 } })
💡 Explanation Range comparisons use BSON comparison semantics. When to use it Filter prices, dates, or thresholds.
Key Takeaways
  • Range comparisons use BSON comparison semantics.
  • Typical use: Filter prices, dates, or thresholds.

$and /$or /$nor

Reference type:Cheat sheetFormat:Syntax + example + use case

Reference group: Query filter operators

What it does

Combines predicates; separate fields in one object are implicitly ANDed.

Syntax

{$or: [{...}, {...}]}

Use case

Express alternatives.

$and /$or /$nor: practical example
db.users.find({ $or: [{ role: "admin" }, { permissions: "manage" }] })
💡 Explanation Combines predicates; separate fields in one object are implicitly ANDed. When to use it Express alternatives.
Key Takeaways
  • Combines predicates; separate fields in one object are implicitly ANDed.
  • Typical use: Express alternatives.

$exists

Reference type:Cheat sheetFormat:Syntax + example + use case

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.

$exists: practical example
db.users.find({ middleName: { $exists: false } })
💡 Explanation Tests field presence even when its value is null. When to use it Distinguish missing from null.
Key Takeaways
  • Tests field presence even when its value is null.
  • Typical use: Distinguish missing from null.

$type

Reference type:Cheat sheetFormat:Syntax + example + use case

Reference group: Query filter operators

What it does

Matches a specified BSON type.

Syntax

{ field: { $type: "string" } }

Use case

Audit type drift.

$type: practical example
db.products.find({ price: { $type: "string" } })
💡 Explanation Matches a specified BSON type. When to use it Audit type drift.
Key Takeaways
  • Matches a specified BSON type.
  • Typical use: Audit type drift.

$regex

Reference type:Cheat sheetFormat:Syntax + example + use case

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.

$regex: practical example
db.users.find({ email: { $regex: "@example\\.com$", $options: "i" } })
💡 Explanation Matches strings using regular expressions. When to use it Pattern matching and cleanup. Caution Unanchored/case-insensitive regex may be inefficient.
Key Takeaways
  • Matches strings using regular expressions.
  • Typical use: Pattern matching and cleanup.

$expr

Reference type:Cheat sheetFormat:Syntax + example + use case

Reference group: Query filter operators

What it does

Allows aggregation expressions inside query filters.

Syntax

{$expr: expression}

Use case

Compare fields in one document.

$expr: practical example
db.inventory.find({ $expr: { $lt: ["$available", "$reorderPoint"] } })
💡 Explanation Allows aggregation expressions inside query filters. When to use it Compare fields in one document.
Key Takeaways
  • Allows aggregation expressions inside query filters.
  • Typical use: Compare fields in one document.

$mod

Reference type:Cheat sheetFormat:Syntax + example + use case

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.

$mod: practical example
db.jobs.find({ attempt: { $mod: [2, 0] } })
💡 Explanation Matches numeric values with a given remainder. When to use it Select even/odd counters.
Key Takeaways
  • Matches numeric values with a given remainder.
  • Typical use: Select even/odd counters.

$elemMatch

Reference type:Cheat sheetFormat:Syntax + example + use case

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.

$elemMatch: practical example
db.orders.find({ items: { $elemMatch: { sku: "KB-1", quantity: { $gte: 2 } } } })
💡 Explanation Requires one array element to satisfy all inner conditions. When to use it Filter embedded items.
Key Takeaways
  • Requires one array element to satisfy all inner conditions.
  • Typical use: Filter embedded items.

$all

Reference type:Cheat sheetFormat:Syntax + example + use case

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.

$all: practical example
db.articles.find({ tags: { $all: ["mongodb", "indexing"] } })
💡 Explanation Requires all listed values in an array. When to use it Find records with all labels.
Key Takeaways
  • Requires all listed values in an array.
  • Typical use: Find records with all labels.