- Lab
- Core Tech

Guided: Build a MongoDB-Powered Application with Node.js
In this lab, you will learn about MongoDB (a NoSQL database), Mongoose schemas and models, and implement a RESTful API to create, update, read, search for, and delete recipes. You will be given a recipe application to watch your changes come to life in a real codebase. By the end of this lab, you will have a deeper understanding of how MongoDB and Mongoose can be used in a Node.js application.

Path Info
Table of Contents
-
Challenge
Introduction
Welcome to the Guided: Build a MongoDB-Powered Application with Node.js lab.
Throughout this lab, you will go over the differences between a NoSQL database and a SQL database. You will then connect a Node.js server to a local Mongo database (a NoSQL database option). Once connected, you will learn about data modeling in MongoDB. This will include building schemas to represent a
Recipe
and anIngredient
. Throughout, you will become familiar with MongoDB terminology likecollection
anddocument
. Lastly, you will become familiar with how to perform create, read, update, and delete (CRUD) operations against your Mongo database using Mongoose. You will complete a RESTful API utilizing Node.js and Mongoose. This API will support an existing application for storing recipes. You will verify that the API works as intended by starting the application’s client and server and checking to see if data changes are persisted.A basic understanding of Node.js and Express will be useful to complete this lab, but it is not necessary. Any specifics around these subjects that you would need to understand the inner workings of to complete this lab will be provided. This lab will cover what a NoSQL database is as well as MongoDB and Mongoose technologies.
Starting the Recipe Application
To start, you have been given a recipe application created in a MERN (MongoDB, Express, React, Node.js) stack. You can create, read, update, and delete recipes using this application.
Start the application by starting the client in one terminal and starting the server in another terminal. In one terminal, start the client by following the directions below:
- Change directories to the
client
directory.- Run
cd client
.
- Run
- Run
npm run start
to start the client.
In another terminal, start the server by following the directions below:
- Change directories to the
server
directory.- Run
cd server
.
- Run
- Run
npm run dev
to start the server.
Once the client and server are running, the application can be seen in the Web Browser tab if you visit
http://localhost:3000
. Take some time to start and visit the application. When the application has been started, you will see the photo below. Currently, no recipes can be seen in the recipe table when the application starts. This is because the application is not connected to the local Mongo database yet and the recipe API is not implemented. As the lab progresses, you will see the application become much more functional.
Helpful Tips While Completing This Lab
There is a solution directory that you can refer to if you are stuck or want to check your implementations at any time. Keep in mind that the solution provided in this directory is not the only solution, so it is acceptable for you to have a different solution so long as the application functions as intended.
All dependencies you will need to complete this lab have already been installed for you using Node Package Manager,
npm
. If you are curious about what these dependencies are, feel free to look at thepackage.json
file in theserver
directory. You will learn about these dependencies throughout the lab as well.Just a reminder, you can stop running any foreground process in the Terminal with the command,
Ctrl C
. - Change directories to the
-
Challenge
Introduction to MongoDB and Mongoose
Introduction to MongoDB and Mongoose
MongoDB has already been installed for you and is running locally. For instructions on how this can be done on your own machine, you can visit MongoDB’s self-managed deployments installation instructions. Another popular option is to use MongoDB’s cloud based service MongDB Atlas to host a MongoDB database. This will not be covered in this lab.
Mongoose is a Node.js package that is already installed as a server dependency. To install Mongoose in another project, run
npm install mongoose
in a terminal. The details of why you use Mongoose alongside MongoDB will be included later in this step. For now, you will take some time to learn more about MongoDB.MongoDB is a NoSQL database. If you are unfamiliar with what a NoSQL database is, the next section will review the qualities of both SQL databases (also known as relational databases), and NoSQL databases (also known as non-relational databases),. This will allow you to create a meaningful definition of and understanding of the differences between the two database options.
SQL Databases (Relational Databases)
-
Structure: SQL databases are structured and use a fixed schema with tables, rows, and columns. Columns in a table represent an attribute and rows represent a single record in the database. Each table typically represents a specific entity, and relationships between tables are defined using foreign keys. A Schema is also necessary to add any data to a table and can be cumbersome to change later on.
-
Query Language: They use Structured Query Language (SQL) for querying and managing data. SQL allows for complex queries and joins between tables.
-
Transactions: SQL databases support ACID (Atomicity, Consistency, Isolation, Durability) properties, ensuring reliable transactions.
-
Scalability: Generally, they are vertically scalable, meaning you increase capacity by adding more power to a single server.
-
Use Cases: Ideal for applications requiring complex queries, strong consistency, and structured data, such as banking systems, customer relationship management (CRM), and enterprise resource planning (ERP).
NoSQL Databases
-
Structure: NoSQL databases are more flexible and can handle unstructured or semi-structured data. They include various data models, such as key-value stores, document stores, column-family stores, and graph databases. A Schema is not required as no structure is required by a NoSQL database. However, libraries do exist to create a Schema for NoSQL database
collections
like the one you will use in this lab, Mongoose. -
Query Language: They do not have a standard query language; instead, each type of NoSQL database may have its own API or query method. Unlike the name sounds, NoSQL does not mean you can never use SQL. The term actually stands for, “Not Only SQL”.
-
Transactions: Many NoSQL databases prioritize availability and partition tolerance (as per the CAP theorem) and may offer eventual consistency instead of strict ACID compliance.
-
Scalability: NoSQL databases are typically horizontally scalable, allowing for the addition of more servers to handle increased loads.
-
Use Cases: Suitable for big data applications, real-time web apps, content management systems, and scenarios where the schema may evolve over time, such as social media platforms and IoT applications.
Summary of SQL databases and NoSQL databases
In summary, SQL databases are great for structured data and complex relationships, while NoSQL databases excel in flexibility and scalability for diverse and rapidly changing data types. The choice between them largely depends on the specific needs of your application.
MongoDB Architecture
In this lab, you are learning about the popular NoSQL database, MongoDB, for data storage. MongoDB’s structure includes
collections
that storedocuments
unlike a SQL database that would be a table with rows and columns. You can loosely think of acollection
like a table and acollection
like a row or record within that table. This means that acollection
would represent an entity in an application and adocument
is an instance of the entity containing various relevant fields to represent thedocument
.A MongoDB
document
looks a lot like a JSON object (though it is technically a Binary JSON or BSON object). It is a compilation of key-value pairs where each pair is a field in thedocument
. These fields can also contain nesteddocuments
. There is no enforced structure in any MongoDBdocument
and you can perform any operation you would on a JSON object on a MongoDBdocument
.The ability to nest
documents
inside of anotherdocument
can make it easy to associate relateddocuments
with onecollection
. This means you would only have to maintain onecollection
unlike what would be necessary for a SQL database. The trade off is queries to fetch the nesteddocuments
can get complex and difficult to maintain.An example of a MongoDB
document
is below that represents a blogger’s profile and blog posts.{ "_id": ObjectId("60d5ec49b1234567890abcdef"), "username": "john_doe", "email": "[email protected]", "profile_picture": "https://example.com/images/john_doe.jpg", "bio": "Tech enthusiast and avid traveler.", "created_at": ISODate("2023-09-18T10:00:00Z"), "blog_posts": [ { "post_id": ObjectId("60d5ec49b1234567890abcde"), "title": "Exploring the World of NoSQL", "content": "NoSQL databases are becoming increasingly popular due to their flexibility and scalability. In this post, we explore various types of NoSQL databases and their use cases.", "date": ISODate("2023-08-01T12:00:00Z"), "tags": ["NoSQL", "Database", "Tech"], "comments": [ { "comment_id": ObjectId("60d5ec49b1234567890abcdf"), "user": "jane_smith", "content": "Great insights! I love using MongoDB.", "date": ISODate("2023-08-02T08:30:00Z") }, { "comment_id": ObjectId("60d5ec49b1234567890abce0"), "user": "mark_taylor", "content": "Very informative! Thanks for sharing.", "date": ISODate("2023-08-02T09:00:00Z") } ] }, { "post_id": ObjectId("60d5ec49b1234567890abcdf"), "title": "A Day in Tokyo", "content": "Tokyo is a vibrant city with so much to offer. From food to culture, here’s my experience visiting the city.", "date": ISODate("2023-08-15T14:00:00Z"), "tags": ["Travel", "Tokyo", "Adventure"], "comments": [] } ] }
Introduction to Mongoose
To talk to MongoDB from the Node.js application in this lab, Mongoose is a library that is a supported Node.js package. Mongoose will be used in the upcoming activity to connect to a MongoDB database from Node. In future steps, Mongoose will be used in future steps to create schemas, models, and build queries. Keep reading to learn more about Mongoose before jumping into the activity.
Mongoose is an Object Data Modeling (ODM) library for MongoDB and Node.js. It provides a straightforward way to model your data and interact with MongoDB databases, offering several useful features:
- Schema Definition: Mongoose allows you to define schemas for your data, which serve as a blueprint for your documents. This helps enforce structure and validation.
const mongoose = require('mongoose'); const userSchema = new mongoose.Schema({ username: { type: String, required: true }, email: { type: String, required: true, unique: true }, password: { type: String, required: true }, createdAt: { type: Date, default: Date.now } });
- Models: Once you've defined a schema, you can create a model based on that schema. Models are used to create and manage documents in the database.
const User = mongoose.model('User', userSchema);
-
Data Validation: Mongoose provides built-in validation to ensure that documents meet certain criteria before being saved to the database. You can specify validation rules at the schema level.
-
Middleware: Mongoose supports middleware (also known as hooks) that can be executed at specific points in the lifecycle of a document (e.g., before saving or after removing).
-
Query Building: Mongoose offers a powerful and fluent API for building queries, making it easier to interact with the database without writing raw MongoDB queries.
User.find({ username: 'john_doe' }, (err, users) => { if (err) console.error(err); console.log(users); });
-
Population: Mongoose allows you to reference documents in other collections, making it easy to work with related data. You can "populate" references to retrieve full documents.
-
Plugins: Mongoose supports plugins that can add functionality to schemas and models, allowing for reusable code.
Activity - Connect Node to MongoDB
- Import Mongoose in
server/server.js
. Add the following line under the list of imports:import mongoose from "mongoose"
- Connect to the local MongoDB host and
recipedb
database usingmongoose.connect
. This can be done by addingmongoose.connect("mongodb://127.0.0.1:27017/recipedb");
underneath the imports inserver/server.js
.- Note: If the
recipedb
database did not exist, MongoDB would automatically create it for us when connecting. In the case of this lab, therecipedb
exists and already has three recipedocuments
in it.
- Note: If the
You can test your MongoDB connection with the
mongosh
command. This will also print what connection string the database is running on.Conclusion
In this step, you learned about MongoDB and Mongoose. In the activity, you connected the recipe application to a local instance of a MongoDB database.
-
-
Challenge
Data Modeling with Mongoose
Data Modeling with Mongoose
Mongoose allows you to define schemas for your data. These schemas can enforce certain
document
structures even though MongoDB is a NoSQL database. Mongoose also allows you to create models based on schemas. These models can be used to perform CRUD operations ondocuments
in acollection
.In this step, you will define the
Recipe
andIngredient
schemas. After these schemas are defined, you will create aRecipeModel
that will be used to create, update, read, and delete recipedocuments
in future steps.You will not need to create a model for the
Ingredient
schema. This is because you will only ever store an ingredient as a nesteddocument
in a recipe, so you will not need to use a model to query ingredients. Lastly, you will export the schemas and model you create.
Activity 1 - Define and Export an Ingredient Schema
In
server/models/ingredient.js
follow the steps below to define anIngredient
Schema.- Import Mongoose at the top of the file.
- Store
mongoose.Schema
in a constant variable calledSchema
. - Create a new
Schema
object with the properties below and store this object in a constant variable titledIngredientSchema
.- quantity: Number
- metric: String
- ingredient: String
- Note:
Schema
is passed a Javascript object with key-value pairs. The object will then be the enforced structure ofdocuments
that are of the createdSchema
type.
- Export
IngredientSchema
Example recipe.js After Activity 1
// Step 3 - Activity 1.1 Import mongoose import mongoose from "mongoose"; // Step 3 - Activity 1.2 mongoose Schema variable const Schema = mongoose.Schema; // Step 3 - Activity 1.3 Define IngredientSchema const IngredientSchema = new Schema({ quantity: Number, metric: String, ingredient: String, }); // Step 3 - Activity 1.4 export IngredientSchema export { IngredientSchema };
Activity 2 - Define and Export a Recipe Schema
In
server/models/recipe.js
follow the steps below to define aRecipe
Schema.- Import Mongoose at the top of the file.
- Import
IngredientSchema
from./ingredient.js
- Store
mongoose.Schema
in a constant variable calledSchema
. - Create a new
Schema
object with the properties below and store this object in a constant variable titledRecipeSchema
.- title: String
- instructions: String
- ingredients: [IngredientSchema] - This is an array of ingredients
- prepTime: Number
- cookTime: Number
- Export
RecipeSchema
Example recipe.js After Activity 2
// Step 3 - Activity 2.1 Import mongoose import mongoose from "mongoose"; // Step 3 - Activity 2.2 Import IngredientSchema import { IngredientSchema } from "./ingredient.js"; // Step 3 - Activity 2.3 mongoose Schema variable const Schema = mongoose.Schema; // Step 3 - Activity 2.4 Define RecipeSchema const RecipeSchema = new Schema({ title: String, instructions: String, ingredients: [IngredientSchema], prepTime: Number, cookTime: Number, }); // Step 3 - Activity 2.5 Export RecipeSchema export { RecipeSchema };
Activity 3 - Define a Recipe Model
Models that can be used to perform queries are defined through Mongoose’s
Schema
interface. Since you createdRecipeSchema
in the previous activity, you can create a recipe model.- Above the
export
statement inserver/models/recipe.js
, callmongoose.model
to create a recipe model. Store the recipe model in a constant variable calledRecipeModel
.- Note: The database can be accessed through
mongoose.model
. The first argument passed tomongoose.model
is the singular name of the collection your model is for. In this case, it is”Recipe”
. Mongoose tries to find acollection
with the plural version of this parameter,“Recipes”, and if it does not find one, it will create one. The second parameter is the schema
documentsin this
collectionshould follow. You will pass
RecipeSchema` as the second parameter.
- Note: The database can be accessed through
- Export
RecipeModel
Example recipe.js After Activity 3
import mongoose from "mongoose"; import { IngredientSchema } from "./ingredient.js"; const Schema = mongoose.Schema; const RecipeSchema = new Schema({ title: String, instructions: String, ingredients: [IngredientSchema], prepTime: Number, cookTime: Number, }); // Step 3 - Activity 3.1 Create RecipeModel const RecipeModel = mongoose.model("Recipe", RecipeSchema); // Step 3 - Activity 3.2 Export RecipeModel export { RecipeSchema, RecipeModel };
Conclusion
In this step, you have created schemas and a model using mongoose. Continue on to begin querying your MongoDB database.
-
Challenge
Read Documents From the Recipe Collection with Mongoose
Read Documents From the Recipe Collection with Mongoose
In this step, you will finish implementing the
GET /recipes
andGET /recipes/:id
endpoints inserver/server.js
. This will include selecting all recipe documents, filtering the selection of recipe documents on title, and selecting a recipe document based on itsid
.
Activity 1 - Get All Recipes
- Import
RecipeModel
from./models/recipe.js
. This is necessary to use the model to perform queries against the”Recipes”
collection. Addimport { RecipeModel } from "./models/recipe.js";
next to the existing imports. - Inside the
GET /recipes
endpoint, you will notice atry
catch
block. Inside thetry
block, select all recipe documents in theRecipes
collection.- Note: To select all documents in the
Recipes
collection, you will need to pass an empty document,{}
as the query filter parameter to the first argument of theRecipeModel
’sfind
method.
- Note: To select all documents in the
- Await the results of the
find
method as all Mongoose queries are asynchronous calls. - Store the results of the
find
method in theresults
variable.
Example GET /recipes Endpoint After Activity 1
app.get("/recipes", query("title").escape(), async (req, res, next) => { const title = req.query.title; try { let results; // Step 4 - Activity 1.2 - 1.4 Get All Recipes results = await RecipeModel.find({}); res.status(200).json(results); } catch (error) { next(error); } });
You will notice that the
GET /recipes
endpoint sends back a200
status code and theresults
as JSON. This means that if you visit the recipe application while it is running and refresh the home page, you should now see three different recipes in the recipes table. This is proof that your endpoint is fetching data from the MongoDB database. You can also take note that the application would raise an error if an error would occur while fetching data because of the query being wrapped in atry
catch
block.Take some time to visit the application now, it should look like the picture below since the
Recipes
collection has already been seeded with data for you. You can see that search, create, edit, and delete functionality still needs to be implemented for the application to function as intended.
Activity 2 - Get All Recipes That Have the Title URL Query In the Recipe Title
- Inside the
try
block in theGET /recipes
endpoint, write anif
else
statement. Theif
block will be entered if the variabletitle
is defined. Thetitle
variable is created for you at the top of the end point and is a query parameter on the request’s url. - Move the
results = await RecipeModel.find({});
line of code that you wrote in the previous activity into theelse
case. - Select all recipe documents in the
Recipes
collection withtitle
in the recipe’s title.- Note: This requires using the query filter parameter that is passed to the
find
method. The query filter parameter determines the select criteria. To find all documents that containtitle
in the recipe document’s title, you need to pass the following object:{ title: new RegExp(title, “i”) }
- Note: This requires using the query filter parameter that is passed to the
- Await the results of the
find
method. - Store the results of the
find
method in theresults
variable.
Example GET /recipes Endpoint After Activity 2
app.get("/recipes", query("title").escape(), async (req, res, next) => { const title = req.query.title; try { let results; // Step 4 - Activity 2.1 Create an If Else Block Based On If Title Is Defined if (title) { // Step 4 - Activity 2.3 - 2.5 Find recipes with title in their titles results = await RecipeModel.find({ title: new RegExp(title, "i"), }); // Step 4 - Activity 2.1 Create an If Else Block Based On If Title Is Defined } else { // Step 4 - Activity 2.2 Move Get All Recipes Into the Else Case results = await RecipeModel.find({}); } res.status(200).json(results); } catch (error) { next(error); } });
Because the variable
results
is conditionally set based on if atitle
is passed to the endpoint or not, this endpoint can now respond to two different requests for data. One request wants to fetch all the recipes and the other request wants to fetch all recipes that havetitle
in their title.Refresh the application in the Web Browser and you should now be able to search the applications based on title. This may look like the picture below.
Activity 3 - Get A Recipe By Its ID
- Import
ObjectID
from”mongodb”
next to the existing imports. This will look likeimport { ObjectId } from "mongodb";
This module is necessary to convert string or number ids into a MongoDBObjectID
data type. This conversion will be necessary to actually perform a search for a recipe collection based on its id. - Inside the
try
block in theGET /recipes/:id
endpoint, store the result ofObjectId.createFromHexString(req.params.id);
in a constant variable called,id
. - Inside the
try
block in theGET /recipes/:id
endpoint, await the result ofRecipeModel.findById(id)
and save it to the constant variable called,result
. The constant variable result is current just being set to an empty object, it is up to you to change that.
Example GET /recipe/:id Endpoint After Activity 3
app.get("/recipes/:id", async (req, res) => { try { // Step 4 - Activity 3.2 const id = ObjectId.createFromHexString(req.params.id); // Step 4 - Activity 3.3 const result = await RecipeModel.findById(id); res.status(200).json(result); } catch (error) { next(error); } });
As mentioned before,
result
is sent on as JSON with a status code of200
. Refresh the recipe application, and you will be able to click on each recipe’s title in the table to see the specifics around the recipe on a new page. This will load a page like the photo below. This verifies that you can query the MongoDB database for just one recipe at a time by its ID.
Conclusion
In this step, you were able to read data from the local MongoDB database instance. You also verified that the code you wrote is working correctly by visiting the recipe application in the Web Browser.
- Import
-
Challenge
Update a Recipe Document with Mongoose
Update a Recipe Document with Mongoose
You will now finish implementing the
PUT /recipes/:id
endpoint. This will include creating a MongoDBObjectId
object from the requestsid
parameter, finding the recipe document with the corresponding id, setting the recipe’s fields to match what was sent in requestsbody
, and saving the recipe document.Activity - Update a Recipe
- Inside the
try
block in thePUT /recipes/:id
endpoint, store the result ofObjectId.createFromHexString(req.params.id);
in a constant variable called,id
. - Save the request’s body, accessed through
req.body
to a constant variable called,body
. - Find the recipe with the corresponding id, like you did in the previous step. Assign the result to a variable called,
result
. This variable is already created for you, just change the assignment from the empty object to what you get back from Mongoose'sfindById
command.- Note: The
result
variable should be created with thelet
keyword because it is not a constant variable and will change.
- Note: The
- Set
result
’stitle
tobody
’stitle
.- Hint: This assignment will look like
result.title = body.title;
. Future assignments will look similar except with different field names.
- Hint: This assignment will look like
- Set
result
’sinstructions
tobody
’sinstructions
. - Set
result
’singredients
tobody
’singredients
. - Set
result
’sprepTime
tobody
’sprepTime
. - Set
result
’scookTime
tobody
’scookTime
. - Await the result of calling
save
onresult
.- Remember:
result
is a recipe document in theRecipes
collection. By savingresult
, you are updating the corresponding document.
- Remember:
Example PUT /recipes/:id Endpoint After Activity 1
app.put("/recipes/:id", async (req, res, next) => { try { // Step 5 - Activity 1.1 Change The Request Parameter ID To a MongoDB ObjectID const id = ObjectId.createFromHexString(req.params.id); // Step 5 - Activity 1.2 Save the body of the request to a variable const body = req.body; // Step 5 - Activity 1.3 Find the Corresponding Recipe let result = await RecipeModel.findById(id); // Step 5 - Activity 1.4 Update the Recipe's Title result.title = body.title; // Step 5 - Activity 1.5 Update the Recipe's instructions result.instructions = body.instructions; // Step 5 - Activity 1.6 Update the Recipe's ingredients result.ingredients = body.ingredients; // Step 5 - Activity 1.7 Update the Recipe's prepTime result.prepTime = body.prepTime; // Step 5 - Activity 1.8 Update the Recipe's cookTime result.cookTime = body.cookTime; // Step 5 - Activity 1.9 Save the Updated Recipe await result.save(); res.status(200).json(result); } catch (error) { next(error); } });
The update endpoint responds with a status code of
200
and the JSON of the new recipe if the update operation finished with no errors. Refresh the recipe application and you will now be able to edit a recipe and persist the changes. This is proof that your endpoint is making updates to documents in your database.Conclusion
You now have performed update operations to documents in a MongoDB collection.
- Inside the
-
Challenge
Delete a Recipe Document with Mongoose
Delete a Recipe Document with Mongoose
In this step, you will finish implementing the
DELETE /recipes/:id
endpoint. Like previous steps, you will start by converting the requestid
parameter into a MongoDBObjectID
. Then, you will use the methoddeleteOne
to delete the corresponding recipe.Activity - Delete a Recipe
- Inside the
try
block in thePUT /recipes/:id
endpoint, store the result ofObjectId.createFromHexString(req.params.id);
in a constant variable called,id
. - Await the result of
RecipeModel.deleteOne({ _id: id });
to delete the corresponding recipe.- Note: You will notice the object passed to
deleteOne
has a key titled,_id
. The title and value of this key is what MongoDB adds when it creates a new document. It is similar to a primary key in a SQL database’s table.
- Note: You will notice the object passed to
Example DELETE /recipes/:id Endpoint After Activity 1
app.delete("/recipes/:id", async (req, res, next) => { try { // Step 6 - Activity 1.1 Change The Request Parameter ID To a MongoDB ObjectID const id = ObjectId.createFromHexString(req.params.id); // Step 6 - Activity 1.2 Delete a Recipe With The Corresponding ID await RecipeModel.deleteOne({ _id: id }); res.status(200).json({ message: `Recipe successfully deleted` }); } catch (error) { next(error); } });
If you look at the parts of the delete endpoint that were given to you, you will notice if the operation successfully completes the endpoint responds with a status code of
200
and a message. Refresh the recipe application, you now can remove recipes by clicking the delete button and the change is persisted all the way through to the database.Conclusion
In this step, you programmed a way to delete documents from your MongoDB database using Mongoose’s
RecipeModel
. This operation would have also been possible with thefindByIdAndDelete
command. - Inside the
-
Challenge
Create a Recipe Document with Mongoose
Create a Recipe Document with Mongoose
Now you will finish implementing the
POST /recipes
endpoint. This will be simple using the request’sbody
and thecreate
method on a Mongoose model.Activity - Create a Recipe
- Inside the
try
block in thePOST /recipes
endpoint at the top, assignreq.body
to a constant variable called,body
. - Update the assignment of the
result
variable to be the result from awaiting the result ofawait RecipeModel.create(body);
.
Example POST /recipes/ Endpoint After Activity 1
app.post("/recipes", async (req, res, next) => { try { // Step 7 - Activity 1.1 Store the Reuqest's Body in a Variable const body = req.body; // Step 7 - Activity 1.2 const result = await RecipeModel.create(body); res.status(200).json(result); } catch (error) { next(error); } });
The endpoint responds with a status code of
200
and the JSON of the result from creating the recipe if it encounters no errors. This means if you refresh the recipe application, you should now be able to create recipes and see that they are added to the recipe table. When you refresh once more, new recipes should remain in the table and that is proof that they are being persisted to the database.Conclusion
You have now created new documents in a MongoDB collection.
- Inside the
-
Challenge
Conclusion
Conclusion
Throughout this lab, you became familiar with what a NoSQL database is and how it differs from SQL databases. This lab covered the specific NoSQL database, MongoDB. You worked with Mongoose, a package built to support interactions with MongoDB. You used Mongoose to create
collection
schemas and created a recipe model to perform CRUD operations and enforce schemas. You implemented a RESTful API to perform CRUD operations within the recipecollection
. You have the opportunity to see your API work in live time by running the recipe application’s client and server and visiting the application. Within the application you could search for recipes as well as create, update, and delete recipes. This is a great example of how MongoDB and Mongoose are used in Node.js applications to create server APIs.
What's a lab?
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Provided environment for hands-on practice
We will provide the credentials and environment necessary for you to practice right within your browser.
Guided walkthrough
Follow along with the author’s guided walkthrough and build something new in your provided environment!
Did you know?
On average, you retain 75% more of your learning if you get time for practice.