MongoDB

What is Aggregation in MongoDB with example

Database Management Systems have some common operations that are supported by SQL and NoSQL databases as well. The aggregation operation is one of them and is backed by several relational and non-relational databases. MongoDB is one of those databases that have the support of this operation. Aggregation is a key operation in any database that allows you to process data records to get targeted results. With the help of aggregation, the users can combine several entities to form a single meaningful entity by grouping the data.

The aggregation operations consist of several expressions that help to group the data for a meaningful output. For instance, a laptop, mobiles, gadgets can be combined under a single entity, let’s say technology_store. The entities are combined when individual entities do not represent anything or have no meaning.

This article provides a deep insight into the aggregate method and the expressions supported by this method.

How does the aggregate function work in MongoDB

Firstly, for aggregation, it is recommended to understand the aggregate function; the syntax of this function is provided below:

> db.collection.aggregate(aggregate-operation)

In the syntax, “collection” and “aggregate-operation” are user-defined. The “collection” name can be anything and “aggregate-operation” can be created by using several aggregate expressions supported by MongoDB. Few well known aggregate expressions used are listed below:

  • $sum: This expression adds up the values of a specific field in a document.
  • $min: Gets the minimum value from corresponding values in all documents.
  • $max: Works same as $min, however, it gets the maximum value.
  • $avg: This expression is used to calculate the average of given values in a collection
  • $last: It returns the last document from source document
  • $first: It is used to return the first document from a source document
  • $push: This expression inserts values to an array in resulting document (duplicates may occur while using $push)

How to use an aggregate function in MongoDB

In this section, we have provided a few examples that will help you to understand the working of Aggregation in MongoDB.

The collection name used in this example is “workers” and the content inside it is shown below:

> db.workers.find().pretty()

As output shows, workers have fields: “name”, “designation”, “department” and “salary”.

Example 1: Using $sum expression

The following aggregation operation will group the workers with respect to the associated department and $sum expression is used to give a total number of workers in each department:

As the output shows that the command has categorized the workers with respect’ to associated departments:

> db.workers.aggregate([{$group: {_id: "$department", Total_Workers: {$sum: 1}}}])

You can group the data in other ways as well; like if you want to get the number of workers with respect to their designations; you can also do so by using the below-stated command:

A result like this can be useful to get the number of workers at different designations.

> db.workers.aggregate([{$group: {_id: "$designation", Total_Workers: {$sum: 1}}}])

Example 2: Using $avg expression

In this example, the collection is the same as in Example 1. Here, $avg aggregation expression is used to get the average salary in each department of workers collection. In our case, the following aggregate function will calculate the average salary of workers in “writing” and “video” departments:

> db.workers.aggregate([{$group: {_id: "$department", Average: {$avg: "$salary"}}}])

Example 3: Using $min and $max expressions

You can get the minimum salary by specifying the $min expression in aggregate method: The below-mentioned command will print the minimum salary of workers in both departments:

> db.workers.aggregate([{$group: {_id: "$department", Min_Salary: {$min: "$salary"}}}])

And the command mentioned below will check for the maximum salary of workers by grouping them “designation” wise:

As discussed earlier, for calculation of maximum values, $max operation is used:

> db.workers.aggregate([{$group: {_id: "$designation", Max_Salary: {$max: "$salary"}}}])

Example 4: Using $push expression

This example explains the usage of $push with the aggregate method in MongoDB. The $push expression returns the data as array values and is used to get conditional results on grouped data. Here, in this example, we are using collection “tech_store” and the following content resides inside it:

> db.tech_store.find()

The collection contains a list of few products and their expiry dates. The command written below will perform the following actions:

  • groups the data with respect to the expiry year of each product.
  • the documents falling in each year will be pushed using the $push operator.
> db.tech_store.aggregate([{$group: {_id: {Expiry: { $year: "$Expiry"}}, itemstoexpire: {$push: {Product: "$Product", Quantity: "$Qty" }}}}]).pretty()

Example 5: Using $first and $last expressions

There are two more expressions ($first and $last) that can be used in the aggregate method. For the exercise of these methods, we will use a “laptops” collection that contains the following documents.

> db.laptops.find()

$first: The $first operator is used to print the last value from the grouped data. For instance, the command written below will group the data according to the “Product” field and then the $first operator displays the items that are going to be expired from.

> db.laptops.aggregate([{$group: {_id: "$Product", itemstoexpire: {$first: "$Expiry" }}}]).pretty()

$last: By using $last, you can check the last value of any field in a grouped data. For example, the below-mentioned command will group the data with respect to the “Product” field and the $last operator is then used to get the expiry date (occurring at the end) of each product.

> db.laptops.aggregate([{$group: {_id: "$Product", itemstoexpire: {$last: "$Expiry" }}}]).pretty()

Conclusion

MongoDB has a wide range of functions available to perform specific operations on complete collections or a specific document in a collection. The aggregate function is practiced usually to get the computed result of the collection by grouping the data to fetch meaningful entities. In this informative post, you will learn the basics of the Aggregation concept in MongoDB and the expressions used in Aggregation. In the end, a few aggregation examples are executed to show the implementation of the aggregate function in MongoDB followed by exercising its expressions as well.

About the author

Adnan Shabbir