MongoDB Aggregation Size – Basic 2021 Guide

Aggregation Size Operator Featured Image

In this guide, I am going to cover the MongoDB aggregation size operator.

The aggregation size operator simply counts and returns the number of elements in an array.

The aggregation size operator may accept any expression as long as it pertains to an array. If the argument specified to the operator is missing or does not persist to an array, then the aggregation size operator throws an error.

This guide aims to provide examples to use the MongoDB aggregation size operator for your good. So, let us get this ultimate guide started!

The Aggregation Size Operator Syntax in MongoDB

Let us take a quick look at the syntax of the aggregation size operator.

{ $size:  }

The $size operator is one of the array expression operators used in the aggregation pipeline stage.

Using the Aggregation Size Operator in MongoDB

Let us now go ahead start taking a look at the examples of using MongoDB’s aggregation size operator.

To do so, we will be using the below collection named drones. This is what it looks like:

> db.drones.find().pretty()
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0c"),
        "utility" : [
                "Natural Resource Exploration",
                "Remote sensing",
                "Real estate and construction",
                "Recreation",
                "Delivery"
        ],
        "onSale" : false,
        "name" : "Nimbari Gryphon Medeta 65",
        "price" : 77500,
        "weight" : "77 kilograms",
        "additionalDetails" : {
                "material" : "carbon fiber",
                "moreUses" : [
                        "Precision Agriculture",
                        "Land Inspection",
                        "Water Inspection",
                        "Cinematography"
                ]
        }
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0d"),
        "utility" : [
                "Natural Resource Exploration",
                "Remote sensing",
                "Real estate and construction",
                "Recreation",
                "Delivery"
        ],
        "onSale" : false,
        "name" : "X-Strimmer Eye",
        "price" : 23500,
        "weight" : "24 kilograms",
        "additionalDetails" : {
                "material" : "glass fiber",
                "moreUses" : [
                        "Precision Agriculture",
                        "Cinematography"
                ]
        }
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0e"),
        "utility" : [
                "Natural Resource Exploration",
                "Remote sensing",
                "Real estate and construction",
                "Recreation",
                "Delivery"
        ],
        "onSale" : false,
        "name" : "Khai Balemosh Shefqa TRX",
        "price" : 120500,
        "weight" : "80 kilograms",
        "additionalDetails" : {
                "material" : "aluminum",
                "moreUses" : [
                        "Precision Agriculture",
                        "Land Inspection"
                ]
        }
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0f"),
        "utility" : [
                "Natural Resource Exploration",
                "Recreation",
                "Delivery"
        ],
        "onSale" : false,
        "name" : "Sifinist Croma AX",
        "price" : 99500,
        "weight" : "97 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Precision Agriculture",
                        "Land Inspection",
                        "Water Inspection",
                        "Videography"
                ]
        }
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf10"),
        "utility" : [
                "Remote sensing",
                "Real estate and construction",
                "Recreation"
        ],
        "onSale" : false,
        "name" : "Drovce Finnifield FR-7",
        "price" : 87600,
        "weight" : "13 kilograms",
        "additionalDetails" : {
                "material" : "polysterene",
                "moreUses" : [
                        "Precision Agriculture",
                        "Land Inspection",
                        "Water Inspection",
                        "Videography"
                ]
        }
}

Working on a Top-Level Array Field Using the Aggregation Size Operator

In this example, I will cover an example that explains using the aggregation size on a top-level array field.

I want to see the size of the utility field of the document with the name “Nimbari Gryphon Medeta 65”. Let us proceed with the operation for the same.

> db.drones.aggregate([
... {$match: {name: "Nimbari Gryphon Medeta 65"}},
... {$project: {utilSize: {$size: "$utility"}}}])

The output:

{ "_id" : ObjectId("61673f46b34f185eb7b2bf0c"), "utilSize" : 5 }

Perfect! We get an accurate size of 5, which is correct.

Working on a Nested Array Field Using Aggregation Size

In this example, I want to operate on a nested or embedded array field. I want to know the size of the moreUses array field which is nested into the additionalDetails field of the document with the name “X-Strimmer Eye”.

Let us proceed further with the operation.

> db.drones.aggregate([
... {$match: {name: "X-Strimmer Eye"}},
... {$project: {usageSize: {$size: "$additionalDetails.moreUses"}}}])

The output:

{ "_id" : ObjectId("61673f46b34f185eb7b2bf0d"), "usageSize" : 2 }

Stunning! Just as we expected, we receive the size of the specified embedded array field with the help of the aggregation size operator.

Conclusion

Learn to use the Aggregation size operator in MongoDB.

References

https://docs.mongodb.com/manual/reference/operator/aggregation/size/