Mongo DB “update()” not working – Alternative Code

Update() in mongo db is deprecated and it was used for updating a record. Now this function can be done using replaceOne() . Here is the sample express js code for replacing and updating a mongo db record.

app.route('/articles/:articleTitle').put(function(req, res){

  Article.replaceOne(
    {title: req.params.articleTitle}, 
    {title: req.body.title, content: req.body.content},
    {overwrite: true},
    function(err){
      if(!err){
        res.send("Successfully updated article.");
      }else{
        res.send("failed");
      }
    }
  );
})

About the Author: smartcoder

You might like

Leave a Reply

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