$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.