# Exercise: Best Sellers by Month

Write five report pipelines for Riverbend Bike Shop's sales dashboard, from its top 5 products by units sold to its sales in each month of 2026 and each customer's spending with their name, and let the tests check the rows each one gives back.

Journeyman · MongoDB · Updated November 19, 2026

By Gregor Dzierzon

Riverbend Bike Shop's owner wants a sales dashboard: how many line items the shop has sold, its top 5 products by units, its sales in each month of 2026, what each customer has spent, and which customers haven't ordered yet. Each tile on the dashboard is one aggregation pipeline, and you'll write all five in mongosh syntax. Tests run your pipelines on Riverbend's 8 customers and 40 orders and check every row they give back.

## Get the exercise code

Open this lesson's GitHub repository, [github.com/ask-codey/exercise-mongodb-best-sellers-by-month](https://github.com/ask-codey/exercise-mongodb-best-sellers-by-month), also linked from the **Exercise** box beside the lesson. Clone it into your `mongodb-course` folder. In VS Code, open the `mongodb-course` folder, open the terminal with **Terminal > New Terminal**, and type:

```bash
git clone https://github.com/ask-codey/exercise-mongodb-best-sellers-by-month.git
```

Git makes a folder, `exercise-mongodb-best-sellers-by-month`, with the exercise code inside. Open that folder in VS Code (**File > Open Folder**), open its terminal, and install the packages the tests use:

```bash
npm install
```

The folder holds:

- `queries.js`: the five functions you'll finish.
- `data/customers.json` and `data/orders.json`: Riverbend's 8 customers and 40 orders, from November 2025 to December 2026, in the same shape as *$unwind: One Document per Array Item*'s sales.
- `data/load-into-mongosh.js`: loads them into mongosh, so you can try a pipeline there first.
- `test/`: the tests, and the helper that runs your pipelines for them. You don't need to change either.

## What to do

Every function in `queries.js` gets `db`, which works like it does in mongosh, and returns an empty pipeline for now, which gives back every document unchanged. The comment above each function says exactly which rows it should give back, with examples. Here is the first one:

```javascript
/*
 * 1. Count the line items in all 40 orders, the canceled one and last year's included: one per item in an order's
 *    items array.
 *
 *    Gives back one document: [ { lineItems: 69 } ]
 */
export function countLineItems(db) {
  return db.orders.aggregate([]);
}
```

Replace the empty pipeline, `[]`, in each function with stages of your own:

1. **`countLineItems`**: the number of line items in all 40 orders, as one document, `[ { lineItems: 69 } ]`.
2. **`topFiveProducts`**: the 5 products that sold the most units in the shipped orders (`status: "shipped"`), most first. Each row is `{ _id: <the product's name>, units: <its units sold> }`, starting with `{ _id: "Chain Care Kit", units: 25 }`.
3. **`salesByMonth`**: the shipped orders placed in 2026, one row per month on Riverbend's clock (`timezone: "America/Los_Angeles"`), January first. Each row is `{ _id: "2026-01", sales: <the totals added up>, orders: <how many> }`: 12 rows, the first `{ _id: "2026-01", sales: 288, orders: 3 }`.
4. **`customerSpend`**: each customer's total on shipped orders, with their name brought in from `customers` by `$lookup`, most spent first. Each row is only `{ name, spent }`: 6 rows, from `{ name: "Jordan", spent: 4199 }` to `{ name: "Ken", spent: 509 }`.
5. **`customersWithoutOrders`**: starting from `customers`, the customers with no orders yet, each row only `{ _id, name }`: Maya (4) and Ana (8).

You can try each pipeline in mongosh first. In the exercise folder's terminal, start `mongosh`, type `use best_sellers` to choose a practice database, then `load("data/load-into-mongosh.js")` to put the customers and orders in it. When a pipeline gives the right rows there, copy it into its function after `return`.

## Check your work

Run the tests from the exercise folder:

```bash
npm test
```

