Exercise: Your First Recipes
Add two of Sunday Kitchen's recipe cards with insertOne, find every recipe and one by its name with find, all as mongosh statements, then let the tests check each one and hand your work in with Git.
Novice · MongoDB · Git · Updated
Sunday Kitchen's first three recipes are in the database, and Maya has two more cards ready: Chickpea Curry and Guacamole. Your job is to add them and read the recipes back, the way you did in Your First Documents in mongosh, but this time as statements that tests run and check. This is the course's first exercise, so this lesson walks through every step, from getting the code to handing it in.
Get the exercise code
Open this lesson's GitHub repository, github.com/ask-codey/exercise-mongodb-your-first-recipes, also linked from the Exercise box beside the lesson. Clone it into your mongodb-course folder. In VS Code's terminal:
cd ~/mongodb-course
git clone https://github.com/ask-codey/exercise-mongodb-your-first-recipes.git
Git makes a folder, exercise-mongodb-your-first-recipes, with the exercise code inside. If you can't clone it, click Code on the repository's page, then Download ZIP, and unzip it into your mongodb-course folder instead. Get the Exercise Code with Git walks through cloning step by step, if you'd like a reminder.
Open that folder in VS Code (File > Open Folder), open its terminal with Terminal > New Terminal, and install the packages the tests use:
npm install
The folder holds:
queries.js: the four functions you'll finish.data/recipes.json: Sunday Kitchen's 3 recipes, the ones you added in Your First Documents in mongosh. Each test starts with these.data/load-into-mongosh.js: loads those recipes into the shell, 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 the shell, and returns db.recipes.find({ name: "not written yet" }) for now: a find that finds nothing and adds nothing. The comment above each function says exactly what it should add or find:
/*
* 1. Add the recipe card for Chickpea Curry: a dinner that takes 35 minutes.
*
* Add one document with exactly these three fields: name "Chickpea Curry", meal "dinner", minutes 35.
* Afterward the collection holds 4 recipes.
*/
export function addChickpeaCurry(db) {
return db.recipes.find({ name: "not written yet" });
}
Replace db.recipes.find({ name: "not written yet" }) in each function with one mongosh statement of your own, after the word return:
addChickpeaCurry: add one recipe with exactly three fields,name"Chickpea Curry",meal"dinner"andminutes35, as above. Afterward the collection holds 4 recipes.addGuacamole: add one recipe with exactly three fields,name"Guacamole",meal"snack"andminutes10. Afterward the collection holds 4 recipes.findAllRecipes: find every recipe in the collection: the 3 it starts with, Saturday Pancakes, Weeknight Chili and Lemon Bars.findWeeknightChili: find the 1 recipe named Weeknight Chili.
Type text in double quotes and the minutes as a number, without quotes, and don't type an _id: MongoDB makes one. You don't need to know any JavaScript: the word export and the import line at the top of the file are for the tests, so leave them as they are.
You can try each statement in the shell first. In the exercise folder's terminal, start mongosh, type use first_recipes to choose a practice database, then load("data/load-into-mongosh.js"). It prints recipes: 3 documents, then true. When a statement does what you want there, copy it into its function after return. Loading the file again puts the 3 recipes back as they were.
Check your work
Run the tests from the exercise folder:
npm test
At first all 4 tests fail. That's expected: each statement you finish turns its test green. Every test starts with the same 3 recipes, so one task never changes another. When a test fails, its message says what it expected and what it found, for example 1) addChickpeaCurry should add a recipe named "Chickpea Curry", with that exact spelling. The collection holds 3: Saturday Pancakes, Weeknight Chili, Lemon Bars.
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 (with Atlas, say), 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 on a slow connection. It's kept for every exercise after this one.
Hints
- Copy the shape you already typed. Your First Documents in mongosh added three recipes with exactly these fields; only the values change.
- Capital letters count.
"Chickpea Curry"isn't"chickpea curry", andinsertOneisn'tinsertone. - A number has no quotes.
minutes: "35"stores the text 35, and the test tells you so. - Finding all and finding one use the same method. The difference is what goes inside its parentheses.
When you're done
Every test is green, and npm test prints:
✔ 1) addChickpeaCurry adds Chickpea Curry, a dinner of 35 minutes, so the collection holds 4 recipes (1079.3821ms)
✔ 2) addGuacamole adds Guacamole, a snack of 10 minutes, so the collection holds 4 recipes (10.0257ms)
✔ 3) findAllRecipes finds all 3 recipes: Saturday Pancakes, Weeknight Chili and Lemon Bars (8.7736ms)
✔ 4) findWeeknightChili finds the 1 recipe named Weeknight Chili (9.0536ms)
ℹ tests 4
ℹ suites 0
ℹ pass 4
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 1425.4341
The times in parentheses will be different on your computer. Now look at your two find statements side by side. What would you change to find the recipe named Guacamole 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. Hand In Your Work with Git walks through each step.
On GitHub, click +, then New repository. Name it (for example
exercise-mongodb-your-first-recipes), choose Public, and click Create repository. Leave out the README: your folder already has one.In VS Code's terminal, in your exercise folder, send your work to your new repo. If you cloned the starter, point your copy at your own repo, then commit and push:
git remote set-url origin https://github.com/your-name/exercise-mongodb-your-first-recipes.git git add . git commit -m "My solution" git push -u origin mainIf you downloaded the ZIP instead:
git init -b main git add . git commit -m "My solution" git remote add origin https://github.com/your-name/exercise-mongodb-your-first-recipes.git git push -u origin mainPaste your repo's address, such as
https://github.com/your-name/exercise-mongodb-your-first-recipes, 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 cloned a project, wrote statements that add and find documents, let tests tell you when they were right, and handed the work in with Git. Every exercise in this course follows that same loop.