# Up Next: MongoDB in an App

Look back at the reports and indexes you can build after Module 4, see them work together behind a store's dashboard, and see what Module 5 adds, the same queries running behind a real app's API.

Journeyman · MongoDB · Updated November 19, 2026

By Gregor Dzierzon

## After this lesson you can

- Choose the Module 4 tool a Riverbend task needs, from pipeline stages to indexes and explain

## Key takeaways

- After Module 4, you can turn a store's orders into reports with the aggregation pipeline, from $match and $group to $unwind, $lookup and dates.
- You can make a query fast with the right index, single-field, compound, unique or text, and prove it with explain.
- A report and an index work together, because a pipeline's first $match can use an index like a find can.
- Module 5 puts these queries behind a Node.js API with the official driver, and tests them against a throwaway database.

At the start of this module, you could find orders one query at a time. Now you can answer questions about all of them at once, and keep those answers fast as the orders pile up. Dashboards, reports and every page that has to stay quick on a busy day are built from exactly these two skills. This lesson looks back at what you can do, puts it together on a store's dashboard, and gives you a first look at Module 5.

## What you can do now

After Module 4, you can:

- **build reports** with the aggregation pipeline: `$match` to choose the documents, `$group` for totals, counts and averages, and `$project`, `$set`, `$sort` and `$limit` to shape the answer;
- **reach into arrays and other collections** with `$unwind` for each order's items and `$lookup` for its customer;
- **group by date:** sales by day and by month, in the shop's own time zone;
- **see why a query is slow** with `explain()`: a `COLLSCAN` that reads every document, or an `IXSCAN`, and how much it read for what it returned;
- **choose the index:** a single-field index, a compound index in the order equal, sort, range, a unique index that refuses a repeated email, and a text index for a search box.

Here are both halves at once, on Riverbend's 100,000 orders from *Why Queries Get Slow, and What an Index Is*: delivered revenue for each month of 2026.

```mongodb
db.orders.aggregate([
  { $match: { status: "delivered", orderedAt: { $gte: ISODate("2026-01-01"), $lt: ISODate("2027-01-01") } } },
  { $group: { _id: { $month: "$orderedAt" }, revenue: { $sum: "$total" } } },
  { $sort: { _id: 1 } }
])
```

```text
[
  { _id: 1, revenue: 1771940 },
  { _id: 2, revenue: 1597962 },
  { _id: 3, revenue: 1771089 },
  { _id: 4, revenue: 1714297 },
  { _id: 5, revenue: 1772424 },
  { _id: 6, revenue: 1715161 },
  { _id: 7, revenue: 1770156 },
  { _id: 8, revenue: 1773189 },
  { _id: 9, revenue: 971146 }
]
```

September is short because the orders stop on September 18. Without an index, the `$match` reads all 100,000 orders. With `db.orders.createIndex({ status: 1, orderedAt: 1 })`, an exact match then a range, explain shows it reading only the 23,772 delivered orders of 2026, through an `IXSCAN`.

## Putting it together: a sales dashboard

A store's sales dashboard puts the whole module to work. Each tile on it is a pipeline:

- **revenue by month**, for a chart;
- the **top 10 products** by units sold;
- the **top customers**, with their names brought in from the customers;
- the **average order value** for each payment method.

Each one then gets the index that makes its first stage an index scan, with an explain report to prove it. It's the kind of work a data team does every week: a right answer first, then a fast one, with the proof.

## Which tool fits?

| The job | The tool |
|---|---|
| Totals, counts or averages per group | `$group`, after a `$match` |
| One document per item in an array | `$unwind` |
| Bring in a related document | `$lookup` |
| Sales by month | `$month` or `$dateToString` in `$group` |
| Find out why a query is slow | `explain("executionStats")` |
| One field searched often | a single-field index |
| A filter and a sort together | a compound index: equal, sort, range |
| No two documents with the same email | a unique index |
| A search box over names and descriptions | a text index and `$text` |

## What Module 5 unlocks

Every query you've written so far ran in `mongosh`. In an app, the same queries run inside the server's code, when a page asks for them. Module 5, *MongoDB in an App*, puts them there, for the Maple Street Pet Adoption Center's API:

- **the starter API:** the little JavaScript a ready-made Express API uses, and a tour of its routes;
- **the official Node.js driver:** connecting with a connection string, the same `find` and `insertOne` you know written with `await`, a data layer that answers errors properly, and tests that run against a throwaway database;
- **the same work elsewhere:** Mongoose, the C# driver, and what Azure Cosmos DB's MongoDB API changes.

By its end, you can replace a handed-over API's in-memory store with MongoDB: listings with filters and pages, adoption applications, a validator, and the indexes its listing queries need.

## Check yourself

2 questions to try on the lesson page.

## So what

You can ask a store's data questions across two years and answer them fast enough for a dashboard. Module 5 puts those answers behind an app, where real people ask them.

## Key terms

- [Aggregation Pipeline](https://www.ask-codey.com/glossary/aggregation-pipeline)
- [Index in MongoDB](https://www.ask-codey.com/glossary/index-in-mongodb)
- [explain()](https://www.ask-codey.com/glossary/explain)
- [MongoDB Driver](https://www.ask-codey.com/glossary/mongodb-driver)

## Related

- [Exercise: Fast Order Lookups](https://www.ask-codey.com/lessons/exercise-mongodb-fast-order-lookups)
- [Reports by Day and Month: Dates in a Pipeline](https://www.ask-codey.com/lessons/mongodb-aggregation-reports-by-month)

Source: https://www.ask-codey.com/lessons/mongodb-up-next-mongodb-in-an-app
