简体   繁体   English

如何从mongodb中的集合中删除文档?

[英]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 ? 我现在从客户端的控制器中进行id打印,我想从mongodb删除此文档,以下代码未显示任何错误,但甚至没有从集合中删除文档,如何使用_id删除文档?

controller.js 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. 我不确定diagram是否是您的模型,请尝试使用您的模型,因为我看不到您是通过find or findOne方法获取文档的, find or findOne在其上应用remove方法。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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