Javascript 如何使用 Node.js Mongoose 删除文档?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5809788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I remove documents using Node.js Mongoose?
提问by TIMEX
FBFriendModel.find({
id: 333
}, function (err, docs) {
docs.remove(); //Remove all the documents that match!
});
The above doesn't seem to work. The records are still there.
以上似乎不起作用。记录还在。
Can someone fix?
有人可以修复吗?
回答by Yusuf X
If you don't feel like iterating, try FBFriendModel.find({ id:333 }).remove( callback );
or FBFriendModel.find({ id:333 }).remove().exec();
如果您不想迭代,请尝试FBFriendModel.find({ id:333 }).remove( callback );
或FBFriendModel.find({ id:333 }).remove().exec();
mongoose.model.find
returns a Query, which has a remove
function.
mongoose.model.find
返回一个Query,它有一个remove
函数。
Update for Mongoose v5.5.3 - remove()
is now deprecated.Use deleteOne()
, deleteMany()
or findOneAndDelete() instead.
Mongoose v5.5.3 的更新 -remove()
现在已弃用。使用deleteOne()
,deleteMany()
或findOneAndDelete() instead.
回答by diosney
UPDATE: Mongoose version (5.5.3)
更新:猫鼬版本(5.5.3)
remove() is deprecated and you can use deleteOne(), deleteMany(), or bulkWrite() instead.
remove() 已弃用,您可以改用 deleteOne()、deleteMany() 或 bulkWrite()。
As of "mongoose": ">=2.7.1"
you can remove the document directly with the .remove()
method rather than finding the document and then removing it which seems to me more efficient and easy to maintain.
由于"mongoose": ">=2.7.1"
你可以直接删除文档.remove()
的方法,而不是找到文档,然后删除它,这似乎对我来说更高效和易于维护。
See example:
见示例:
Model.remove({ _id: req.body.id }, function(err) {
if (!err) {
message.type = 'notification!';
}
else {
message.type = 'error';
}
});
UPDATE:
更新:
As of mongoose 3.8.1
, there are several methods that lets you remove directly a document, say:
至于 mongoose 3.8.1
,有几种方法可以让您直接删除文档,例如:
remove
findByIdAndRemove
findOneAndRemove
remove
findByIdAndRemove
findOneAndRemove
Refer to mongoose API docsfor further information.
有关更多信息,请参阅mongoose API 文档。
回答by mtkopone
docs
is an array of documents. so it doesn't have a mongooseModel.remove()
method.
docs
是一个文档数组。所以它没有mongooseModel.remove()
方法。
You can iterate and remove each document in the array separately.
您可以分别迭代和删除数组中的每个文档。
Or - since it looks like you are finding the documents by a (probably) unique id - use findOne
instead of find
.
或者 - 因为看起来你是通过(可能)唯一的 id 查找文档 - 使用findOne
而不是find
.
回答by José Pinto
This for me is the best as of version 3.8.1:
这对我来说是 3.8.1 版本中最好的:
MyModel.findOneAndRemove({field: 'newValue'}, function(err){...});
And it requires only one DB call.
Use this given that you don't perform any remove
actions pior to the search and removal.
它只需要一次数据库调用。考虑到您remove
在搜索和删除之前不执行任何操作,请使用此选项。
回答by Sandro Munda
Simply do
简单地做
FBFriendModel.remove().exec();
回答by Amol M Kulkarni
mongoose.model.find()
returns a Query Objectwhich also has a remove()
function.
mongoose.model.find()
返回一个查询对象,它也有一个remove()
函数。
You can use mongoose.model.findOne()
as well, if you want to remove only one unique document.
mongoose.model.findOne()
如果您只想删除一个唯一的文档,您也可以使用。
Else you can follow traditional approach as well where you first retrieving the document and then remove.
否则,您也可以遵循传统方法,首先检索文档然后删除。
yourModelObj.findById(id, function (err, doc) {
if (err) {
// handle error
}
doc.remove(callback); //Removes the document
})
Following are the ways on model
object you can do any of the following to remove document(s):
以下是model
对象的方法,您可以执行以下任何操作来删除文档:
yourModelObj.findOneAndRemove(conditions, options, callback)
yourModelObj.findByIdAndRemove(id, options, callback)
yourModelObj.findByIdAndRemove(id, options, callback)
yourModelObj.remove(conditions, callback);
yourModelObj.remove(conditions, callback);
var query = Comment.remove({ _id: id });
query.exec();
回答by Samyak Jain
remove()
has been deprecated. Use deleteOne()
, deleteMany()
or bulkWrite()
.
remove()
已被弃用。使用deleteOne()
,deleteMany()
或bulkWrite()
。
The code I use
我使用的代码
TeleBot.deleteMany({chatID: chatID}, function (err, _) {
if (err) {
return console.log(err);
}
});
回答by alexandru.topliceanu
To generalize you can use:
要概括,您可以使用:
SomeModel.find( $where, function(err,docs){
if (err) return console.log(err);
if (!docs || !Array.isArray(docs) || docs.length === 0)
return console.log('no docs found');
docs.forEach( function (doc) {
doc.remove();
});
});
Another way to achieve this is:
实现这一目标的另一种方法是:
SomeModel.collection.remove( function (err) {
if (err) throw err;
// collection is now empty but not deleted
});
回答by damphat
Be careful with findOne and remove!
小心 findOne 并删除!
User.findOne({name: 'Alice'}).remove().exec();
The code above removes ALLusers named 'Alice' instead of the first oneonly.
上面的代码删除了所有名为“Alice”的用户,而不是仅删除第一个用户。
By the way, I prefer to remove documents like this:
顺便说一句,我更喜欢删除这样的文件:
User.remove({...}).exec();
Or provide a callback and omit the exec()
或者提供回调并省略 exec()
User.remove({...}, callback);
回答by Danish
model.remove({title:'danish'}, function(err){
if(err) throw err;
});