EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials MongoDB Tutorial Mongodb aggregation pipeline
Secondary Sidebar
MongoDB Tutorial
  • Advanced
    • MongoDB Array
    • PostgreSQL ARRAY_AGG()
    • Indexes in MongoDB
    • MongoDB create Index
    • MongoDB JSON
    • MongoDB Careers
    • MongoDB Migration
    • MongoDB Export to Excel
    • MongoDB Collection
    • MongoDB List Collections
    • MongoDB Capped Collections
    • MongoDB Delete Collection
    • Mongodb show collections
    • MongoDB Auto Increment
    • MongoDB Triggers
    • MongoDB Projection
    • Replication in MongoDB
    • MongoDB Database
    • Mongo DB Create Database
    • MongoDB Compass
    • MongoDB Users
    • MongoDB Authentication
    • MongoDB GridFS
    • MongoDB Relationships
    • MongoDB MapReduce
    • MongoDB Geospatial
    • MongoDB Monitoring
    • Backup in MongoDB
    • MongoDB Sharding
    • MongoDB Java Drivers
    • MongoDB Import
    • Mongo Database Interview Questions
    • MongoDB Join Two Collections
    • MongoDB Group by Multiple Fields
    • MongoDB Pagination
    • MongoDB Replica Set
    • MongoDB Bulk Update
    • MongoDB greater than
    • MongoDB Encryption
    • MongoDB find in array
    • MongoDB like query
    • Mongodb shell
    • MongoDB port
    • MongoDB Query Operators
    • MongoDB Web Interface
    • MongoDB Query Array
    • MongoDB Transactions
    • MongoDB Not In
    • MongoDB not null
    • MongoDB npm
    • MongoDB Remove
    • MongoDB schema
    • MongoDB find by id
    • Mongodb max document size
    • Mongodb not equal
    • MongoDB keyfile
    • MongoDB Hosting
    • MongoDB Kafka Connector
    • MongoDB exists
    • MongoDB express
    • MongoDB Performance
    • MongoDB Connection String
    • MongoDB BI Connector
    • Mongodb aggregation pipeline
    • Mongodb backup and restore
    • MongoDB Drop Collection
    • MongoDB Text Search
    • MongoDB URI
    • MongoDB Full Text Search
    • MongoDB List Databases
    • MongoDB Use Cases
  • Basics
    • What is MongoDB
    • How To Install MongoDB
    • MongoDB Tools
    • MongoDB GUI Tools
    • MongoDB Versions
    • MongoDB Commands
    • Advantages of MongoDB
    • MongoDB Features
    • Is MongoDB NoSQL
    • Is MongoDB Open Source
    • Build Web Applications using MongoDB
    • MongoDB Data Types
    • MongoDB Administration
    • Data Modeling in MongoDB
    • MongoDB vs Elasticsearch
    • MariaDB vs MongoDB
    • Firebase vs MongoDB
  • Commands
    • Mongodb updateMany
    • MongoDB Aggregation
    • Mongodb unwind
    • Mongodb where
    • MongoDB BSON
    • MongoDB Filter
    • Mongodb Match
    • MongoDB sort by date
    • MongoDB Limit()
    • MongoDB count
    • MongoDB Aggregate
    • MongoDB Distinct
    • MongoDB Unique
    • MongoDB find
    • MongoDB findOne()
    • MongoDB insert
    • MongoDB Delete
    • MongoDB Update
    • Lookup in MongoDB
    • order by in MongoDB
    • MongoDB $regex
    • MongoDB $elemMatch
    • MongoDB ObjectId()
    • MongoDB Skip()
    • MongoDB findAndModify
    • Mongodb findOneAndUpdate
    • MongoDB Date Query
    • MongoDB Timestamp
    • MongoDB sort()
    • MongoDB group by
    • MongoDB Join

Mongodb aggregation pipeline

Mongodb aggregation pipeline

Introduction to Mongodb aggregation pipeline

Mongo aggregation pipeline work by using different operators such as $group, $match, $sort, $geoNear, etc. One of the efficient ways of grouping the multiple fields present inside the documents of MongoDB is by making the use of $group operator which helps in performing various other aggregation functions as well on the grouped data which is an aggregation pipeline.

In this article, we will discuss the aggregation pipelines and study in detail one of the operators named $group along with an example in Mongo DB to group by different multiple fields inside the document which are listed as aggregate operation for single-use, aggregate pipeline, and programming model of MapReduce. We will also have a look at the list of operators used along with aggregation and how we can implement the same along with the help of certain examples.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Syntax of $group operator:

