NodeJS, Express, Mongoose and MongoDB RESTful Web Service – PATCH Request




Hey guys, in our previous post we discussed about retrieving the documents from mongodb using GET request. so in this post we will discuss that updating the document using PATCH request. let’s begin…

To update the document, mongoose provides methods like update(), updateOne() and findByIdAndUpdate(). In this post we will discuss about the findByIdAndUpdate(). So as the name suggests, we will pass an document id which we need to update and the updated object. findByIdAndUpdate() returns Query object.

Let’s look at the example..

If you look at the 3rd parameter, which is options. we are going to set new to true, which means By default, findByIdAndUpdate() returns the document as it was before update was applied. If you set new: true, findOneAndUpdate() will instead give you the object after update was applied

Let’s update the first object

findByIdAndUpdate()


Parameters:

  • id, document object which we need to update
  • document, updated object/document
  • options optional
  • callback function

returns Query object

That’s it for this post, in the next post we will discuss about other methods like update() and updateOne()


About the author

Bushan Sirgur

Well, I am Bushan Sirgur from Banglore, India. Currently, I am working as a Software Developer in a Service Base Company. I am interested in JAVA/J2EE, Angular 2, JavaScript, jQuery, MongoDB.

View all posts

Leave a Reply

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