CRUD Operations using NodeJS, Express, MongoDB and Mongoose
This is another nodejs and express.js tutorial which help to create CRUD operation using Monodb and Mongoose ORM. Mongodb is popular opensource no-SQL database.Mongoose is ORM(Object-relational mapping) that provide helpful methods to do operation with mongodb collections.
I have already shared Add, Edit and Delete Record Using Mysql and Node js Rest Api to Add, Edit and Delete Record from MySQL Using Express JS.I am assuming you have installed mongodb into your server.
This nodejs tutorial is the extended tutorial of nodejs and express.js rest api tuts.I will use mongodb as database and mongoose ORM to do operations on mongo models. I will create HTTP rest call to fetch mongodb collections details, create record into mongodb, update record into mongodb and mongodb delete collection.
We will cover following functionality into this node js tutorial:
- How to create database connection with Mongodb.
- How to create Model using Mongoose.
- How to create collection in Mongodb using Mongoose.
- How to delete collection in Mongodb using Mongoose.
- How to update Collection in Mongodb using Mongoose.
- Creating CRUD operation REST API using Node, ExpressJS, MongoDB and Mongoose.
The Node js Rest API details are as follows:
I am using following files and folder
I am creating a folder 'nodejs-restapi-mongoose-example'
.This is our nodejs project name.
package.json: This file will have all nodejs dependencies module for this example.
config/database.js: This file will use for database connection parameters for Mongodb.
model/employee.js: This file will use to create employee schema and model.
main.js: This file will use to create nodejs application server and routes url.
node_modules folder: This folder will contains all nodejs packages.
How to install package dependencies into Nodejs app
We will create package.json
file into 'nodejs-restapi-mongoose-example'
folder and add below modules in this file.
We will open command line and cd to 'nodejs-restapi-mongoose-example'
folder and run below command to install dependencies modules into 'node_modules'
folder.
d:\nodejs-restapi-mongoose-example > npm install
Step 1: We will create database.js
connection file into config folder, The connection url of mongodb will add here like below,
Step 2: We will create employee.js
model file into model folder, We will create schema and register Employee model.
The line #2
, use to get mongoose ORM instance.
Step 3: We will create add dependency modules into main nodejs file, created main.js
file and included all required modules like below,
Also Checkout other tutorials of nodejs rest api,
Node.js Rest Api to fetch all record from Mongodb Using Mongoose
We will create a GET
Rest Request to access all employee records from MongoDB. We will use mongodb query to fetch data from employee table and send JSON data to client as response object.
The Employee.find
use to get all records from database.The res.end()
method send data to client a json string through JSON.stringify()
method.
Now access http://localhost:8888/employee
rest api url form browser and you will get a all employees record from Mongo database.
Rest Api to get single record from Mongodb Using Node.js and ExpressJS
We will create a GET type rest request to access a single employee record from mongodb. We will use find()
method of mongoose and passed desired employee id.We will send json data to client as response object.
Rest Api to Create New Record into Mongodb Using Node.js and ExpressJS
We will create a new Rest Api to create a new employee data entry into Mongodb table using node.js. I will create POST type Rest request because We will post some JSON data to node server.
Employee.create
method use to create new document into employee collection.
We will create new PUT type Restful api request using nodejs and express to update data into mongodb database.We need to pass employee id which will use to updated record into table.
We have created json data and send mongoose ORM using Employee.findByIdAndUpdate()
, this method takes first parameters is employee_id
which we want to update and second is updated data
in json format.
Delete Record from Mongodb collection
We will create new DELETE Type rest Api request using node js to remove employee record from Mongodb collection.We will pass employee id
as a parameters which you want to delete from Mongodb collection.
Conclusion
We have created simple nodejs application to do crud operation using express.js.We have also connected Mongodb and uses Mongoose ORM. Created Rest API to get all employee,get single employee record,create new employee entry,update employee data and delete employee data from MongoDB Collection through mongoose framework.
You can download source code from below link.
Originally published at https://www.js-tutorials.com on September 23, 2017.