The input and output of the $group operator used for aggregating multiple fields in MongoDB are nothing but a documented record of mongo DB database. It accepts single documents and returns single or multiple documents. While using any of the fields in Mongo DB we will reference it by using the dollar symbol ($) prepended by the field name such as $name of the field. We can make the use of different kinds of operators while grouping the multiple fields which areas listed below.

  • $first – This operator is used in the group by multiple fields operation to get only a single first document from the grouped ones which is mostly used when performing sorting.
  • $push – This operation will insert a new value of a field inside the resultant field.
  • $last – This operation will insert a new value of a field inside the resultant field that is at last.
  • $addToSet – It helps in adding a new value to the existing values of the array of the resulting documents without any duplication taking place.
  • $min – This operator helps in finding out and returning the smallest integer value or smallest value from the supplied and passed integer value.
  • $avg – It will calculate the average value of all the specified numeric values between the fields.
  • $max – This operator helps in finding out and returning the largest integer value or biggest value from the supplied and passed integer value.
  • $sum – It will calculate the sum or total value of all the specified numeric values between the fields.

Aggregation Pipeline

The other method of grouping the fields in MongoDb when the multi-threaded POSIX mode is set to true is the use of pipelines. A particular order is considered while executing the stream of the data in pipelines by a particular set of threads. The pipeline used for aggregation consists of the stages. When the document is processed in a particular stage the resultant is transferred to the next stage and so on.

While using the aggregation pipelines. We can filter out the documents that satisfy the criteria by using the polymer tube functions. It is also used in changing the form of the output document and converting the same.

The stage operators are defined for each the level of aggregation pipeline. The stage operators can internally make the use of the expression operators for having a line break before each of the stages of level or even to calculate the average or sum or to concatenate a particular value. The ultimate results of the aggregation pipeline is considered the final output to be returned which can even be stored in collections if needed.

Processing Flow

The third way of grouping the multiple fields is by making the use of processing flow where we need to consider the following points –

  • We can make use of multiple channels for processing the data at the same time by using the Db.collection.aggregate() function.
  • We don’t need to write any of the custom JS routines for implementing the facility of the group by same as that of SQL group by if we are using the Db.collection.aggregate() function as it internally efficiently does the aggregation and also provides the support for multiple operations to be used inside it.
  • The limit of each phase in a pipeline is 100 MB. An error occurs if we are trying to use a phrase that exceeds the limit of 100 MB in MongoDB. The solution for processing a large amount of data is to use the allowDisk property which can be set to true and all the required data should be written to a temporary file in case if we are using a memory limit of 100 MB in the aggregated pipe nodes.
  • The Db.collection.aggregate() operation can be used with the series of slices in a very efficient way without any loss of data in the result while in the case of map-reduce with series of slices there are chances of losing the result.
  • collection.aggregate() function returns the data stored inside the memory in the form of a cursor which can be used directly as MongoShell.
  • The limitation for the size of BSON document is 16 MB and the output of this function can be stored only inside a single document.

Grouping Method

This is quite similar to the SQL Group By clause as it has three parameters listed below –

Key – this is used for showing the group key

Initial – It will help to give the initial value to the field of document which will be representing the document group.

Reduce – This will return the count of the total number of elements that accepts the parameters including the current element and the aggregate pf result document.

There is also the presence of other optional parameters.

Example – Let us consider an example. We have the customers details document whose contents are as shown below –

Mongodb aggregation pipeline output 1

We have to calculate the total bill amount for each of the stores.

Consider the following statement whose output will be –

js > db.users.group ({key: {store_id : true}, initial: {totalBillAmount : 0}, reduce : function (currentValue, result){result.totalBillAmount += 1}}) `

whose output is as shown below –

Mongodb aggregation pipeline output 2

Conclusion – Mongodb aggregation pipeline

We can make the aggregation pipelines in Mongo DB includes the $group operator and many others that are useful for getting an aggregating result.

Recommended Articles

This is a guide to Mongodb aggregation pipeline. Here we discuss the aggregation pipelines and study in detail one of the operators named $group along with an example. You may also have a look at the following articles to learn more –

  1. MongoDB Aggregation
  2. MongoDB sort()
  3. MongoDB Database
  4. MongoDB Timestamp
Popular Course in this category
MongoDB Training Program (4 Courses, 2 Projects)
  4 Online Courses |  2 Hands-on Projects |  22+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Oracle Training (17 Courses, 8+ Projects)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
SQL Training Program (10 Courses, 8+ Projects)4.7
Oracle DBA Database Management System Training (2 Courses)4.7
MS SQL Training (16 Courses, 11+ Projects)4.7
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - 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

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*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?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more