EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials MongoDB Tutorial MongoDB Aggregate
 

MongoDB Aggregate

Priya Pedamkar
Article byPriya Pedamkar

Updated February 28, 2023

MongoDB Aggregate

 

 

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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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:

MongoDB aggregate1

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:

MongoDB aggregate2

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:

return information using aggregate

MongoDB aggregate4

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:

greater than equal to

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:

less than

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 –

  1. Lookup in MongoDB
  2. MongoDB Commands
  3. MongoDB Tools
  4. MongoDB vs HBase
  5. MongoDB Delete | How to Work?
  6. MongoDB Array | Examples
  7. Guide to MongoDB Distinct
  8. MongoDB Versions | How to Check?

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW