Restful API with Express – Part II

This is the second part of Restful CRUD API with Express, MongoDB and Mongoose. I am going to let you know add a record, edit a record and delete a record using nodejs and mongodb.

I’ll create a restful API with express and nodejs.

I have already shared the tutorial Restful CRUD API with Express, MongoDB and Mongoose.

This node js tutorial will cover the following features:

  • How to create a record using Nodejs.
  • How to delete a record in Mongodb using Nodejs.
  • How to update a record in Mongodb using Expressjs.

CRUD API Using Nodejs, Express and NodeJS

Let’s create methods that help to add a record, edit a record and delete a record.

Create A Record into MongoDB

We will develop a new Rest API to create a new employee data entry into a Mongodb table. Because we’ll be sending JSON data to the node server, I’ll make a POST type Rest request.

Employee.create mongo method is use to create a new document into the employee collection.

Update Record into Mongodb Using Node JS

We’ll use nodejs and express to develop a new PUT type Restful api request to update data in the MongoDB database. We must provide the employee id, which will be used to update the record in the table.

We created json data and sent it to Mongoose ORM using Employee.findByIdAndUpdate(). This function requires two parameters: the employee id to update and the updated data in json format.

Delete Record from Mongodb collection

To remove employee records from the Mongodb collection, we’ll make a new DELETE Type rest API request with node js. Employee id will be sent as a parameter, which you want to remove from the Mongodb collection.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.