When MongoDB users want to gather metrics from a MongoDB database, aggregation of MongoDB is the best tool for this. Bundling the data from numerous record sources which are then operated in various ways on a pool of data for returning a combined result is what MongoDB allows its users. In this chapter, you will learn about the concept of aggregation that s supported by MongoDB.

What Is Aggregation?

In MongoDB, aggregation can be defined as the operation that is used for processing various types of data in the collection, which returns a calculated result. The concept of aggregation mainly clusters out your data from multiple different documents which are then used and operates in lots of ways (on these clustered data) to return a combined result which can bring new information to the existing database. You can relate aggregation to that of the count(*) along with the 'group by' used in SQL since both are equivalent in terms of the working.

MongoDB offers three different ways of performing aggregation:

  • The aggregation pipeline.
  • The map-reduce function.
  • Single purpose aggregation methods.

aggregate() Method in MongoDB

MongoDB's aggregate function will cluster out the records in the form of a collection which can be then employed for providing operations like total number(sum), mean, minimum and maximum, etc. from the aggregated group of data extracted.

For performing such an aggregate function, the aggregate() method is used. The syntax of this method looks something like this:



Now, let us see how the aggregate() method works:

Implementation of aggregate() Method

Consider a collection named programmers, which has the following data. You have used the find() method to take a look at all the different data available within it:





db.programmers.aggregate([{$group : {_id: "$type", TotalRecords: {$sum : 1}}}])


The above-executed aggregate() method will give the result shown. It says that there are three records which do not have any specific type and are available within the collection "programmers" for aggregating. Hence, the above aggregation method has clustered the collection's data in its best possible way.

Another example where we have a collection named writers, which has the following data:


Now, execute the aggregate() method:


db.writers.aggregate([{$group : {_id : "$author", TotalBooksWritten : {$sum : 1}}}])


Different Expressions Used by Aggregate Function

Expression Description
$sum adds up the definite values of every document of a collection.
$avg computes the average values of every document of a collection.
$min finds and returns the minimum of all values from within a collection.
$max finds and returns the maximum of all values from within a collection.
$push feeds in the values to an array in the associated document.
$first fetches out the first document.
$last fetches out the last document.
$addToSet feeds in the values to an array without duplication.

Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram