Cheat Sheet: Array Operators
Array query predicates, aggregation expressions, update modifiers, and positional update selectors.
$all
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.
db.products.find({ tags: { $all: ["mongodb", "database"] } })- Requires an array to contain every listed value.
- Typical use: Find records with all required labels.
$elemMatch
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.
db.orders.find({ items: { $elemMatch: { sku: "KB-1", quantity: { $gte: 2 } } } })- Requires one array element to satisfy all inner conditions.
- Typical use: Match multiple predicates on the same embedded item.
$size
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.
db.posts.find({ tags: { $size: 3 } })$size does not directly support arbitrary ranges.- Matches arrays with exactly n elements.
- Typical use: Find arrays of exact length.
$in
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.
db.products.find({ category: { $in: ["keyboard", "mouse"] } })- Matches a value equal to any listed item; for arrays, a matching element is sufficient.
- Typical use: Filter by allowed categories.
$nin
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.
db.products.find({ status: { $nin: ["deleted", "blocked"] } })$exists if missing fields must not match.- Matches values not in the list and also matches missing fields.
- Typical use: Exclude statuses.
$isArray
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.
{ $project: { isArray: { $isArray: "$tags" } } }- Returns true if the expression resolves to an array.
- Typical use: Guard against inconsistent imported data.
$size
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.
{ $project: { tagCount: { $size: { $ifNull: ["$tags", []] } } } }$ifNull and validation for missing/null values.- Returns array length; errors if the resolved value is not an array.
- Typical use: Calculate array length.
$arrayElemAt
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.
{ $project: { latest: { $arrayElemAt: ["$events", -1] } } }- Returns the element at an index; negative indexes count from the end.
- Typical use: Select a boundary element from an ordered array.
$filter
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.
{ $project: { passing: { $filter: { input: "$scores", as: "s", cond: { $gte: ["$$s", 70] } } } } }- Keeps elements satisfying a condition and preserves relative order.
- Typical use: Filter arrays without unwinding.
$map
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.
{ $project: { lines: { $map: { input: "$items", as: "i", in: { sku: "$$i.sku", total: { $multiply: ["$$i.price", "$$i.qty"] } } } } } }- Transforms each element into a new value or object.
- Typical use: Compute per-element values.