Exercise: Recipe Finder

Write six mongosh queries for Sunday Kitchen's recipe pages, from quick dinners to the recipes nobody has rated yet, and let the tests run each one on the 40 recipes and check what it finds.

Apprentice · MongoDB · Updated

By

Sunday Kitchen's 40 recipes are in a database now, and its website wants pages built on them: quick dinners, a theme week, quick vegetarian recipes, a "quick or loved" list, a reminder of the recipes nobody has rated yet, and a count for the desserts badge. Each one is a query, and you'll write all six in mongosh syntax. Tests run your queries on the same 40 recipes as this chapter's lessons and check exactly what each one finds.

Get the exercise code

Open this lesson's GitHub repository, github.com/ask-codey/exercise-mongodb-recipe-finder, 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:

git clone https://github.com/ask-codey/exercise-mongodb-recipe-finder.git

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

npm install

The folder holds:

  • queries.js: the six functions you'll finish.
  • data/recipes.json: Sunday Kitchen's 40 recipes, the same ones you loaded in Adding Many Documents with insertMany.
  • data/load-into-mongosh.js: loads those recipes into mongosh, so you can try a query there first.
  • test/: the tests, and the helper that runs your queries 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 db.recipes.find({}) for now, which finds all 40 recipes. The comment above each function says exactly what it should find, with how many recipes and examples:

/*
 * 1. Find the dinners that take 30 minutes or less.
 *
 *    Finds 7 recipes: Pad Thai (30 minutes), Black Bean Tacos (20), Spaghetti Carbonara (25), Veggie Stir-Fry (20),
 *    Chicken Fried Rice (25), Teriyaki Salmon (25) and Beef Tacos (30).
 */
export function quickDinners(db) {
  return db.recipes.find({});
}

Replace db.recipes.find({}) in each function with one mongosh statement of your own, after the word return:

  1. quickDinners: the 7 dinners (meal: "dinner") that take 30 minutes or less, as above.
  2. themeWeek: the 8 recipes whose cuisine is "Greek", "French" or "Middle Eastern".
  3. quickVegetarian: the 8 vegetarian recipes (vegetarian: true) that take 20 minutes or less.
  4. quickOrLoved: the 10 recipes that take 15 minutes or less, or are rated 4.8 or higher. Either one is enough.
  5. notRatedYet: the 7 recipes nobody has rated yet: the 5 with no rating field and the 2 with rating: null.
  6. countDesserts: the number of desserts (meal: "dessert"), which is 6. It gives back a number, not the recipes.

You can try each query in mongosh first. In the exercise folder's terminal, start mongosh, type use recipe_finder to choose a practice database, then load("data/load-into-mongosh.js") to put the 40 recipes in it. When a query finds the right recipes there, copy it into its function after return.

Check your work

Run the tests from the exercise folder:

npm test

At first every test fails. That's expected: each query you finish turns its test green. When a test fails, its message says what it expected and what your query found, for example 1) quickDinners should find the 7 dinners that take 30 minutes or less. Pad Thai and Beef Tacos take exactly 30, so they count., followed by the recipes your query found.

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. If you didn't, the first npm test downloads MongoDB for the tests, nearly 1 GB on Windows and about 180 MB on a Mac, which can take a few minutes; it's kept for every exercise after this one.

Hints

  • Say the question in words first. "30 minutes or less" is "less than or equal to", "one of these three" is a list, "either one" is two conditions where one is enough. Comparison Operators: $gt, $lt, $in and More and Combining Conditions: $and, $or and $not have the operator for each.
  • Watch the edges. The tests check the recipes that sit exactly on a line: dinners that take exactly 30 minutes, and recipes rated exactly 4.8.
  • Two fields in one filter already mean "and". Vegetarian and quick doesn't need an operator to join them.
  • "Not rated yet" is two kinds of recipe. Missing Fields, null and $exists shows the one filter that finds both.
  • A count is a different method. find lists the recipes; Finding Documents with find and findOne shows the method that counts them.

When you're done

Every test is green, and npm test prints:

✔ 1) quickDinners finds the 7 dinners that take 30 minutes or less (1105.2893ms)
✔ 2) themeWeek finds the 8 Greek, French and Middle Eastern recipes (12.6087ms)
✔ 3) quickVegetarian finds the 8 vegetarian recipes that take 20 minutes or less (11.8975ms)
✔ 4) quickOrLoved finds the 10 recipes that take 15 minutes or less, or are rated 4.8 or higher (12.5288ms)
✔ 5) notRatedYet finds the 7 recipes with no rating field or rating: null (12.0778ms)
✔ 6) countDesserts gives 6, the number of desserts (12.5415ms)
ℹ tests 6
ℹ suites 0
ℹ pass 6
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 1572.2663

The times in parentheses will be different on your computer. Now read each of your six filters aloud. Does each one say the question in its comment, word for word? If Sunday Kitchen changed "quick" to mean 25 minutes, which queries would you change, and where?

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-finder), 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:

    git remote set-url origin https://github.com/your-name/exercise-mongodb-recipe-finder.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-finder, 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 search page, a "quick dinners" shelf, a reminder list: each is a filter you can now write, read aloud, and check against the data. That's most of what an app asks a database day to day.

Key terms

Related