Concept of Aggregation in MongoDB

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:

Syntax:
db.collection_name.aggregate(aggregate_operation)

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:

Example:
db.programmers.find()
Output:

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

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:

Example:

Now, execute the aggregate() method:

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

Different Expressions Used by Aggregate Function

ExpressionDescription
$sumadds up the definite values of every document of a collection.
$avgcomputes the average values of every document of a collection.
$minfinds and returns the minimum of all values from within a collection.
$maxfinds and returns the maximum of all values from within a collection.
$pushfeeds in the values to an array in the associated document.
$firstfetches out the first document.
$lastfetches out the last document.
$addToSetfeeds in the values to an array without duplication.

Scroll Back to Top