Stop 06 · MongoDB Data Modeling & AggregationProjects · 1/1

Sales Report with the Aggregation Pipeline

Part of the MERN Stack Roadmap

In short

Every shop owner eventually asks: what sold best this month, and how much did we make? In this project you answer real business questions from raw order data using only the aggregation pipeline.

Build a monthly sales report for an online store from raw orders data, using $match, $group, $lookup and $sort to answer real business questions.

The jaadu works a small factory conveyor belt where mangoes are sorted, grouped into piles and finally poured into neat glass jars.

Think of it like this

A mango juice factory line

Mangoes go in one end. One station throws out the unripe ones ($match), another splits crates into single mangoes ($unwind), the next sorts them by variety and weighs each pile ($group), and the last puts labels on the jars ($project). Raw fruit in, a neat report out.

Watch it happen

Top products for November

$match
$unwind
$group
$lookup
$sort + $limit

1/5Keep paid orders from November: 3,400 orders.

The big ideas

What you need to know

Start from questions

Revenue per month, top 10 products, revenue by city, average order value. Write each as one pipeline.

$match first

Filter early so later stages handle fewer documents and indexes can be used.

Dates in $group

Group by month with $dateToString or $dateTrunc, and set a timezone like Asia/Kolkata.

Serve it from Express

Wrap each pipeline in a GET /api/reports/... route so a React dashboard can chart it later.

See it in code

Walk through the code

js
1// Top 10 products by revenue in November 2026 (IST)2db.orders.aggregate([3  { $match: {4      status: "paid",5      createdAt: { $gte: ISODate("2026-10-31T18:30:00Z"), $lt: ISODate("2026-11-30T18:30:00Z") },6  } },7  { $unwind: "$items" },8  { $group: {9      _id: "$items.productId",10      unitsSold: { $sum: "$items.qty" },11      revenue: { $sum: { $multiply: ["$items.price", "$items.qty"] } },12  } },13  { $lookup: { from: "products", localField: "_id", foreignField: "_id", as: "product" } },14  { $unwind: "$product" },15  { $project: { _id: 0, name: "$product.name", category: "$product.category", unitsSold: 1, revenue: 1 } },16  { $sort: { revenue: -1 } },17  { $limit: 10 },18]);19 20// Revenue per month21db.orders.aggregate([22  { $match: { status: "paid" } },23  { $group: { _id: { $dateToString: { format: "%Y-%m", date: "$createdAt", timezone: "Asia/Kolkata" } }, revenue: { $sum: "$total" } } },24  { $sort: { _id: 1 } },25]);

1/6Paid orders in November, with the boundaries shifted to Indian time.

Two report pipelines. Extend the project with revenue by city and average order value.

Save yourself the pain

Common mistakes

Oops

Summing $items.price without multiplying by quantity.

Do this instead

Revenue is price x qty for each line. Use $multiply inside $sum.

Oops

Grouping dates in UTC and wondering why late-night orders land in the wrong day.

Do this instead

Pass timezone: "Asia/Kolkata" to date operators, or shift your boundaries to IST.

Your mission

Build the full report

~75 min

Quick check

Why do you need $unwind before grouping by product here?

Go deeper

  1. 1Find revenue per month, top 5 products and top cities, such as Bengaluru, Delhi and Hyderabad.
  2. 2Add indexes for the fields your pipelines filter on and check explain().
  3. 3Expose one report through an Express endpoint that returns JSON.

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge