MongoDB Fundamentals: Part 2 - Getting to Know How aggregation and pipeline works

Table of contents
Before reading this article, I request everyone to first clear the basics of MongoDB.
mongodb_basic Clicking on this word will redirect you to the MongoDB basic article
Let's start with what aggregation is and how it works
Aggregation
this is user collection
[
{
_id: ObjectId("60d5f9a8f8d2eae6b0d9f9a1"),
name: "Skyltt Singh",
email: "skyltt@gmail.com",
phone: "9999990001",
address: "Delhi, India"
},
{
_id: ObjectId("60d5f9a8f8d2eae6b0d9f9a2"),
name: "Lalit Aswal",
email: "aswal@gmail.com",
phone: "9999990002",
address: "Dehradun, India"
},
{
_id: ObjectId("60d5f9a8f8d2eae6b0d9f9a3"),
name: "Kiddo Sharma",
email: "kido@gmail.com",
phone: "9999990003",
address: "Mumbai, India"
},
{
_id: ObjectId("60d5f9a8f8d2eae6b0d9f9a4"),
name: "Beny Mehra",
email: "beny@gmail.com",
phone: "9999990004",
address: "Pune, India"
}
]
assume we have customer collection
[
{
_id: ObjectId("60d5ec49f8d2eae6b0d9f9c1"),
userId: ObjectId("60d5f9a8f8d2eae6b0d9f9a1"),
customer: "Skyltt",
items: [
{ product: "Pen", qty: 2, price: 5 },
{ product: "Notebook", qty: 1, price: 15 }
],
status: "delivered",
orderDate: ISODate("2025-06-01T00:00:00Z")
},
{
_id: ObjectId("60d5ec49f8d2eae6b0d9f9c2"),
userId: ObjectId("60d5f9a8f8d2eae6b0d9f9a2"),
customer: "Aswal",
items: [
{ product: "Bag", qty: 1, price: 50 },
{ product: "Pen", qty: 4, price: 5 }
],
status: "pending",
orderDate: ISODate("2025-06-02T00:00:00Z")
},
{
_id: ObjectId("60d5ec49f8d2eae6b0d9f9c3"),
userId: ObjectId("60d5f9a8f8d2eae6b0d9f9a3"),
customer: "kido",
items: [
{ product: "Laptop", qty: 1, price: 600 },
{ product: "Mouse", qty: 1, price: 20 }
],
status: "delivered",
orderDate: ISODate("2025-06-05T00:00:00Z")
},
{
_id: ObjectId("60d5ec49f8d2eae6b0d9f9c4"),
userId: ObjectId("60d5f9a8f8d2eae6b0d9f9a4"),
customer: "Beny",
items: [
{ product: "Pencil", qty: 10, price: 1 }
],
status: "shipped",
orderDate: ISODate("2025-06-07T00:00:00Z")
}
]
Get the total sales (amount) for each customer
Here we are going to use $unwind, which is used to deconstruct an array field from the input documents, outputting one document for each element of the array
db.orders.aggregate([
{ $unwind: "$items" }, // Flatten array of items
{
$group: {
_id: "$customer",
totalAmount: {
$sum: {
$multiply: ["$items.qty", "$items.price"]
}
}
}
}
])
// output will be
[
{ "_id": "SkyLTT", "totalAmount": 25 },
{ "_id": "Aswal", "totalAmount": 70},
{ "_id": "kido", "totalAmount": 620},
{ "_id": "Beny", "totalAmount": 10 },
]
To find the top 3 customers by total purchase.
db.orders.aggregate([
{ $unwind: "$items" },
{
$group: {
_id: "$customer",
totalSpent: {
$sum: {
$multiply: ["$items.qty", "$items.price"]
}
}
}
},
{ $sort: { totalSpent: -1 } },
{ $limit: 3 }
])
// output will be
[
{
"_id": "kido",
"totalSpent": 620
},
{
"_id": "Aswal",
"totalSpent": 70
},
{
"_id": "SkyLTT",
"totalSpent": 25
}
]
to find the monthly sales report
db.customer.aggregate([
{
$group: {
_id: { $month: "$orderDate" }, // Group by month
monthlySales: {
$sum: {
$sum: {
$map: {
input: "$items",
as: "item",
in: { $multiply: ["$$item.qty", "$$item.price"] }
}
}
}
}
}
},
{ $sort: { "_id": 1 } }
])
// output will be
[
{
"_id": 6, // June
"monthlySales": 745
}
]
Join orders with customer details using $lookup
//input query
db.customer.aggregate([
{
$lookup: {
from: "user",
localField: "userId",
foreignField: "_id",
as: "userDetails"
}
},
{ $unwind: "$userDetails" },
{
$project: {
_id: 0,
customer: 1,
status: 1,
orderDate: 1,
items: 1,
"userDetails.name": 1,
"userDetails.email": 1,
"userDetails.phone": 1,
"userDetails.address": 1
}
}
])
// output
[
{
customer: "Skyltt",
status: "delivered",
orderDate: ISODate("2025-06-01T00:00:00Z"),
items: [
{ product: "Pen", qty: 2, price: 5 },
{ product: "Notebook", qty: 1, price: 15 }
],
userDetails: {
name: "Skyltt Singh",
email: "skyltt@gmail.com",
phone: "9999990001",
address: "Delhi, India"
}
},
{
customer: "Aswal",
status: "pending",
orderDate: ISODate("2025-06-02T00:00:00Z"),
items: [
{ product: "Bag", qty: 1, price: 50 },
{ product: "Pen", qty: 4, price: 5 }
],
userDetails: {
name: "Lalit Aswal",
email: "aswal@gmail.com",
phone: "9999990002",
address: "Dehradun, India"
}
},
{
customer: "kido",
status: "delivered",
orderDate: ISODate("2025-06-05T00:00:00Z"),
items: [
{ product: "Laptop", qty: 1, price: 600 },
{ product: "Mouse", qty: 1, price: 20 }
],
userDetails: {
name: "Kiddo Sharma",
email: "kido@gmail.com",
phone: "9999990003",
address: "Mumbai, India"
}
},
{
customer: "Beny",
status: "shipped",
orderDate: ISODate("2025-06-07T00:00:00Z"),
items: [
{ product: "Pencil", qty: 10, price: 1 }
],
userDetails: {
name: "Beny Mehra",
email: "beny@gmail.com",
phone: "9999990004",
address: "Pune, India"
}
}
]
In this guide, we explored how aggregation and pipelines work in MongoDB, using real-world examples like calculating customer sales, generating monthly reports, and joining collections with $lookup
. These techniques are powerful for transforming and analyzing data directly within MongoDB.
I hope you found this blog useful. If so, please consider sharing it with your friends, and I’d love to hear your feedback. You can reach out to me on Twitter at @lalitaswal2 — I’d be happy to hear your thoughts and suggestions for future topics.
Subscribe to my newsletter
Read articles from skyLTT directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

skyLTT
skyLTT
Working knowledge of MERN, LAMP, and Django and currently working as a MERN stack developer in KhiladiAdda