Updated February 28, 2023
Introduction to MongoDB Aggregate
MongoDB Aggregate is used to process the data or records and return the combined output as a result. Aggregate operation in MongoDB will be used to group the values from the multiple documents with each other. After the grouping of values, it will perform various operations on grouped data and then return the combined single result as output. MongoDB aggregate is significant and useful in MongoDB to return a single result of data from multiple documents. MongoDB aggregate is similar to the count (*) with the group by function in SQL language it will continue similar work in MongoDB.
Syntax
Below is the syntax:
collection_name.aggregate (pipeline, options)
collection_name.aggregate (aggregate operation)
Parameter description of MongoDB Aggregate method
Here are the parameters:
- Collection name: The collection name is defined as a process the data of documents from the collection by using the aggregate method. The collection name is used with the aggregate command in MongoDB to process the data and return the result.
- Aggregate: Aggregate operation in MongoDB will be used to group the values from the multiple documents with each other after grouping values. It will perform a variety of operations on grouped data. Aggregate is significant and useful in MongoDB.
- Pipeline: The pipeline is defined as a sequence of stages or data aggregate operations in MongoDB. There are multiple pipeline operator available in MongoDB like $merge, $out, $lookup, $match, $project, $limit, etc. We need to specify the pipeline parameter before specifying the options parameter.
- Options: This is an optional parameter in the MongoDB aggregate command. This is an additional option used in aggregate to passes the aggregate command. It is available only if we pass the pipeline stage as an array in MongoDB.
- Aggregate operation: This parameter is essential and useful in the aggregate function. We have used different expression in aggregate operation, and we have done different operations using the aggregate function.
How does Aggregate Command work in MongoDB?
Below is the working of aggregate command:
MongoDB aggregate is used to process various data types and return a calculated result using processed data. In MongoDB, there are three ways to perform aggregate are as follows.
1. Aggregation pipeline
It is defined as a sequence of stages or data aggregate operations. There are multiple pipeline operator which was available in MongoDB like $addFields, $bucket, $merge, $out, $lookup, $match, $project, $limit, $count, $facet etc. We need to specify the pipeline parameter before specifying the options parameter in the MongoDB aggregate method.
2. Map-reduce function
MongoDB provides a technique of map-reduce to perform aggregate operations. Map-reduce will perform aggregation in two stages.
3. Single-purpose aggregate method
This is a technique of Single purpose aggregation method to perform aggregate operations. It will perform all these operations in a single collection. Aggregate operation `will be used to group the values from the multiple documents with each other. After grouping the values, it will perform various operations on grouped data and then return the combined single result as output. MongoDB aggregate is used to process the data or records and return the combined output as a result.
Below are the different expressions which were used by the aggregate function in MongoDB.
- $sum – It will be used, to sum up, all the values of all the documents from the collection.
- $avg – It will average all the values of all the documents from the collection.
- $min – It will be used to return the min value of all the collection documents.
- $max –It will be used to return the max value of all the documents from the collection.
- $addToSet – It will insert a value into an array, but no duplicate values are allowed.
- $push – Insert value into an array.
- $first – It will return the first document.
- $last – It will return the last document.
Examples to Implement MongoDB Aggregate
Below is the example of the aggregate method:
We have taken an example of an emp_count table to describe examples of MongoDB’s aggregate method as follows. Below is the data description of the emp_count table are as follows.
Code:
db.emp_count.find ()
Output:
Example #1
Group by and calculate sum: The below example shows we have a calculating the sum of employee numbers which have a matching salary. We have used an aggregate $sum expression to find the number of employees who have the same salary.
Code:
db.emp_count.aggregate([{$group: {_id: "$emp_salary", category_salary: {$sum : 1}}}])
Output:
Example #2
Example to return information using aggregate pipeline operator: We have used $match pipeline operator to return information in description format. The below example shows the aggregate pipeline operator is as follows.
Code:
db.emp_count.explain().aggregate([{ $match: { emp_name: "ABC" } }, { $group: { _id: "$emp_salary", salary: { $sum: 1 } } }])
Output:
Example #3
Using greater than equal to the operator with aggregate ($match)pipeline operator: The below example shows greater than equal to the operator with a $match pipeline operator in the aggregate method.
Code:
db.emp_count.aggregate([{ $match: { emp_salary: {$gte: "20000"} }}])
Output:
Example #4
Using less than an operator with an aggregate ($match) pipeline operator: Below example shows less than an operator with an aggregate ($match) pipeline operator in the aggregate method.
Code:
db.emp_count.aggregate ([{ $match: { emp_salary: {$lt: "50000"} }}])
Output:
Conclusion
MongoDB aggregate is significant in MongoDB to return a single result of data from multiple documents. Aggregates used to group the values from the multiple documents with each other. After grouping the values, it will perform various operations on grouped data and then return the result.
Recommended Articles
This is a guide to MongoDB Aggregate. Here we discuss an introduction to MongoDB Aggregate with syntax, parameters, how it works, and examples with code and output. You can also go through our other related articles to learn more –