27 Topics
Chapter•54 Minutes

Cheat Sheet: Array Operators

Array query predicates, aggregation expressions, update modifiers, and positional update selectors.

$all

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

Reference group: Array query operators

What it does

Requires an array to contain every listed value.

Syntax

{ field: { $all: [v1, v2] }}

Use case

Find records with all required labels.

$all: practical example
db.products.find({ tags: { $all: ["mongodb", "database"] } })
💡 Explanation Requires an array to contain every listed value. When to use it Find records with all required labels.
Key Takeaways
  • Requires an array to contain every listed value.
  • Typical use: Find records with all required labels.

$elemMatch

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

Reference group: Array query operators

What it does

Requires one array element to satisfy all inner conditions.

Syntax

{ array: { $elemMatch: { ... } } }

Use case

Match multiple predicates on the same embedded item.

$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 Match multiple predicates on the same embedded item.
Key Takeaways
  • Requires one array element to satisfy all inner conditions.
  • Typical use: Match multiple predicates on the same embedded item.

$size

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

Reference group: Array query operators

What it does

Matches arrays with exactly n elements.

Syntax

{ arrayField: { $size: n } }

Use case

Find arrays of exact length.

Important notes

Query $size does not directly support arbitrary ranges.

$size: practical example
db.posts.find({ tags: { $size: 3 } })
💡 Explanation Matches arrays with exactly n elements. When to use it Find arrays of exact length. Caution Query $size does not directly support arbitrary ranges.
Key Takeaways
  • Matches arrays with exactly n elements.
  • Typical use: Find arrays of exact length.

$in

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

Reference group: Array query operators

What it does

Matches a value equal to any listed item; for arrays, a matching element is sufficient.

Syntax

{ field: { $in: [values] } }

Use case

Filter by allowed categories.

$in: practical example
db.products.find({ category: { $in: ["keyboard", "mouse"] } })
💡 Explanation Matches a value equal to any listed item; for arrays, a matching element is sufficient. When to use it Filter by allowed categories.
Key Takeaways
  • Matches a value equal to any listed item; for arrays, a matching element is sufficient.
  • Typical use: Filter by allowed categories.

$nin

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

Reference group: Array query operators

What it does

Matches values not in the list and also matches missing fields.

Syntax

{ field: { $nin: [values] } }

Use case

Exclude statuses.

Important notes

Use $exists if missing fields must not match.

$nin: practical example
db.products.find({ status: { $nin: ["deleted", "blocked"] } })
💡 Explanation Matches values not in the list and also matches missing fields. When to use it Exclude statuses. Caution Use $exists if missing fields must not match.
Key Takeaways
  • Matches values not in the list and also matches missing fields.
  • Typical use: Exclude statuses.

$isArray

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

Reference group: Array aggregation expressions

What it does

Returns true if the expression resolves to an array.

Syntax

{$isArray: expression}

Use case

Guard against inconsistent imported data.

$isArray: practical example
{ $project: { isArray: { $isArray: "$tags" } } }
💡 Explanation Returns true if the expression resolves to an array. When to use it Guard against inconsistent imported data.
Key Takeaways
  • Returns true if the expression resolves to an array.
  • Typical use: Guard against inconsistent imported data.

$size

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

Reference group: Array aggregation expressions

What it does

Returns array length; errors if the resolved value is not an array.

Syntax

{$size: arrayExpr}

Use case

Calculate array length.

Important notes

Use $ifNull and validation for missing/null values.

$size: practical example
{ $project: { tagCount: { $size: { $ifNull: ["$tags", []] } } } }
💡 Explanation Returns array length; errors if the resolved value is not an array. When to use it Calculate array length. Caution Use $ifNull and validation for missing/null values.
Key Takeaways
  • Returns array length; errors if the resolved value is not an array.
  • Typical use: Calculate array length.

$arrayElemAt

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

Reference group: Array aggregation expressions

What it does

Returns the element at an index; negative indexes count from the end.

Syntax

{$arrayElemAt: [array, index]}

Use case

Select a boundary element from an ordered array.

$arrayElemAt: practical example
{ $project: { latest: { $arrayElemAt: ["$events", -1] } } }
💡 Explanation Returns the element at an index; negative indexes count from the end. When to use it Select a boundary element from an ordered array.
Key Takeaways
  • Returns the element at an index; negative indexes count from the end.
  • Typical use: Select a boundary element from an ordered array.

$filter

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

Reference group: Array aggregation expressions

What it does

Keeps elements satisfying a condition and preserves relative order.

Syntax

{$filter: { input, as?, cond, limit? }}

Use case

Filter arrays without unwinding.

$filter: practical example
{ $project: { passing: { $filter: { input: "$scores", as: "s", cond: { $gte: ["$$s", 70] } } } } }
💡 Explanation Keeps elements satisfying a condition and preserves relative order. When to use it Filter arrays without unwinding.
Key Takeaways
  • Keeps elements satisfying a condition and preserves relative order.
  • Typical use: Filter arrays without unwinding.

$map

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

Reference group: Array aggregation expressions

What it does

Transforms each element into a new value or object.

Syntax

{$map: { input, as?, in }}

Use case

Compute per-element values.

$map: practical example
{ $project: { lines: { $map: { input: "$items", as: "i", in: { sku: "$$i.sku", total: { $multiply: ["$$i.price", "$$i.qty"] } } } } } }
💡 Explanation Transforms each element into a new value or object. When to use it Compute per-element values.
Key Takeaways
  • Transforms each element into a new value or object.
  • Typical use: Compute per-element values.