[英]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.