# Up Next: Aggregation and Indexes

Look back at what Module 3 lets you design, see it applied to a whole store's screens, and see what Module 4 adds, reports built from a year of orders and indexes that keep them fast.

Journeyman · MongoDB · Updated November 19, 2026

By Gregor Dzierzon

## After this lesson you can

- Describe what the aggregation pipeline and indexes will let you do with a year of a store's orders

## Key takeaways

- After Module 3, you can design documents for a whole store, starting from the questions its screens ask.
- Embed what's read together, reference what's shared, copy a few fields on purpose, and add a validator for the rules that matter.
- A conditional update keeps the stock right, a transaction keeps two documents in step, and some data fits a relational database better.
- Module 4 turns stored orders into reports with the aggregation pipeline, and makes queries fast with indexes that explain() proves.

At the start of this module, a document was something you wrote one at a time. Now you can design the documents for a whole store, explain each choice, and keep its stock right on a busy evening. That's the part of MongoDB work that decides whether an app stays fast and correct as it grows. This lesson looks back at what you can do, puts it together on a whole store, and gives you a first look at Module 4.

## What you can do now

After Module 3, you can:

- **model from the app's questions:** list the screens, then the queries behind them, then the documents;
- **embed or reference:** keep an order's lines inside the order, and link each order to its customer by `_id`, choosing by the relationship (one-to-few, one-to-many, many-to-many);
- **use the common patterns:** copy a product's name and price into an order line, keep a product's five newest reviews and its average rating up to date, and keep bikes, helmets and lights in one collection told apart by `type`;
- **set rules** with a `$jsonSchema` validator, and read the error a bad document gets;
- **keep a store's data right:** sell the last bike to exactly one shopper with a conditional update, and keep a stock change and an order together with a transaction;
- **say when a relational database fits better,** and why many apps use both.

## Putting it together: a whole store

Modeling a whole online store for documents starts from its screens and the questions behind each one:

- a **product page** with the product and its newest reviews;
- a **category page** that lists the products of one kind;
- a **cart**, and a **checkout** that never oversells;
- **my orders**, a customer's orders, newest first.

From those, you can now write the documents for products of several kinds, customers, carts, orders and reviews; a validator for products; and the query each screen needs. Then comes the part people ask about in interviews: saying, for each relationship, whether you embedded or referenced and why, and naming one place where a relational database would have been easier. Anyone can store data, and fewer people can explain the shape they chose.

## Up next: a year of orders

A store that's been open a year has thousands of orders, and its owner starts asking questions about all of them at once. Which products sold best? What did each month bring in? Who are the best customers? Here's a taste, on the three orders of this chapter's small set, the ones *If You Know SQL: One Store Designed Both Ways* shows: how many of each product have been sold.

```mongodb
db.orders.aggregate([
  { $unwind: "$lines" },
  { $group: { _id: "$lines.name", sold: { $sum: "$lines.quantity" } } },
  { $sort: { sold: -1, _id: 1 } }
])
```

```text
[
  { _id: 'Beam 400 Light', sold: 2 },
  { _id: 'Smooth Chain Oil', sold: 2 },
  { _id: 'Gravel Runner', sold: 1 },
  { _id: 'Trailhead Helmet', sold: 1 }
]
```

That's an **aggregation pipeline**: the orders pass through stages in order, like parts on a conveyor belt. `$unwind` gives each order line a document of its own, `$group` adds up the quantities for each product name, and `$sort` puts the best sellers first.

## What Module 4 unlocks

Module 4 teaches the pipeline stage by stage, on a year of Riverbend's orders:

- **reports:** `$match` to choose the orders, `$group` for totals, counts and averages, `$project` and `$sort` to shape the answer;
- **lists inside documents and other collections:** `$unwind` for order lines, and `$lookup` to bring in each order's customer;
- **dates:** sales by day and by month, the data behind a sales chart;
- **speed:** why a query slows down as a collection grows, what an **index** is (the book's index that saves you reading every page), and how `explain()` proves your query uses one.

Together, the two halves give a dashboard its numbers: right answers from pipelines, made fast with indexes.

## Check yourself

2 questions to try on the lesson page.

## So what

Module 3 gave your data a shape you can explain. Module 4 asks that data questions across a whole year, and answers them fast enough for a dashboard.

## Key terms

- [Data Modeling in MongoDB](https://www.ask-codey.com/glossary/data-modeling-in-mongodb)
- [Aggregation Pipeline](https://www.ask-codey.com/glossary/aggregation-pipeline)
- [Index in MongoDB](https://www.ask-codey.com/glossary/index-in-mongodb)

## Related

- [Exercise: Checkout Without Overselling](https://www.ask-codey.com/lessons/exercise-mongodb-checkout-without-overselling)
- [When a Relational Database Is the Better Choice](https://www.ask-codey.com/lessons/mongodb-when-relational-is-better)

Source: https://www.ask-codey.com/lessons/mongodb-up-next-aggregation-and-indexes
