Learning Journey
136 views0 likes

MongoDB Aggregation

Aggregation in mongodb helps us to perform various operations on our documents on our database

Martins John Okafor
October 27, 2025
3 months ago

Aggregation in MongoDB.
Day 12 of the 120-days fullstack software development. Just imagine you want to process, transform, and analyze data, and you don't want to perform the task on your code to reduce the work load on your code, this is where the MongoDB aggregation comes in.
Aggregation operations performs different operations on multiple documents and return computed results for example, grouping data, filtering, counting, summing, averaging, sorting, etc.
MongoDB uses the Aggregation Pipeline — which is a sequence of stages where each stage transforms the documents before passing them to the next stage.
Here's how it goes: Each stage is an object that starts with a stage operator like $match, $group, $project, sort, limit, etc
$match: it helps in filtering documents and accepts comparison operators like: $gt, $lt, $eq, $ne, $in, $exists.
{ $match: { age: { $gt: 25, $lt: 40 } } }

$project: Selects or reshapes fields, can also be used to create computed fields.
{ $project: { fullname: { $concat: ["$firstName", " ", "$lastName"] } } }

$group: Groups documents by a key and performs aggregation operations on it.{
$group: {
_id: "$fieldName",
total: { $sum: "$amount" },
average: { $avg: "$amount" },
count: { $sum: 1 }
}
}

$sort: Sorts documents in ascending or depending order, -1 for depending order and 1 for ascending order. { $sort: { field: 1 } } // ascending
{ $sort: { field: -1 } } // descending

$limit and $skip: Limit or skip a certain number of documents. { $limit: 10 }
{ $skip: 5 }

$lookup: Performs an operation of joing documents between collections.
{
$lookup: {
from: "customers",
localField: "customerId",
foreignField: "_id",
as: "customerInfo"
}
}

$unwind: Deconstructs an array field into multiple documents more like the spread operator. { $unwind: "$arrayField" }

$addFields: Adds new fields to documents.
{ $addFields: { totalPrice: { $multiply: ["$price", "$quantity"] } } }

$replaceRoot / $replaceWith: Replaces the root document with a sub-document.
{ $replaceRoot: { newRoot: "$nestedObject" } }

$count: Counts documents in the pipeline.
{ $count: "totalDocuments" }

$facet: Works on multiple pipelines on the same input and returns results in one document.
{
$facet: {
totalCount: [{ $count: "count" }],
priceStats: [{ $group: { _id: null, avgPrice: { $avg: "$price" } } }]
}
}
$out: responsible for writing the result of the pipeline to a new collection.
{ $out: "newCollectionName" }

$merge: It can help in writing aggregation results to an existing collection (replaces $out in modern MongoDB).
{
$merge: {
into: "targetCollection",
whenMatched: "replace",
whenNotMatched: "insert"
}
}

Last updated: February 4, 2026

Written by: Martins John Okafor

Tags:

#mongoDB
#aggregation
#javascript
#coding
#programming

More Articles

Deep Technology's

We are on an unwavering mission to be Nigeria's most loved software company, helping people discover new problem-solving strategies to solve problems around the globe.

NO 33 Democracy Crescent, Gaduwa Estate Gudu,Abuja FCT, Nigeria

© 2026 Deep Technology's. All rights reserved.