The $size operator counts elements in an array. Use it in find queries to match an exact array length, or in aggregation pipelines to add a count field and filter by array length dynamically.
01
Count Elements
Array length.
02
Query + Agg
Two contexts.
03
Exact Match
In find queries.
04
$expr Filter
Range counts.
05
Use Cases
Tags, skills, items.
06
Empty = 0
[] returns zero.
Fundamentals
Definition and Usage
MongoDB’s $size operator returns how many elements an array contains. In an aggregation pipeline, { $size: "$tags" } produces a number like 3. In a find query, { tags: { $size: 3 } } matches only documents whose tags array has exactly three elements.
This is one of the most practical array operators — use it to validate list lengths, score content by tag count, or find documents with empty arrays.
💡
Beginner Tip
Query $size only supports exact counts. Need “at least 3 tags”? Use aggregation $size inside $match with $expr and $gte.
Foundation
📝 Syntax
$size has two forms depending on where you use it:
Relevance scoring — count overlap after set operations like $setIntersection.
Data quality checks — flag records where list fields have unexpected lengths.
🧠 How $size Works
1
MongoDB reads the array
In aggregation, the input expression resolves to an array field or literal. In queries, MongoDB checks the named field.
Input
2
Element count is computed
MongoDB counts top-level array elements. Nested arrays count as one element each.
Count
3
Result is used or returned
Aggregation stores the integer in a new field or compares it in $expr. Queries match or exclude documents.
Output
=
🔢
Integer count
Use for display, sorting, filtering, or chaining with other operators.
Wrap Up
Conclusion
The $size operator counts array elements in both queries and aggregation pipelines. Use the query form for exact-length matches and the aggregation form when you need computed counts or range comparisons.
Pair it with set operators like $setIntersection for overlap scoring, or with $slice to extract portions of arrays after you know their size. Next in the series: $slice.
Use aggregation $size + $expr for “at least N” logic
Guard missing fields with $ifNull: [ "$tags", [] ] before $size
Combine with $setIntersection for overlap counts
Remember empty arrays return 0, not null
❌ Don’t
Use query $size with $gt or $lt — it only accepts exact integers
Pass non-array values to aggregation $size without checking
Confuse element count with byte size (see $bsonSize for document bytes)
Expect $size to flatten nested arrays before counting
Forget that null input in aggregation causes an error
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $size
Use these points when counting array elements in MongoDB.
5
Core concepts
🔢01
Count
Array length.
Purpose
📝02
Query + Agg
Two forms.
Syntax
🛠03
Exact in find
Integer only.
Query rule
🗃04
+ $expr
Range filters.
Pattern
⚠05
[] → 0
Empty array.
Edge case
❓ Frequently Asked Questions
$size returns the number of elements in an array. In aggregation pipelines it is an expression: { $size: "$tags" }. In find queries it matches documents where an array field has an exact length: { tags: { $size: 3 } }.
Aggregation syntax: { $size: <array expression> }. Query syntax: { <arrayField>: { $size: <non-negative integer> } }. The query form requires an exact integer match.
Not directly in a find query — query $size only supports exact counts. In aggregation, wrap $size in $expr with $gt or $gte inside $match: { $expr: { $gte: [ { $size: "$tags" }, 3 ] } }.
$size returns 0 for an empty array []. This applies in both aggregation expressions and when matching { field: { $size: 0 } } in queries.
In aggregation, $size on null or a non-array value causes an error. Use $ifNull or $isArray guards when fields may be missing or wrong type.