We learned how to use Mongoose to create Schemas and then interact with our MongoDB database. In the following exercise, we will practice how to implement this by creating awesome recipes.
- Fork this repo
- Clone this repo
-
Upon completion, run the following commands:
git add . git commit -m "done" git push origin master
-
Create Pull Request so your TAs can check up your work.
Create a Recipe
model inside the file /models/Recipe.model.js
. The schema should have the following fields:
- title - type
String
. It should be required and unique. - level - type
String
. Only can be one of the following values: Easy Peasy - Amateur Chef - UltraPro Chef (remember the ENUM 😉) - ingredients - type
Array
. - cuisine - type
String
. Should be required. - dishType - type
String
. Possible values: Breakfast - Dish - Snack - Drink - Dessert - Other. - image - type
String
. Default value: https://images.media-allrecipes.com/images/75131.jpg. - duration - type
Number
. Min value should be 0. - creator - type
String
- created - type
Date
. By default today.
In index.js
, using the Model.create
method, you should pass the info to create a new recipe. After the creation, you can use MongoDB Compass to check everything went ok. After inserting the recipe, console.log
the title
of the recipe.
To run your code, remember you should use:
$ node index.js
We are importing an array of recipes form the data.js
file. Using the Model.insertMany
method, you should add that entire array to the database. After inserting the elements, print on the console the title of each recipe.
Now you should have six different recipes in the database, but there was a mistake in one of them. The Rigatoni alla Genovese does not take that long. You should update the duration
field and set it to 100. After updating it, print a success message!
Oh oh! The Carrot Cake
is no longer available, so we need to remove it from the database. Using the Model.deleteOne
method, remove that recipe from the database and display a success message after doing it!
After doing all the task you should close the database. Otherwise, the connection will keep open. Be careful about the asynchronicity of all process; you should close it after everything is done! 😉
Happy coding! ❤️