Aggregate Function in MongoDB

MongoDB Aggregate Function

In MongoDB, the Aggregation function is defined as the best method to work with a cluster of data and find out a common result. Performing aggregation means, collecting data from different documents, grouping all the data together and them applying operation like finding out the maximum vales, sorting them, sum of total values and many more.

MongoDB Aggregate Example

To understand the concept of aggregation, let's discuss it practically with the help of an example.

For example, You have three documents in a single collection. Every documents contains 3 fields, Name, Product and Price as shown below:

MongoDB Aggregate Example

Now, If you know that how many different categories of product you have in a collection, you can apply aggregate function $groupĀ to group all different categories of product all together and display it on the screen as shown below:

 db.productall.aggregate([{$group:{_id:'$Product'}}])

Output

MongoDB Aggregate Example

On the further articles, we are going to discuss about various other aggregation functions that we have in MongoDB.