简体   繁体   中英

how to delete document from collection in mongodb?

I have id printing in controller from client now i want to delete this document from mongodb with below code is not showing any error but not even deleting document from collection , How can i delete document using _id ?

controller.js

var Diagram = {
    remove: function(id, res) {
        console.log('deletecontroller', id);
        diagram.remove({
            _id: id
        });
    }
}
module.exports = Diagram;

I am not sure whether diagram is your model here, try with your Model, because i don't see you are getting a document via find or findOne method on which you can apply remove method.

Model.remove({ _id: id}, function(err){});

Or you can also find and remove:

Model.findOne({_id: id}, function (error, daigram){
   daigram.remove();
});

You can also use the latest version:

MyModel.findOneAndRemove({_id: id}, function(err){...});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM