Aggregation

Lesson 10
Author : Afrixi
Last Updated : February, 2023
MongoDB - noSQL Database
This course covers the basics of working with MongoDB.

Aggregation in MongoDB is the process of performing complex data analysis operations on collections and returning the results in a structured manner. It is a powerful feature that allows you to perform a wide variety of data analysis tasks, such as grouping, filtering, and computing statistics.

The aggregation framework in MongoDB provides a set of operators and stages that can be combined to perform complex data analysis tasks. Here are some examples of the operators and stages available in the aggregation framework:

$match: filters the documents in the collection based on a set of criteria.

$group: groups the documents in the collection based on a set of criteria and computes aggregate values for each group.

$project: reshapes the documents in the collection by selecting or transforming fields.

$sort: sorts the documents in the collection based on a set of criteria.

$limit: limits the number of documents returned by the aggregation pipeline.

$skip: skips a number of documents in the collection before processing the remaining documents.

$lookup: performs a left outer join between two collections.

Here’s an example of an aggregation pipeline that groups documents in a collection by a specific field and computes the average value of another field for each group:

db.sales.aggregate([
   {
      $group: {
         _id: "$product",
         avgQuantity: { $avg: "$quantity" }
      }
   }
])

In this example, the aggregation pipeline starts with the $group stage, which groups documents by the “product” field and computes the average value of the “quantity” field for each group. The results of the aggregation pipeline will be a list of documents, with each document representing a group and its associated average value.

Aggregation is a powerful tool that can be used to perform a wide variety of data analysis tasks in MongoDB. It is an essential skill for anyone who wants to work with large datasets and perform complex data analysis tasks.