Patch method Api in Nodejs

PATCH method is used when you want to update the resource like update the name, status etc. PATCH method is faster than PUT method. PUT method basically used when you want to modify the whole resource value.

Suppose, You have users collection which has three fields
1) _id
2) name
3) email

let users=[

Create the patch method API to update the document in the MongoDB

var MongoClient = require('mongodb').MongoClient;
var express = require('express');
var app = express();
app.patch('/updateUser/:id', function (req, res) {

    MongoClient.connect(url, function (err, db) {


    let req_params=req.params;
    let req_body=req.body;

    let user_id=(typeof !== 'undefined') ? (;
    let user_name=(typeof !== 'undefined') ? ("";

    let user_details={

    db.collection("users").UpdateOne({"_id":user_id},{$set:user_details},function(err, result) {

    if (err){
      res.json({data:result,messages:"some thing went wrong",status:501})

   res.json({data:result,messages:"user update successfully",status:200})



   res.json({data:err,messages:"some thing went wrong",status:501})



Note:- In the above example req.body is used to get the URL body parameters value and req.params is used to get the route parameters value.