Aggregation Tutorial
Aggregation Tutorial
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.
$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.
db.collectionName.aggregate(pipeline, options)
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
$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).
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.