What is mongodb aggregation ?

Yeu Lind Yeo
3 min readApr 4, 2021

--

Mongodb aggregation is the most powerful tool when it came to grouping document in a collection with specific condition. Additional fields can be added during grouping too. It’s an operation used to process data with different conditions and output as one result. Conditions are separated into stages in the document and processed in several stages one by one. Result from the previous stages will be used in the stages and so on.

db.<collection>.aggregate([{stage1},{stage2},{stage3}])

Aggregation Stages Overview

Some examples on the aggregation stages and functionality:

$match — filter document by certain query

$group — group by certain criteria

$project — filter fields in the document

$sort — sort objects

$count — count number of object in documents

$limit — limit numbers of documents

$skip — skip certain amount of documents

$out — write results of aggregation into another collection

Aggregation stage operators

Each stage starts from the stage operators and followed by the expression

For example:

{ $<stageOperator> : {} }

{ $match: {age:{$gt:30}}

{ $group: {_id: “$age” }}

{ $sort: {count: -1 }}

Aggregation Expression

Expression refers the name of the fields of the input documents

$<fieldName>

{ $group: { _id: “$age” }}

Can refer to nested fields using dot notation

For example

{ $group : { _id: “$company.location.country”}}

Aggregation process

I’ll use match and group stage operator as the example here.

Let’s say we want to perform this operation :

Step 1) Filter person with age more than 30

Stage 2) Group person with the same age and get the sum of people with the same age

Match produce subset of documents to perform group operation. With this process, you’ll get brand new documents with age more than 30 .

Group combine documents with specific criteria. With this process, you’ll get sum of person belongs to the same criteria which is age in this example.

db.persons.aggregate([{$match: {age:{$gt:30}}},{$group: { _id:"$age", count: {$sum:1}}}]);

let’s say you have 100 documents and 5 of it are age > 30, you’ll get 10 documents from match to perform the group operation.

Result will be like .

{ “_id” : 33, “count” : 4 } { “_id” : 40, “count” : 5 } { “_id” : 34, “count” : 4 } { “_id” : 39, “count” : 5 } { “_id” : 38, “count” : 4 } { “_id” : 36, “count” : 5 } { “_id” : 37, “count” : 5 } { “_id” : 31, “count” : 8 } { “_id” : 32, “count” : 3 } { “_id” : 35, “count” : 1 }

To test this, first you need to insert documents first.

persons.json

db.persons.insertMany([array])

Then proceed with

db.persons.aggregate([{$match: {age:{$gt:30}}},{$group: { _id:"$age", count: {$sum:1}}}]);

You’ll get the same result.

Here you go, your first aggregation experience.

Clap for me if you like it, it would definitely motivates me to write more . 😂

Thanks

--

--

Yeu Lind Yeo

Coding is an art and I’m an artist 😎. Web developer at day, dreamer at night. Curiosity kills a cat and luckily I’m not a cat. 🐶