MongoDB Aggregation (Types & Expression) – 7 Stages of Aggregation Pipeline

Hope, you are enjoying MongoDB tutorials. The last session was all about MongoDB GridFS. Today, we will see a new term called MongoDB Aggregation, an aggregation operation, MongoDB processes the data records and returns a single computed result. Here, we will talk about types of aggregation, expression, and stages of aggregation pipeline with examples.

So, let’s start the MongoDB Aggregation Tutorial.

What is MongoDB Aggregation

1. What is MongoDB Aggregation?

In aggregation operation, MongoDB processes the data records and returns a single computed result. It actually groups multiple documents and then performs aggregation operation on it and after that returns a single result to the end user.

MongoDB can perform aggregation in 3 ways and they are as follows:

  1. Aggregation Pipeline
  2. Map-Reduce Function
  3. Single Purpose Aggregation Method

i. MongoDB Aggregation Pipeline

Aggregation process in MongoDB is modeled on the concept of data processing pipelines. Multiple documents enter the pipeline and then these documents are being transformed into aggregated results. The operations being performed during pipeline include filter and document transformation in which they operate like queries and modify the form of output document respectively.

The pipeline method provides efficient data aggregation using other operations present in MongoDB. It can also operate on a sharded collection. It can also use MongoDB indexes to improve its efficiency at some stages. With all this aggregation pipeline has an internal optimization phase due to which it can make this process optimal for the processor.

MongoDB Aggregation Pipeline Example

MongoDB Aggregation Pipeline Example

ii. Map-Reduce

As the name suggests it has two operations in itself, they are a map in which each document is being processed along with emitting one or more objects for each document and reduce phase in which output of map operation are being combined together. MongoDB Map-reduce can optionally have a finalize stage in which it can do some final modifications in the output of the document. Map-Reduce uses JavaScript to perform its operations including the finalize operation. Even though javascript provides greater flexibility than aggregation pipeline, but still it is less efficient and more complex operation as compared to the aggregation pipeline in MongoDB.

MongoDB Mapreduce Example

MongoDB Mapreduce Example

iii. Single Purpose Aggregation Operations

These operations aggregate all the documents from a single collection in MongoDB. Even though they provide simple access to common aggregation operations they lack the flexibility and capability of map-reduce and aggregation pipeline.

Single Purpose Aggregation Operation Example

Single Purpose Aggregation Operation Example

2. Stages of MongoDB Aggregation Pipeline

These are 7 stages of aggregation of Pipeline in MongoDB:

  1. $project
  2. $match
  3. $group
  4. $sort
  5. $skip & $limit
  6. $first & $last
  7. $unwind

Do you know what is covered and analyzing queries in MongoDB?

i. $project

This stage is used to select certain fields from a collection. We can also add, remove or reshape a key.

Example –

db.example.aggregate([
  {
    $project:{
     _id:1,
     'dept':{$toUpper:'$name'},
     'newexp':{$add:['$exp',10]}
   }
      }
  ])

Here we are creating ‘dept’ from earlier ‘name’ which was in upper case. And in ‘newexp’ we are adding the experience as 10 years.

ii. $match

It is used in filtering operation and it can reduce the number of documents that are given as input to the next stage. 

Example –

db.example.aggregate([
   {
     $match:{
        name:'xyz'
      }
    }
])

Here we are aggregating documents that have a name equal to xyz.

Join DataFlair on Telegram

iii. $group

It does the work as the name says. It groups all documents based on some keys.

Example –

db.example.aggregate([
  {
     $group:{
       _id:{'dept':'$name'},
       employee_count:{$sum:10}
      }
   }
])

Let’s Create an Auto-Increment Sequence in MongoDB?

iv. $sort

It is used to sort all the documents.

Example –

Here we want to group all department in ascending order and then find the count of employees.

db.example.aggregate([
  {
    $group:{
      _id:'$name',
      employee_count:{$sum:1}
    }
 },
 {
    $sort:{
       _id:1
      }
   }
])

v. $skip & $limit

Using skip we can skip forward in the list of all documents for the given limit. And using limit we can limit the number of documents we want to look at by specifying the limit as per our convenience.

Example –

db.example.aggregate([
   {
     $group:{
       _id:'$name',
       employee_count:{$sum:10}
      }
   },
  {
     $sort:{
     _id:1
    }
  },
 {
    $skip:4
 },
 {
    $limit:5
 }
])

vi. $first & $last

It is used to get the first and last values in each group of documents.

Example –

db.example.aggregate([
  {
    $group:{
      _id:'$name',
      employee_count:{$sum:1},
      record:{ $first:'$code'}
     }
   }
])

vii. $unwind

We can use unwind for all documents, that are using arrays.

Example –

Let’s say that we are having a sample document:

{
  a:abcdata,
  b:xyzdata,
  c:[a1,a2,a3]
}

Also, see – MongoDB ObjectID and Atomic Operations

After this if we perform unwind operation on c then we will get 3 documents as follows:

{
    a:abcdata,
    b:xyzdata,
    c:a1
}
{
    a:abcdata,
    b:xyzdata,
    c:a2
}
{
    a:abcdata,
    b:xyzdata,
    c:a3
}

3. MongoDB Aggregation Expression

Following are the aggregation expression in MongoDB with an example:

MongoDB Aggregation Expression

  • $sum – Sums up values from all documents in the collection.
db.examples.aggregate([{$group : {_id : "$by_student", num_dataflair : {$sum : "$likes"}}}])
  • $avg – Average of all documents in the collection.
db.examples.aggregate([{$group : {_id : "$by_student", num_dataflair : {$avg : "$likes"}}}])
  • $min – Minimum values of all documents in a collection.
db.examples.aggregate([{$group : {_id : "$by_student", num_dataflair : {$min : "$likes"}}}])
  • $max – Maximum values of all documents in a collection.
db.examples.aggregate([{$group : {_id : "$by_student", num_dataflair : {$max : "$likes"}}}])
  • $push – Inserts value in the resulting document.
db.examples.aggregate([{$group : {_id : "$by_student", url : {$push: "$url"}}}])

How to Execute MongoDB in Java Program?

  • $addToSet – Inserts value in resulting document without creating duplicates.
db.examples.aggregate([{$group : {_id : "$by_student", url : {$addToSet : "$url"}}}])
  • $first – First document from the source documents according to grouping.
db.examples.aggregate([{$group : {_id : "$by_student", first_url : {$first : "$url"}}}])
  • $last – Last document from the source documents according to grouping.
db.examples.aggregate([{$group : {_id : "$by_student", last_url : {$last : "$url"}}}])

4. Conclusion

Hence, we studied about Aggregation in MongoDB with types: Aggregation Pipeline, Map-Reduce Function, and Single Purpose Aggregation Method with their examples. Along with this, we discussed the stages of aggregation pipeline and expression used in aggregation. Hope, you found it useful. Please do comment for queries.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.