简体   繁体   English

如何使用文档ID从mongoDB集合中删除文档?

[英]How to delete document from mongoDB collection using the document's ID?

I am trying to remove a document from a mongoDB collection by finding the document in the collection using an id. 我正在尝试通过使用ID在集合中查找文档来从mongoDB集合中删除文档。 Below is a snippet of my code and it says that the note was successfully deleted but when I go back to where all the notes are displayed, the note that was supposedly deleted is still there. 下面是我的代码片段,其中说该便笺已成功删除,但是当我返回显示所有便笺的位置时,应该删除的便笺仍然存在。

router.post('/delete', function(req, res) {    
    notesCollection.remove(prevID, function(err, records){
        if(err){
            res.render("deleteFail.jade");                
        }
        else{                
            res.render("deleteSuccess.jade");
        }
    });  
});

Where prevID is the _id of the note currently trying to be deleted. 其中prevID是当前尝试删除的注释的_id。 Any help is greatly appreciated! 任何帮助是极大的赞赏! Thanks 谢谢

Are you using mongoose? 你在用猫鼬吗? I am pretty certain the first argument has to be a query object, eg { _id: prevId } and not just the id directly. 我敢肯定,第一个参数必须是查询对象,例如{ _id: prevId }而不仅仅是ID。

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

相关问题 使用mongoose从mongodb集合中的文档数组中删除对象 - delete object from document array in mongodb collection using mongoose 通过ID删除MongoDB文档 - delete MongoDB document by ID 如何过滤MongoDB集合文档的数组 - How to filter MongoDB collection document's array 使用 DELETE 请求删除 MongoDB 集合中的最后一个文档 - Delete last document in MongoDB collection with DELETE request 如何使用Meteor通过ID从另一个集合中的文档返回文档属性? - How do I return document property from document in another collection by ID using Meteor? 当某个日期小于新的Date()时,如何从mongodb集合中删除文档 - How to delete a document from a mongodb collection when some date is smaller than new Date() 如何在羽毛应用程序的mongodb中同时使用相同的id删除两个不同集合中的文档 - How to remove document in two different collection using same id at same time in mongodb on feathers application 如何使用另一个集合中的字段值更新Mongodb集合中的文档 - How to update document in Mongodb collection with a field value from another collection MongoDB 创建引用另一个集合的文档 ID 的新文档 - MongoDB Create New Document that references another Collection's Document IDs 如何按文件ID删除行 - How to delete row by document ID
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM