Unlock the Power of Aggregation in MongoDB: A Beginner's Guide to Mastering Data Analysis

Unlock the Power of Aggregation in MongoDB: A Beginner's Guide to Mastering Data Analysis

What is Aggregation?

Aggregation in MongoDB helps to filter documents from top to bottom and it deals with metrics that have to do with getting proportion results.

Importation of Aggregation.

  • It gives you flexibility in how you want to output data.

  • It provides you to perform all query operations.

Aggregate Syntax

modelName.aggregate([
    stage a,
    stage b, 
    .......,
    .......,
    .......,
    stage n
])

From the image above, you have modelName which is referring to the name of the model called collection in MongoDB. In NodeJs when you create a model let's say the name of the model User has the following field: name, email, plan, age, and state. The image below shows how to create a model in NodeJs.

Then if you want to use the model with the aggregate syntax. you have invoked the variable name you used for the model name and therefore you write the code in the format below.

import { User } from '../model/user.model.ts';

User.aggregate([
    stage a, 
    stage b,
    .......,
    .......,
    .......,
    stage n
]);

The aggregate method takes an array of objects in which the objects are stages pipelines. In each stage of the document in the array, you have conditions guiding what the output should produce after filtering based on those conditions.

Sometimes there can be multiple objects of stages therefore in each stage, the result it produces in the first stage of filtering the document will be processed and passed the second stage, and then it filters again, and finally, after it gets to the last stage of the condition it will produce only the minimal output after being passed through all stages.

The stage operators include: $match, $group, $sort, $project, $skip and there are other but you will be working with this stage in this article and because there are mostly used in the operator.

Aggregation Expression

What is Aggregate Expression?

Aggregation expression refers to the field in a document. So therefore our expression points to the name field in the document. For example; from the image below after the stage operators. The expressions are $email and $age and there are referring to the field in the documents.

User.aggregate([
    { 
        $match: { 'email': '$email'}
    },    
    {
        $group: {
                "_id": "$age",
               }
    }
])

NB: match stage operator uses a standard MongoDB query and accepts all other queries.

Here is an image below to show you the expression in MongoDB aggregation and some other details.

Aggregation Methods in MongoDB

Methods in MongoDB aggregation are used in each stage of the aggregation framework or pipeline. In each stage, you have operations that are performed.

The image below shows the array's stage operators or stage conditions in each object.

User.aggregate([
    {
        $match: {}
    },
    {
        $group: {}
    }
    {
        $sort: {}
    },
])

We have each object which is also known as a stage or condition for filtering the document. In each stage, we have stage operators used to filter documents.

To get started you will need to have this repository MONGO-AGGREGATION set up on any of the preferred code text editors. Follow the steps below:

  • Clone the repository by entering git clone <repository-url>

  • Enter the yarn install to get all the dependencies installed in your codebase.

  • Then start the server yarn dev to start in development mode.

There are already some routes and controllers that hold the path and logic that is already written down. The process is just for you to go the route and send it on POSTMAN. Go to Run in Postman which we give you access to have the MongoDB collection on your postman application.

$match

This aggregate stage operator is used to filter documents by a certain query.

Go to your POSTMAN. Here is the postman link create some users and you will see a user route file, then try to run the POST method to create users for you.

Then you can try to fetch users with ages greater than 10 with the logic below. You can also try out a different value for yourself by just changing the value in the user controller.

Syntax:

{ $match: { <query> } }

const user = await User.aggregate([
    {
        $match: {
            age: {$gt: 10}
        }
    }
])

you should get the following output that consists of users with only an age greater than 10 as stated below

$group

This aggregation stage operator is used to group documents by certain criteria of a particular field. The $group stage operation is quite complex but with the syntax and trying it. you will get how it works.

Go to your postman and test the routes where you have the code logic below and you should get the following output with a distinct _id of user age.

Syntax:

{ $group: { _id: <expression>, <field1>:
{ <accumulator1>: <expression1> }, ...} }

// for example let say you have a plan model referencing user model and the user has selected different plans and have different total amount. you can get each individual total amount of the plan selected by the user.

const result = await User.aggregate([
    {
        $group: {
                "_id": "$age",
               }
    }
])

Using the aggregate method above. The result will produce a document of just one field which is the _id and the value of the documents will be distinct based on the field name used as the value for the _id property.

NB: The _id is a compulsory field that shows that we are calculating the different total amounts for different plans.

$sort

This aggregation stage operator is used to sort documents either by ascending or descending order of their arrangement in the database.

Try out the sort aggregate method on POSTMAN and you will get the following output sorted either by descending order -1 or ascending order 1

Syntax:

{ $sort: { field1: value1 }}

const result = await User.aggregate([
    {
        $sort: {
                age: -1
               }
    }
])

Here is the output after sending the request for the aggregate above.

$Project

This aggregration stage operator is used to select some certain fields from a collection.

Syntax:

wait User.aggregate([
            {
                // project the age field
                $project: {
                    name: 1
                }
            }
])

So basically it select name of field in the document and output only that field that is used in the $project stage without adding any other field to the result except the _id which is clearly stated that it's compulsory.

$Skip

This aggregation stage operator is used to skip or omit nth number of documents and passes the remaining documents

Syntax:

await User.aggregate([
            {
                // project the age field
                $project: {
                    name: 1
                },
            }, 
            {
                // limit by 2 document
                $limit: 2
            }
 ])

you are only getting a specified number of documents given to the $limit stage operator and here you are getting only two documents because you give it a value of 2.

Conclusion

Aggregation helps produce a calculated and summarized result from the documents input provided and helps in formatting the type of output you want in your results.

Other resources can help you learn more about aggregate in MongoDB. The links are w3school MongoDB

Thank you for reading! Feel free to ask me any questions.

I'd love to connect with you on Twitter | LinkedIn | GitHub

See you in my next blog article. Take care!!!