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:
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.
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.
Match the documents based on specified criteria provided to `$match` operator.
{ $match: { <query_criteria> } }
{ $match: { accountActive: true } }
Groups documents by a specified key and performs aggregation operations on the grouped data.
{ $group: { _id: <group_by_field>, <aggregation_operations> } }
{ $group: { _id: "$country" } }
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.
{ $project: { <projection_specification> } }
// $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 } }
Sorts documents (Ascending or Descending) based on specified fields.
{ $sort: { <sorting_criteria> } }
{ $sort: { population: 1 } } // ASC { $sort: { population: -1 } } // DESC
Limits the number of documents passed to the next stage in the pipeline.
{ $limit: <limit_value> }
{ $limit: 10 }
Deconstructs an array field from the input documents to output a document for each element.
{ $unwind: <array_field> }
{ $unwind: "$cities" }
Performs a left outer join to another collection in the same database to filter in documents from the "joined" collection for processing.
{ $lookup: { from: <collection_to_join>, localField: <field_from_input_docs>, foreignField: <field_from_joined_docs>, as: <output_array_field> } }
// 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" }, ]
db.employees.aggregate([ { $lookup: { from: "department", localField: "employeeId", foreignField: "_id", as: "employeesDetails" } } ])
Skips the specified number of documents in the result set.
{ $skip: offset_value }
{ $skip: 0 } { $limit: 20 }
Limit and Skip we often use with pagination.
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.