Open In App

Aggregation Pipeline Stages in MongoDB

Last Updated : 06 Oct, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In MongoDB, an aggregation pipeline is a series of steps to process and analyze data. Each stage performs a specific task, such as filtering, grouping, or sorting, and passes the results to the next stage. Combining these stages makes it easy to change and understand the data.

Syntax:

db.<collection_name>.aggregate(pipeline, options) 

or 

db.<collection_name>.aggregate( [ { <stage1> }, {<stage2>},... ] )

  • db.<collection_name>: Refers to the collection to work on.
  • .aggregate(): Starts the aggregation process.
  • pipeline: A list of stages (steps) that process the data. Each stage is written inside {}.
  • options: Extra settings (optional) to control the aggregation.\
  • [ { <stage1> }, { <stage2> }, ... ]: Stages run in order, one after another.

Aggregation Stages

Aggregation stages are individual steps that work on data to filter, group, or sort it.

1. $addFields Stage

$addFields stage is used to add new fields to the existing documents. It returns documents that contain all the existing fields and newly added fields.

Syntax :

db.<collection_name>.aggregate(
            [
                { 
                      $addFields: { <newField1>: <expression>, <newField2>: <expression> ... }
               }
 ]);

Example:

Let's suppose we have to Add a new field isActive with the value true to every document in the employee_details collection using the MongoDB aggregation pipeline.

Query:

db.employee_details.aggregate([
       { 
          $addFields: {"isActive" : true }
       }
])

Output:

$addFields Stage
 

2. $set Stage

$set stage is used to add new fields to the existing documents. It returns documents that contain all the existing fields and newly added fields. It is the same as that of the $addFields stage. If the name of the new field is the same as an existing field (including _id), $set overwrites the existing value of that field with the new value.

Syntax:

db.<collection_name>.aggregate(
           [
               { 
                     $set: { <newField>: <expression>, ... }
              }
]);

Example:

Let''s suppose we have to Add a new field `company_name` with the value `GFG` to every document in the `employee_details` collection using the MongoDB aggregation pipeline.

Query:

db.employee_details.aggregate([
       { 
          $set: {"company_name" : "GFG"}
       }
])

Output:

$set Stage
 

Note: $set stage is new staging in version 4.2.

3. $unset Stage

$unset stage is used to remove/exclude fields from the documents. If you want to remove the field or fields from the embedded document then use the dot notation. $unset stage is new staging in version 4.2.

Syntax:

  • To remove a single field, The $unset stage takes a string that specifies the field to remove.

     db.<collection_name>.aggregate(
          [
              { 
                 $unset: "<field>" 
              }
         ]);

  • To remove multiple fields, The $unset takes an array of fields to remove.

db.<collection_name>.aggregate(
          [
              { 
                 $unset: [ "<field1>", "<field2>", ... ]
              }
         ]);

Example:

Let''s suppose we have to Remove the nested field `address.phone.type` from every document in the `employee_details` collection using the MongoDB aggregation pipeline.

Query:

  db.employee_details.aggregate([  
   {
      $unset: "address.phone.type"
   }
]);

Output:

$unset Stage
 

4. $project Stage

$project stage is used to include fields, exclude _id field, add new fields and replace the existing field value in the output document. The following is the table that describes the specification and its description.

SpecificationDescription
field : <1 or true>include the field in the output document 
_id : <0 or false>exclude the _id from the output document
field : <expression>add the new field or replace the existing field value
field : <0 or false>exclude the field from the output document.
If you use the field to exclude instead of _id then you 
can't use any other specification.

Syntax:

db.<collection_name>.aggregate(
        [
            { 
               $project: { <specification>}
            }
 ]) 

Example: 

Let''s suppose we have to Retrieve only the `firstName` and `lastName` fields from the `name` object and the `salary` field for every document in the `employee_details` collection, excluding the `_id` field, using the MongoDB aggregation pipeline.

Query:

db.employee_details.aggregate(
 [
   {
        $project:{"name.firstName":1, "name.lastName":1, "salary":1, "_id":0}
   }
]);

Output:

$project Stage
 

5. $count Stage

$count returns the count of the documents present in the selected collection. 

Syntax:

db.<collection_name>.aggregate(
           [
               { 
                     $count: <String>
              }
 ]);

  • <string> is the name of the output field which has the count as its value.

Example:

Let''s suppose we have to Count the total number of documents in the `employee_details` collection and return the result as `employees_count` using the MongoDB aggregation pipeline.

Query:

db.employee_details.aggregate(
   [
       {  $count: "employees_count" }
]);

Output:

$count Stage
 

