0% found this document useful (0 votes)
9 views

Aggregation Tutorial

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Aggregation Tutorial

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

What is Aggregation in MongoDB?

Aggregation is a way of processing a large number of documents in a collection by


means of passing them through different stages. The stages make up what is known as
a pipeline. The stages in a pipeline can filter, sort, group, reshape and modify
documents that pass through the pipeline.

One of the most common use cases of Aggregation is to calculate aggregate values for
groups of documents. This is similar to the basic aggregation available in SQL with
the GROUP BY clause and COUNT, SUM and AVG functions. MongoDB
Aggregation goes further though and can also perform relational-like joins, reshape
documents, create new and update existing collections, and so on.

How does the MongoDB aggregation pipeline work?

Here is a diagram to illustrate a typical MongoDB aggregation pipeline.

How does the MongoDB aggregation pipeline work?

Here is a diagram to illustrate a typical MongoDB aggregation pipeline.

 $match stage – filters those documents we need to work with, those that fit our
needs
 $group stage – does the aggregation job
 $sort stage – sorts the resulting documents the way we require (ascending or
descending)

The input of the pipeline can be a single collection, where others can be merged later
down the pipeline.

The pipeline then performs successive transformations on the data until our goal is
achieved.

This way, we can break down a complex query into easier stages, in each of which we
complete a different operation on the data. So, by the end of the query pipeline, we
will have achieved all that we wanted.
This approach allows us to check whether our query is functioning properly at every
stage by examining both its input and the output. The output of each stage will be the
input of the next.

There is no limit to the number of stages used in the query, or how we combine them.

MongoDB aggregate pipeline syntax

This is an example of how to build an aggregation query:

db.collectionName.aggregate(pipeline, options)

 where collectionName – is the name of a collection,


 pipeline – is an array that contains the aggregation stages,
 options – optional parameters for the aggregation

his is an example of the aggregation pipeline syntax:

pipeline = [

{ $match : { … } },

{ $group : { … } },

{ $sort : { … } }

MongoDB $match

The $match stage allows us to choose just those documents from a collection that we
want to work with. It does this by filtering out those that do not follow our
requirements.

MongoDB $project

It is rare that you ever need to retrieve all the fields in your documents. It is good
practice to return only those fields you need so as to avoid processing more data than
is necessary.

MongoDB $group

With the $group stage, we can perform all the aggregation or summary queries that
we need, such as finding counts, totals, averages or maximums.
Operator Meaning

$count Calculates the quantity of documents in the given group.

$max Displays the maximum value of a document’s field in the collection.

$min Displays the minimum value of a document’s field in the collection.

$avg Displays the average value of a document’s field in the collection.

$sum Sums up the specified values of all documents in the collection.

$push Adds extra values into the array of the resulting document.

MongoDB $out

This is an unusual type of stage because it allows you to carry the results of your
aggregation over into a new collection, or into an existing one after dropping it, or
even adding them to the existing documents (new in 4.1.2 version).

The $out stage must be the last stage in the pipeline.

MongoDB $unwind

You cannot work directly on the elements of an array within a document with
stages such as $group. The $unwind stage enables us to work with the values of the
fields within an array.
Where there is an array field within the input documents, you will sometimes need to
output the document several times, once for every element of that array.

Each copy of the document has the array field replaced with the successive element.

MongoDB $sort

You need the $sort stage to sort your results by the value of a specific field.

$addFields
It is possible that you need to make some changes to your output in the way of new
fields. In the next example, we want to add the year of the foundation of the
university.

MongoDB $count

The $count stage provides an easy way to check the number of documents obtained in
the output of the previous stages of the pipeline.

MongoDB $lookup

Using the $lookup, here is an aggregate query that merges fields from two collections.

MongoDB $sortByCount

This stage is a shortcut for grouping, counting and then sorting in descending order
the number of different values in a field.

You might also like