# Exercise: Recipe Cards as Documents

Turn three of Sunday Kitchen's handwritten recipe cards into documents with the right types, arrays and nested documents, insert each with insertOne, and let the tests read them back and check every field and its type.

Apprentice · MongoDB · Updated November 19, 2026

By Gregor Dzierzon

Three of Sunday Kitchen's paper cards are still waiting to be typed in: Weeknight Chili, Lemon Bars and Chickpea Curry. Each one becomes a document like the Saturday Pancakes card you built in this chapter, with its tags as a list, its ingredients as a list of documents, its nutrition box as a document inside it, the cost of a serving as exact money and the day it was added as a real date. Tests read each card back and check every field, and its type: `"45"` in quotes where 45 belongs fails, with a message that says why.

## Get the exercise code

Open this lesson's GitHub repository, [github.com/ask-codey/exercise-mongodb-recipe-cards-as-documents](https://github.com/ask-codey/exercise-mongodb-recipe-cards-as-documents), 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-recipe-cards-as-documents.git
```

Git makes a folder, `exercise-mongodb-recipe-cards-as-documents`, 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 three functions you'll finish, one per card.
- `README.md`: the three handwritten cards, each with its text written out under its picture.
- `data/cards.json`: card 1, Saturday Pancakes, typed in the way every card should be. Copy its shape.
- `data/load-into-mongosh.js`: loads card 1 into mongosh, so you can try an insert there first.
- `test/`: the tests, and the helper that runs your statements for them. You don't need to change either.

## What to do

Each function in `queries.js` starts an `insertOne` with the card's `_id` and name. The comment above it lists every other field, with its exact name and value:

```javascript
/*
 * 1. Card 2, Weeknight Chili.
 *
 *    _id 2 and name "Weeknight Chili" are there already. Add:
 *    cuisine "American", meal "dinner", minutes 45, servings 6, vegetarian false, rating 4.4,
 *    tags, in this order: "spicy", "make-ahead",
 *    ingredients, in this order, each a document with a name, an amount and a unit:
 *      ground beef, 1, pound / kidney beans, 2, can / crushed tomatoes, 1, can / chili powder, 2, tablespoon
 *    nutrition, a document: calories 420, protein 28,
 *    costPerServing, money: 2.10, written NumberDecimal("2.10"),
 *    added, a date: January 12, 2026, written ISODate("2026-01-12").
 */
export function addWeeknightChili(db) {
  return db.cards.insertOne({ _id: 2, name: "Weeknight Chili" });
}
```

Finish the `insertOne` in each function, inside its braces, with one field for every line of the comment:

1. **`addWeeknightChili`**: card 2, as above.
2. **`addLemonBars`**: card 3, Lemon Bars: an American dessert, 50 minutes, 16 servings, vegetarian, rated 4.8, four ingredients, $0.45 a serving, added January 17, 2026.
3. **`addChickpeaCurry`**: card 4, Chickpea Curry: an Indian dinner, 35 minutes, 4 servings, vegetarian, rated 4.7, four ingredients, $1.75 a serving, added January 20, 2026.

Every value takes the type *BSON Types and the _id Field* gives it: text in quotes, counts and ratings as numbers, `true` or `false`, the tags in square brackets, each ingredient as `{ name: ..., amount: ..., unit: ... }` inside the ingredients list, the nutrition box in braces, money with `NumberDecimal("...")` and the date with `ISODate("...")`. Add no other fields, and spell each name as card 1 does.

You can try each insert in mongosh first. In the exercise folder's terminal, start `mongosh`, type `use recipe_cards` to choose a practice database, then `load("data/load-into-mongosh.js")` to put card 1 in it. When your card goes in and `db.cards.find()` shows it with the right values, copy the `insertOne` 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: there are two tests per card, one for its text, numbers, true or false, money and date, and one for its tags, ingredients and nutrition. When a test fails, its message names the card, the field, and what's wrong with it, for example *1) Weeknight Chili: minutes should be a number, 45, but it's text: "45". Take the quotes off a number.* Fix that field, run the tests again, and the next message points at the next field.

The tests start their own throwaway MongoDB, so they never change your databases. They use the MongoDB you installed in *Setup: MongoDB, mongosh and Compass*, or, without one, the copy the first exercise downloaded.

## Hints

- **Copy the shape, change the values.** Card 1 in `data/cards.json` has every field these cards need. In a JSON file, `{"$numberDecimal": "0.85"}` is how money is written, and in mongosh it's `NumberDecimal("0.85")`.
- **Quotes decide the type.** `45` is a number and `"45"` is text, which a search for quick dinners would skip. *Documents: Fields, Values, Quotes and Braces* shows the mistakes mongosh can't catch.
- **A list of documents is brackets around braces.** *Arrays and Documents Inside Documents* builds the pancake ingredients one document at a time, with a comma after every one but the last.
- **Lay it out like the lessons.** One field per line, indented two spaces a level, makes a missing comma or `]` quick to find.

## When you're done

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

```text
✔ 1) addWeeknightChili stores card 2, Weeknight Chili, with its text, numbers, true or false, money and date as the right types (1081.6006ms)
✔ 2) addWeeknightChili stores card 2's tags as a list, its ingredients as a list of documents and its nutrition as a document (9.2726ms)
✔ 3) addLemonBars stores card 3, Lemon Bars, with its text, numbers, true or false, money and date as the right types (8.6095ms)
✔ 4) addLemonBars stores card 3's tags as a list, its ingredients as a list of documents and its nutrition as a document (9.1598ms)
✔ 5) addChickpeaCurry stores card 4, Chickpea Curry, with its text, numbers, true or false, money and date as the right types (8.2932ms)
✔ 6) addChickpeaCurry stores card 4's tags as a list, its ingredients as a list of documents and its nutrition as a document (8.29ms)
ℹ tests 6
ℹ suites 0
ℹ pass 6
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 1457.7529
```

The times in parentheses will be different on your computer. Now read one of your cards aloud, field by field. If Sunday Kitchen added a fifth card tomorrow with a sale price and a "best before" date, which types would you reach for, and why not text?

## 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-recipe-cards-as-documents`), 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-recipe-cards-as-documents.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-recipe-cards-as-documents`, 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

Typing a card in looks like the dull part of every app, and it's where quiet mistakes start. You've now stored real records whose every value has the right type, which is what makes every search, sort and total in Module 2 come out right.

## Key terms

- [Document in MongoDB](https://www.ask-codey.com/glossary/document-in-mongodb)
- [Array Field](https://www.ask-codey.com/glossary/array-field)
- [Embedded Document](https://www.ask-codey.com/glossary/embedded-document)
- [Decimal128](https://www.ask-codey.com/glossary/decimal128)

## Related

- [Documents: Fields, Values, Quotes and Braces](https://www.ask-codey.com/lessons/mongodb-fields-values-quotes-and-braces)
- [Arrays and Documents Inside Documents](https://www.ask-codey.com/lessons/mongodb-arrays-and-nested-documents)
- [BSON Types and the \_id Field](https://www.ask-codey.com/lessons/mongodb-bson-types-and-the-id-field)

Source: https://www.ask-codey.com/lessons/exercise-mongodb-recipe-cards-as-documents