At first every test fails. That's expected: each pipeline you finish turns its test green. When a test fails, its message lists the rows it expected and the rows your pipeline gave, for example *2) topFiveProducts should give the top 5 products by units sold in the shipped orders, most first. The canceled order 7031 doesn't count: with it, Beacon Rear Light would show 24.*

The tests start their own throwaway MongoDB, so they never change your databases. If you installed MongoDB in *Setup: MongoDB, mongosh and Compass*, they use it.

## Hints

- **Build one stage at a time.** Run the first stage in mongosh, look at what it hands on, then add the next. *The Aggregation Pipeline: Data on a Conveyor Belt* shows the habit.
- **Products live in arrays.** Units per product need each line item on its own first; *$unwind: One Document per Array Item* shows how, and what it does to a count.
- **Names live in another collection.** An order holds only `customerId`. *$lookup: Bringing in Related Documents* brings the customer in, and shows what a customer with no orders gets.
- **Watch whose clock.** Two orders were placed late on the last evening of a month in Riverbend, which is already the next month in UTC. *Reports by Day and Month: Dates in a Pipeline* shows the option that counts them in the right month.
- **Only shipped orders count** in functions 2, 3 and 4. The canceled order still has its items, so a pipeline without the `$match` gets different numbers.

## When you're done

Every test is green, and `npm test` prints:

```text
✔ 1) countLineItems gives [ { lineItems: 69 } ] (1089.545ms)
✔ 2) topFiveProducts gives the 5 best sellers of the shipped orders, from Chain Care Kit (25 units) to Summit Helmet (10) (15.9763ms)
✔ 3) salesByMonth gives the 12 months of 2026 on Riverbend's clock, with 2026-02 at 286 from 4 orders (15.5409ms)
✔ 4) customerSpend gives the 6 customers who ordered, with their names, from Jordan (4199) to Ken (509) (15.4461ms)
✔ 5) customersWithoutOrders finds Maya (4) and Ana (8) (16.0688ms)
ℹ tests 5
ℹ suites 0
ℹ pass 5
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 1514.5641
```

The times in parentheses will be different on your computer. Now look at where the `$lookup` sits in your `customerSpend`. How many times does it search `customers`, and how many would it search if it came first? If the dashboard showed customer names on every page view, what would you change in the orders instead?

## Submit your exercise

The exercise is how you finish this lesson. When your work is done, put it on GitHub in a repo of your own, then submit that repo's address here.

1. On GitHub, click **+**, then **New repository**. Name it (for example `exercise-mongodb-best-sellers-by-month`), choose **Public**, and click **Create repository**. Leave out the README: your folder already has one.
2. In VS Code's terminal, in your exercise folder, send your work to your new repo. Point your copy at your own repo, then commit and push:

   ```bash
   git remote set-url origin https://github.com/your-name/exercise-mongodb-best-sellers-by-month.git
   git add .
   git commit -m "My solution"
   git push -u origin main
   ```

3. Paste your repo's address, such as `https://github.com/your-name/exercise-mongodb-best-sellers-by-month`, into **Submit your exercise** on this page. The site checks that the repo is public and holds your commits, and then the lesson is complete.

Changed something after you submitted? Push it, and submit again: your latest submission is the one that counts.

## So what

Best sellers, a monthly chart, top customers by name and a list for a welcome email: these are the tiles of a real sales dashboard, and each one is a pipeline you can now write, check row by row, and explain stage by stage.

## Key terms

- [$unwind](https://www.ask-codey.com/glossary/unwind)
- [$lookup](https://www.ask-codey.com/glossary/lookup)
- [$dateToString](https://www.ask-codey.com/glossary/datetostring)
- [Aggregation Pipeline](https://www.ask-codey.com/glossary/aggregation-pipeline)

## Related

- [$unwind: One Document per Array Item](https://www.ask-codey.com/lessons/mongodb-aggregation-unwind)
- [$lookup: Bringing in Related Documents](https://www.ask-codey.com/lessons/mongodb-aggregation-lookup)
- [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/exercise-mongodb-best-sellers-by-month
