Aggregate Method

In MongoDB, aggregation is the process of transforming data sets and performing various operations on them to generate aggregated results.

MongoDB provides the Aggregation Framework, which is a flexible and powerful tool for performing aggregation operations on collections of documents.

Here's an overview of the Aggregation Framework and how it works:

Basic Structure:

The Aggregation Framework operates on collections of documents, processing them through a pipeline of stages.

Each stage performs a specific operation on the documents and passes the results to the next stage.

Example:

db.collection.aggregate([
    { $stage1: { <stage1_operator> }},
    { $stage2: { <stage2_operator> }},
    ...
    { $stageN: { <stageN_operator> }}
])

Each stage in the pipeline consists of an operator that specifies the operation to be performed.

Common Aggregation Stages and Operators:

$match:

Match the documents based on specified criteria provided to $match operator.

Syntax:

{ $match: { <query_criteria> } }

Example:

{ $match: { accountActive: true } }

$group:

Groups documents by a specified key and performs aggregation operations on the grouped data.

Syntax:

{ $group: { _id: <group_by_field>, <aggregation_operations> } }

Example:

{ $group: { _id: "$country" } }

$project:

Reshape documents by including, excluding, or adding fields. so that fields will be available across all aggregator stages and, if required, can be sent back to the client as well.

Syntax:

{ $project: { <projection_specification> } }

Example:

// $project basically create virtual document while looping in aggreator stages.
// 1 indicate: we need this attribute (data) in document.
// 0 indicate or not defining key in project operator will exclude from document.

{ $project: { country: 1, _id: 1, city: 1 } }

$sort:

Sorts documents (Ascending or Descending) based on specified fields.

Syntax:

{ $sort: { <sorting_criteria> } }

Example:

{ $sort: { population: 1 } } // ASC
{ $sort: { population: -1 } } // DESC

$limit:

Limits the number of documents passed to the next stage in the pipeline.

Syntax:

{ $limit: <limit_value> }

Example:

{ $limit: 10 }

$unwind:

Deconstructs an array field from the input documents to output a document for each element.

Syntax:

{ $unwind: <array_field> }

Example:

{ $unwind: "$cities" }

$lookup:

Performs a left outer join to another collection in the same database to filter in documents from the "joined" collection for processing.

Syntax:

{
  $lookup:
    {
      from: <collection_to_join>,
      localField: <field_from_input_docs>,
      foreignField: <field_from_joined_docs>,
      as: <output_array_field>
    }
}

Example:

// Department collection
[
  { _id: 1, employeeId: 101, deptName: "HR" },
  { _id: 2, employeeId: 102, deptName: "Business Analyst" },
  { _id: 3, employeeId: 103, deptName: "Director" }
]

// employees collection
[
  { _id: 101, name: "Alice" },
  { _id: 102, name: "Bob" },
  { _id: 103, name: "John" },
]

Example:

db.orders.aggregate([
  {
    $lookup: {
      from: "employees",
      localField: "employeeId",
      foreignField: "_id",
      as: "employeesDetails"
    }
  }
])

skip(offset):

Skips the specified number of documents in the result set.

Syntax:

{ $skip: offset_value }

Example:

{ $skip: 0 }
{ $limit: 20 }

Limit and Skip we often use with pagination.

Pagination Example:

  • On Page 1: we skipped 0 documents and the limit applied to 20.

  • On Page 2: we will skip the first 20 documents and the limit will be the same 20.