Mongoose Example
Users REST API with MongoDB
In this example, we will modify the Users REST API example to use the MongoDB database. Same queries will be send form the Postman to server side and same endpoints will be used in server side. We only modify codes inside the endpoints to communicate with MongoDB.
We will separate database connection and database model to different files under db
and models
directories.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Project
Take a copy of the previous version of Users REST API and make below modification. This way you will have both of the versions.
Mongoose
MongoDB can be accessed directly using Node.js's Mongo driver library, but it's a bit hard. The Mongoose module is available for this purpose. Through Mongoose, JavaScript objects can be saved directly as Mongo documents.
We are going to use the Mongoose ODM to access our users data. Mongoose will act as a front end to MongoDB. This ODM (Object Data Model) and database combination is extremely popular in the Node community. Document storage and queries looks very much like JSON and it is familiar to JavaScript developers.
Install Mongoose module to your project:
1 |
|
Dotenv
Dotenv is a zero-dependency module that loads environment variables from a .env
file into process.env
. You can use this module to store your host, port and passwords to environment-specific variables.
Install dotenv module to your project:
1 |
|
Create a .env
file to root of your project and remember add used port and MongoDB connection string into it.
.env | |
---|---|
1 2 |
|
Remember use .gitignore
and add .env
and node_modules
into it, so those will not be published to Git clouds.
.gitignore | |
---|---|
1 2 |
|
Connection to database
Create a new db
folder and a new mongodb.js
file inside it. As for the mongodb.js
, the database connection is separated and later imported in to index.js
. The parameters are used for supressing deprecation warnings when initiating the connection.
db/mongodb.js | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
And require it in index.js
.
index.js | |
---|---|
1 |
|
We will made a connection to MongoDB a little bit differently compared to earier given course materials. We will first check if the connection to MongoDB can be established correctly. If this happens we will start the actual server. Here we can use async/await
keywords which are working nicely in this situation.
index.js | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Save your files and test that connection to MongoDB is working.
Users model
Create a new models
folder and User.js
file into it. We will use this model in usersController.js
.
User.js | |
---|---|
1 2 3 4 5 6 7 8 9 |
|
Modify controllers to use MongoDB
Remove/comment previously used JSON based users data and require User
model.
usersController.js | |
---|---|
1 2 3 4 5 6 |
|
createUser
Modify createUser
function to communicate with MongoDB. We are getting data from the client in the body of the POST request and pass it on to the models create()
function, which is just a thin wrapper for the save()
function what we have used previously in the MongoDB examples.
createUser | |
---|---|
1 2 3 4 5 6 7 8 9 |
|
Use Postman and create a one user document.
getUsers
Modify getUsers
function to communicate with MongoDB. We are returning the status code 200 OK and the data or 500 internal server error in case of an error.
getUsers | |
---|---|
1 2 3 4 5 6 7 8 |
|
Use Postman to get all the user documents.
getUser
Modify getUser
function to communicate with MongoDB. User's id
is sent from client to server with request parameters.
getUser | |
---|---|
1 2 3 4 5 6 |
|
Use Postman to get one user document.
updateUser
Modify updateUser
function to communicate with MongoDB.
updateuser | |
---|---|
1 2 3 4 5 6 7 |
|
Use Postman and update on user document.
Read More
Goals of this topic
Understand
- Use Mongoose with Node/Express application.