# Exercise: Model the Bike Shop's Orders

Add a customer, a product and an order to Riverbend Bike Shop's store in the right shape, embedding and referencing where each belongs, then answer three of the app's questions and explain each choice in a sentence.

Journeyman · MongoDB · NoSQL · Updated November 19, 2026

By Gregor Dzierzon

Riverbend Bike Shop's store is growing: a new customer has signed up, a new light has arrived, and Jordan has placed another order. Each needs a document in the store's shape, with the right data embedded and the right data referenced. Then three of the app's screens need their questions answered: Alex's "my orders" page, an order page and the Helmets category page. You'll write all six as mongosh statements, and tests check what each one added or found. Last, you'll say in one sentence why each piece of data is embedded or referenced.

## Get the exercise code

Open this lesson's GitHub repository, [github.com/ask-codey/exercise-mongodb-model-the-bike-shops-orders](https://github.com/ask-codey/exercise-mongodb-model-the-bike-shops-orders), 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-model-the-bike-shops-orders.git
```

Git makes a folder, `exercise-mongodb-model-the-bike-shops-orders`, 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 six functions you'll finish.
- `CHOICES.md`: four modeling choices, each waiting for your sentence.
- `data/`: Riverbend's store in four files (customers, categories, products and orders), the same as in *Model for the Questions Your App Asks*.
- `data/load-into-mongosh.js`: loads the store into mongosh, so you can try a statement there first.
- `test/`: the tests, and the helper that runs your statements 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 a placeholder for now. The comment above each function says exactly what to add or find. Here is the first one:

```javascript
/*
 * 1. Add a new customer: Maya, with _id 4 and the email "maya@example.com".
 *    Her address is 30 Ferry Lane, Riverbend, zip "97403". Embed it in her document as
 *    address: { street, city, zip }, the way the other customers' addresses are.
 *
 *    The customer collection then holds 4 customers.
 */
