# Exercise: Plant Shop Catalog

Move a plant shop's catalog into MongoDB with the official driver, connecting, saving 12 plants with insertMany, reading them back, finding the pet-safe ones and adding a new plant, and let tests on a throwaway MongoDB check every function.

Apprentice · MongoDB · Node.js · Updated November 19, 2026

By Gregor Dzierzon

Fern & Fig's catalog still lives in a JSON file, and the shop wants it in MongoDB. You'll finish the shop's catalog module with the official driver: connect, save the 12 plants, read them back, find the ones that are safe for pets, and add a new plant. Tests check every function as you go, on a throwaway MongoDB of their own.

## Get the exercise code

Open this lesson's GitHub repository, [github.com/ask-codey/exercise-node-plant-shop-catalog](https://github.com/ask-codey/exercise-node-plant-shop-catalog), also linked from the **Exercise** box beside the lesson. Clone it into your `nodejs-course` folder. In VS Code's terminal:

```bash
cd ~/nodejs-course
git clone https://github.com/ask-codey/exercise-node-plant-shop-catalog.git
```

Git makes a folder, `exercise-node-plant-shop-catalog`, with the exercise code inside. Open that folder in VS Code (**File > Open Folder**), open its terminal with **Terminal > New Terminal**, and install the packages:

```bash
npm install
```

Then copy `.env.example` to a new file named `.env`, which holds the connection string `npm start` uses: `Copy-Item .env.example .env` in PowerShell, or `cp .env.example .env` on a Mac. *Get the Exercise Code with Git* walks through cloning step by step, if you'd like a reminder.

The folder holds:

- `catalog.js`: the five functions, for you to finish.
- `plants.json`: Fern & Fig's 12 plants, each with its care details embedded.
- `show-catalog.js`: runs your functions against your own MongoDB and prints what they did.
- `catalog.test.js`: the tests. You don't need to change it.
- `throwaway-database.js`: starts the throwaway MongoDB the tests use. You don't need to read it or change it.

## What to do

Each function in `catalog.js` stops with the error *Not built yet.* for now. The comment at its top says what each one takes and gives back:

```javascript
//   openCatalog(uri)          connects with the connection string it's given, and gives back an object with two
//                             properties: client, the MongoClient, and plants, the plants collection of the
//                             plant_shop database
//   seedCatalog(plants)       saves the 12 plants from plants.json in the collection with one insertMany, and gives
//                             back how many it saved: 12
//   allPlants(plants)         gives back every plant in the collection, as an array: [] when there are none
//   petSafePlants(plants)     gives back only the plants whose petSafe is true: 6 of the 12, Spider Plant first
//   addPlant(plants, plant)   saves one new plant, and gives back the _id it got
```

1. **Open the catalog.** In `openCatalog(uri)`, make a `MongoClient` from the connection string, pick the `plant_shop` database and its `plants` collection, and give back both the client and the collection.
2. **Seed it.** In `seedCatalog(plants)`, save every plant `readPlantsFile()` gives you with one `insertMany`, and give back how many were saved.
3. **Read it back.** Finish `allPlants(plants)`, then `petSafePlants(plants)`, which finds only the plants whose `petSafe` is `true`.
4. **Add a plant.** In `addPlant(plants, plant)`, save the one plant and give back its new `_id`.

## Check your work

Run the tests from the exercise folder:

```bash
npm test
```

No MongoDB needs to be running: the tests start a throwaway MongoDB of their own, and every test begins with an empty database, so your own data is never touched. If MongoDB is installed on your computer, the tests use it. If it isn't, the first `npm test` downloads MongoDB for the tests, **about 950 MB on Windows and about 180 MB on a Mac**, once per computer, so the first run takes a while.

At first all 7 tests fail. That's expected: each function you finish turns its tests green. A failing test's message says what it expected and what came back, for example *allPlants should give back an array of plants, but it gave back find()'s cursor, not a list of plants. Did you forget toArray()?*

To see your catalog in your own MongoDB, run `npm start` with your server running, then look in Compass: the `plant_shop` database holds the `plants` collection.

## Hints

- **Three steps, one line each.** *Connecting from Node and Saving Documents* makes a client, picks a database, then a collection. `openCatalog` does exactly that, and gives the client back too, so whoever opened the catalog can close it.
- **Await, then read the result.** `insertMany` and `insertOne` give back a result object, not the plants. Its `insertedCount` and `insertedId` are the parts you need.
- **A cursor isn't a list.** `find()` gives back a cursor; `toArray()` turns it into the array the tests expect.
- **Matching a field.** `find` takes a document of fields and values to match, as *Connecting from Node and Saving Documents* shows with the pet-safe plants.
- **The shape is already chosen.** Each plant in `plants.json` keeps its care details embedded, as *Shaping a Document: Embed or Reference* decided, so `insertMany` saves them as they are.

## When you're done

Every test is green, and `npm start` prints what your functions did (your `_id` will be different):

```text
Saved 12 plants from plants.json.
Added the Peperomia, with the _id 6afc9479bfecda68fd5d5633.
The catalog has 13 plants.
Safe for pets: Spider Plant, Boston Fern, Calathea, Parlor Palm, African Violet, Ponytail Palm, Peperomia.
```

Now look back at `openCatalog`. The other four functions take the collection as a parameter instead of making their own client. What would go wrong if each of them made a new `MongoClient` every time it was called?

## 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. *Hand In Your Work with Git* walks through each step.

1. On GitHub, click **+**, then **New repository**. Name it (for example `exercise-node-plant-shop-catalog`), 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-node-plant-shop-catalog.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-node-plant-shop-catalog`, 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

A catalog module that connects, saves and reads is the bottom layer of every API you'll build with a database: the routes come next, and they only call functions like these.

## Key terms

- [MongoDB Driver](https://www.ask-codey.com/glossary/mongodb-driver)
- [MongoClient](https://www.ask-codey.com/glossary/mongoclient)
- [insertOne and insertMany](https://www.ask-codey.com/glossary/insertone-and-insertmany)
- [find](https://www.ask-codey.com/glossary/find-mongodb)
- [Document in MongoDB](https://www.ask-codey.com/glossary/document-in-mongodb)

## Related

- [Documents and Collections](https://www.ask-codey.com/lessons/node-mongodb-documents-and-collections)
- [Shaping a Document: Embed or Reference](https://www.ask-codey.com/lessons/node-mongodb-designing-documents)
- [Setup: MongoDB and Compass](https://www.ask-codey.com/lessons/node-mongodb-setup)
- [Connecting from Node and Saving Documents](https://www.ask-codey.com/lessons/node-mongodb-connect-and-insert)

Source: https://www.ask-codey.com/lessons/exercise-node-plant-shop-catalog
