Learning MongoDB

0% completed

Previous
Next
MongoDB Aggregation

Aggregation in MongoDB is a powerful way to process data and return computed results. This framework is designed for efficient data processing and allows for complex transformations and analyses within the database. Aggregation operations group values from multiple documents together and can perform a variety of operations on the grouped data to return a single result.

Aggregation Framework

The MongoDB aggregation framework provides a means to calculate aggregated values without having to use Map-Reduce. The framework is based on the concept of a pipeline, where documents are passed through a sequence of stages, each of which performs a specific operation.

Aggregation Pipeline

The aggregation pipeline is a framework for data aggregation modeled on the concept of data processing pipelines. Documents enter a multi-stage pipeline that can transform them and output the aggregated results. Each stage in the pipeline performs an operation on the input documents and passes the result to the next stage.

Key Aggregation Pipeline Stages

StageDescription
$matchFilters the documents to pass only the documents that match the specified condition(s) to the next pipeline stage.
$groupGroups input documents by the specified _id expression and for each distinct grouping, outputs a document.
$projectPasses along the documents with only the specified fields to the next stage.

Basic Aggregation Pipeline Example

To understand the aggregation pipeline, consider the following example where we want to group user data by their age and count the number of users in each age group.

Example Setup

First, let's insert some documents into the users collection to work with:

db.users.insertMany([ { name: "Alice", age: 25, city: "New York" }, { name: "Bob", age: 30, city: "San Francisco" }, { name: "Charlie", age: 35, city: "Los Angeles" }, { name: "David", age: 30, city: "New York" }, { name: "Eve", age: 25, city: "San Francisco" } ])

Example Aggregation Pipeline

db.users.aggregate([ { $match: { age: { $gte: 25 } } }, { $group: { _id: "$age", count: { $sum: 1 } } }, { $sort: { _id: 1 } } ])

Explanation:

  1. $match: Filters documents to include only those where the age is greater than or equal to 25.
  2. $group: Groups documents by the age field and counts the number of documents in each group.
  3. $sort: Sorts the output documents by the _id field in ascending order.

Detailed Stage Descriptions

$match

The $match stage filters documents based on a specified condition. This stage limits the number of documents passed to the next stage of the pipeline.

Syntax

{ $match: { <field>: <value> } }

Example

db.users.aggregate([ { $match: { city: "New York" } } ])

This command filters the documents to include only those where the city is "New York".

$group

The $group stage groups documents by a specified expression and outputs one document per group. The output documents can contain computed fields that hold the result of an aggregation operation on the documents in each group.

Syntax

{ $group: { _id: <expression>, <field>: { <accumulator>: <expression> } } }

Example

db.users.aggregate([ { $group: { _id: "$city", totalAge: { $sum: "$age" } } } ])

This command groups documents by the city field and calculates the total age of users in each city.

$project

The $project stage reshapes each document in the stream, such as by adding new fields or removing existing fields.

Syntax

{ $project: { <field1>: <include|exclude>, <field2>: <include|exclude>, ... } }

Example

db.users.aggregate([ { $project: { name: 1, age: 1, _id: 0 } } ])

This command includes only the name and age fields in the output documents and excludes the _id field.

The MongoDB aggregation framework is a powerful tool for data analysis and transformation. By understanding and utilizing the various stages and operators available in the aggregation pipeline, you can perform complex queries and aggregations efficiently within your MongoDB database. This lesson provides an introduction to the key concepts and basic usage of the aggregation pipeline, laying the foundation for more advanced topics in subsequent lessons.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next