export function addCustomer(db) {
  return db.customers.insertOne({});
}
```

Replace the placeholder in each function with one mongosh statement of your own, after the word `return`. Functions 1 to 3 add one document each with `insertOne`; the test then reads the document you added and checks every field:

1. **`addCustomer`**: the customer `{ _id: 4, name: "Maya", email: "maya@example.com" }`, with her address embedded as `address: { street: "30 Ferry Lane", city: "Riverbend", zip: "97403" }`.
2. **`addProduct`**: the product `{ _id: 107, name: "Trail Light 800", price: 49 }`, with `categoryIds: ["lights", "commuting"]` (an array of the categories' ids, not copies of the categories) and `photos: ["trail-800.jpg"]`.
3. **`addOrder`**: order 5005, placed on `ISODate("2026-11-16")`, referencing Jordan with `customerId: 2` (not a copy of his document), with `shipTo: { street: "48 Mill Road", city: "Riverbend", zip: "97402" }` embedded, and two line items embedded in `items`, each with `productId`, `name`, `price` and `quantity`: one Summit Helmet (`productId` 104, `price` 89) and two Beacon Rear Lights (`productId` 106, `price` 25). Its `total` is 139, worked out as 89 + 2 × 25 = 139.

Functions 4 to 6 answer one question each:

4. **`alexsOrders`**: the orders that reference Alex, `customerId: 1`, newest first by `orderedAt`. It finds exactly 2 orders, in this order: 5003 (November 2), then 5001 (September 3).
5. **`orderPage`**: order 5002 as one document, not a list, with its 3 items: Canal City Bike, Harbor Helmet and Beacon Front Light.
6. **`helmetsPage`**: the products whose `categoryIds` include `"helmets"`. It finds exactly 2: Harbor Helmet and Summit Helmet.

Every number is a number (`4`, not `"4"`), and every piece of text is in double quotes, as the comments show.

You can try each statement in mongosh first. In the exercise folder's terminal, start `mongosh`, type `use bike_shop_orders` to choose a practice database, then `load("data/load-into-mongosh.js")` to put the store in it. When a statement does what you want there, copy it into its function after `return`. Loading the file again puts the store back as it started.

## Check your work

Run the tests from the exercise folder:

```bash
npm test
```

At first every test fails. That's expected: each statement you finish turns its test green. When a test fails, its message says what it expected and what your statement did, for example *3) The order should reference Jordan with customerId: 2, his _id, and hold no copy of his document.*, followed by what your order holds.

The tests start their own throwaway MongoDB and load the store fresh before every test, so they never change your databases, and an insert in one test never shows up in another.

## Hints

- **Copy a shape that's already there.** The comment at the top of `queries.js` shows a customer, a category, a product and an order. Your documents look the same, with their own values.
- **Embedded is inside; referenced is only the `_id`.** An address in braces inside the customer is embedded. `customerId: 2` is a reference: Jordan's name and email stay in his own document. *Embedding: Keeping Related Data Together* and *Referencing: Linking Documents by _id* show both.
- **An array of ids is a list of plain values.** `categoryIds` holds the categories' `_id`s, the way *Choosing by Relationship: One-to-Few, One-to-Many, Many-to-Many* links products and categories.
- **One document, not a list.** The order page wants the order itself; *Finding Documents with find and findOne* shows the method that gives back one document.
- **Newest first is a sort.** *Sorting and Paging: sort, limit and skip* shows how to sort from the latest date down.

## When you're done

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

```text
✔ 1) addCustomer adds Maya as customer 4, with her address embedded (1177.2489ms)
✔ 2) addProduct adds the Trail Light 800 as product 107, with categoryIds lights and commuting (35.8786ms)
✔ 3) addOrder adds order 5005 for customer 2, with shipTo and 2 line items embedded (34.7615ms)
✔ 4) alexsOrders finds orders 5003 and 5001, newest first (34.1078ms)
✔ 5) orderPage gives order 5002 as one document, with its 3 items (31.1129ms)
✔ 6) helmetsPage finds the 2 products in the helmets category (32.3887ms)
ℹ tests 6
ℹ suites 0
ℹ pass 6
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 1699.7605
```

The times in parentheses will be different on your computer.

Now open `CHOICES.md` and finish its four sentences: why a customer's address is embedded, why a product keeps an array of category ids, why an order references its customer, and why an order embeds its items and its shipping address. Say each reason in your own words, as you'd explain it to someone joining the shop's team. The tests don't read this file, but whoever looks at your repo will. If Riverbend started showing each customer's name on the order page, would you change the model, and what would it cost?

## 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-model-the-bike-shops-orders`), 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-model-the-bike-shops-orders.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-model-the-bike-shops-orders`, 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

You've shaped a store's data the way the app reads it: embedded where data belongs together, referenced where it's shared or keeps growing, and you can say why for each one. That's the decision every MongoDB app is built on.

## Key terms

- [Embedded Document](https://www.ask-codey.com/glossary/embedded-document)
- [Document Reference](https://www.ask-codey.com/glossary/document-reference)
- [insertOne and insertMany](https://www.ask-codey.com/glossary/insertone-and-insertmany)
- [findOne](https://www.ask-codey.com/glossary/findone)

## Related

- [Model for the Questions Your App Asks](https://www.ask-codey.com/lessons/mongodb-model-for-your-queries)
- [Embedding: Keeping Related Data Together](https://www.ask-codey.com/lessons/mongodb-embedding-documents)
- [Referencing: Linking Documents by \_id](https://www.ask-codey.com/lessons/mongodb-referencing-documents)
- [Choosing by Relationship: One-to-Few, One-to-Many, Many-to-Many](https://www.ask-codey.com/lessons/mongodb-choosing-by-relationship)

Source: https://www.ask-codey.com/lessons/exercise-mongodb-model-the-bike-shops-orders