Note: To get count of the documents in the collection we can also use db.<collection_name>.count(). It only returns the count we can't specify any output field.

6. $unwind Stage

$unwind stage is used to deconstruct an array field from the input documents to output documents. If mentioned field path is not an array but is also not missing, null, or an empty array then $unwind treats the non-array field as a single element array and the includeArrayIndex for that field is null.

Syntax:

db.<collection_name>.aggregate(
     [
         {
             $unwind:
             {
                  path: <field path>,
                  includeArrayIndex: <string>,
                  preserveNullAndEmptyArrays: <boolean>
             }
        }
]);

In the above syntax:

  • Path specifies which field you want to unwind.
  • includeArrayIndex includes array index only for array, not an application for fields that are not an array, null or empty.
  • preserveNullAndEmptyArrays, If true then this option is used to include documents whose field is missing, null or empty array field. By default it is false.

Example:

Let''s suppose we have to Skip the first 3 documents and then unwind the `skills` array field in each remaining document in the `employee_details` collection using the MongoDB aggregation pipeline.

Query:

db.employee_details.aggregate(
   [
       {$skip : 3},
       { $unwind: "$skills" }
   ]
);

Output:

$unwind Stage
 

Note: $unwind stage does not include documents whose field is missing, null or empty array until you use preserveNullAndEmptyArrays : true

7. $limit Stage

$limit stage is used to limit the number of documents passed to the next stage in the pipeline.

Syntax:

db.<collection_name>.aggregate(
    [
        {
           $limit: <positive 64-bit integer> 
       }
 ]);

Example: 

Let's suppose we have to Limit the results to the first 2 documents and then count these documents, returning the result as `employees_count`, using the MongoDB aggregation pipeline.

Query:

db.employee_details.aggregate(
  [
      {$limit : 2},
      {$count : "employees_count" }
  ]);

Output:

$limit Stage
 

8. $sort Stage

$sort stage is used to sort the documents. If sorting is on multiple fields, the sort order is evaluated from left to right. 
For example, documents are first sorted by <field1> and then documents with the same <field1> values are further sorted by <field2>.

Syntax:

db.<collection_name>.aggregate(
   [
       { $sort: { <field1>: <sort order>, <field2>: <sort order> ... } }
  ]);

<field> represents the document field on the basis of which we sort the document either ascending or descending depends on the <sort-order>. <sort order> has the following values :

    Value             Order
    1 Ascending 
   -1Descending

Example of $sort in Ascending Order

Let''s suppose we have to Sort the documents in the `employee_details` collection in ascending order based on the `firstName` field in the `name` object using the MongoDB aggregation pipeline.

Query:

db.employee_details.aggregate(
  [
      {$sort : {"name.firstName" : 1}}
  ]);

Output:

 $sort Stage - Ascending Order
 

Example of $sort in Descending Order

Let''s suppose we have to Sort the documents in the `employee_details` collection in descending order based on the `firstName` field in the `name` object using the MongoDB aggregation pipeline.

Query:

db.employee_details.aggregate(
    [
      {$sort : {"name.firstName" : -1}}
  ]);

Output:

 $sort Stage - Descending Order
 

Note: You can sort a maximum of 32 keys.

9. $sortByCount Stage

$sortByCount stage is used to group the documents on the basis of field path (document field) and then count the number of documents in each different group and the documents are sorted by count in descending order. The $sortByCount stage returns the documents.

Syntax: 

db.<collection_name>.aggregate(
   [
       {$sortByCount : "<field path>"}
   ]);

Example:

Let''s suppose we have to Create a MongoDB aggregation pipeline to list and count the occurrences of each skill across all employees in the `employee_details` collection after unwinding the `skills` array field.

Query:

    db.employee_details.aggregate(
   [
      {$unwind : "$skills"},
      {$sortByCount : "$skills"}
  ]);

Output:

$sortByCount Stage
 

Note: The $sortByCount stage is equivalent to the following: $group + $sort sequence: { $group: { _id: <expression>, count: { $sum: 1 } } }, { $sort: { count: -1 } }

10. $skip Stage

$skip stage is used to skip the number of documents and passes the remaining documents to the next stage in the pipeline. It returns the remaining documents.

Syntax:

 db.<collection_name>.aggregate(
   [ 
       { $skip: <positive 64-bit integer> }
  ]);

Example:

Let''s suppose we have to skip the first 3 documents in the `employee_details` collection using the MongoDB aggregation pipeline.

Query:

  db.employee_details.aggregate(
    [
      {$skip : 3}
   ]);

Output:

$skip

Article Tags :

Explore