hussain
hussain

Reputation: 7123

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;

Upvotes: 1

Views: 1697

Answers (1)

Thalaivar
Thalaivar

Reputation: 23642

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){...});

Upvotes: 2

Related Questions