Unable to delete data from mongoDB

You need to pass the _id value as an ObjectID, not a string:

var mongodb = require('mongodb');
    router.delete('/contact/:id', (req, res, next) => {
      contact.deleteOne({ _id: new mongodb.ObjectID(req.params._id) }, function(err, result) {
        if (err) {
          res.json(err);
        } else {
          res.json(result);
        }
      });
    });

Leave a Comment