MongoDB $size Operator
When working with data in MongoDB, arrays are a fundamental data type used to store multiple values in a single field. Whether we're handling lists of tags, categories, or other collections, it's often necessary to determine the size of an array. MongoDB provides the $size
operator to help us efficiently calculate the number of elements in an array.
MongoDB $size Operator
The $size
operator is a MongoDB aggregation operator that returns the number of elements in an array. It is widely used in the aggregation pipeline to manipulate and analyze documents based on the size of array fields. The $size
operator works seamlessly in stages like $project
, $match
, and $addFields
, making it an essential tool for data analysis and manipulation.
Syntax:
{ $size: <expression> }
<expression>
: The expression can be a field or any valid MongoDB expression that evaluates to an array. The operator returns the count of elements in the array.
Common Use Cases for the MongoDB $size
Operator
1. Counting the Number of Elements in an Array
The most common use of the $size
operator is to count the number of elements in an array field within a document.
2. Filtering Documents Based on Array Size
You can filter documents by comparing the size of an array to a specific value (e.g., greater than, less than, or equal to a certain number).
3. Transforming Data with Array Size
Use $size
to add new fields to documents that represent the size of an array, allowing for further analysis and manipulation.
Examples of MongoDB $size Operator
Let's explore some real-world examples of how to use the $size operator in MongoDB aggregation queries. For these examples we are working with:
- Database: GeeksforGeeks
- Collection: arrayExample
- Document: three documents that contain the details in the form of field-value pairs.

Example 1: Counting Array Elements Using $size
In this example, we will count the number of elements in the numbers1
array for a document where the name
is "Lolo" using a $size operator.
Query:
db.arrayExample.aggregate([
... {$match: {name: "Lolo"}},
... {$project: {number1Size: {$size: "$numbers1"}}}])
Output:

Explanation:
- We match the document where
name
is "Lolo". - The
$size
operator calculates the size of thenumbers1
array, which contains 3 elements.
Example 2: Using $size Operator in the Embedded Document
In this example, we are going to find the size of the indoorGames array inside the favGame embedded document for a document where name is "Piku"
Query:
db.arrayExample.aggregate([
... {$match: {name: "Piku"}},
... {$project: {resultSize: {$size: "$favGame.indoorGames"}}}])
Output:

Explanation:
The $size
operator counts the number of elements in the indoorGames
array inside the embedded favGame
document. The result is 4
because the array contains four elements.
Example 3: Filtering Documents Based on Array Size
This example shows how to filter documents where the size of the numbers1 array is greater than 2.
Query:
db.arrayExample.aggregate([
{ $match: { $expr: { $gt: [{ $size: "$numbers1" }, 2] } } }
])
Output:
[
{
"_id": ObjectId("60b8d5f22b0a99d3a6c8b70d"),
"name": "Lolo",
"numbers1": [1, 2, 3]
}
]
Explanation:
- The
$size
operator calculates the size of thenumbers1
array. - The
$gt
operator ensures that only documents with more than 2 elements in thenumbers1
array are returned. In this case, the document for "Lolo" is returned because thenumbers1
array has 3 elements.
Important Points About $size Operator
- The $size operator in MongoDB is used to find the total number of elements present in a specified array.
- If the argument of the $size operator is missing or does not resolve to an array, errors may occur.
- It is an aggregation operator that can be used in the aggregation pipeline stages.
- The $size operator can be used to find the size of arrays in documents, allowing for efficient data analysis and manipulation.
Conclusion
In conclusion, the $size
operator in MongoDB is a valuable feature for developers and data analysts working with array data. Its ability to quickly and accurately determine array sizes makes it an essential tool for various data processing tasks. Whether used in aggregation pipelines or other contexts, the $size
operator enhances the functionality of MongoDB and improves the efficiency of array-related operations. Make sure to follow best practices and combine $size
with other MongoDB operators to get the most out of this powerful feature.