FBFriendModel.find({
    id: 333
}, function (err, docs) {
    docs.remove(); //Remove all the documents that match!
});
以上似乎不起作用。记录还在。
有人可以修复吗?
FBFriendModel.find({
    id: 333
}, function (err, docs) {
    docs.remove(); //Remove all the documents that match!
});
以上似乎不起作用。记录还在。
有人可以修复吗?
更新:mongoose版本(5.5.3)
remove() 已弃用,您可以改用 deleteOne()、deleteMany() 或 bulkWrite()。
至于"mongoose": ">=2.7.1"您可以直接使用该.remove()方法删除文档,而不是查找文档然后删除它,这在我看来更有效且易于维护。
见示例:
Model.remove({ _id: req.body.id }, function(err) {
    if (!err) {
            message.type = 'notification!';
    }
    else {
            message.type = 'error';
    }
});
更新:
至于 mongoose 3.8.1,有几种方法可以让您直接删除文档,例如:
removefindByIdAndRemovefindOneAndRemove有关更多信息,请参阅mongoose API 文档。
docs是一个文档数组。所以它没有mongooseModel.remove()方法。
您可以分别迭代和删除数组中的每个文档。
或者 - 因为看起来您是通过(可能)唯一的 id 查找文档 - 使用findOne而不是find.
这对我来说是 3.8.1 版本中最好的:
MyModel.findOneAndRemove({field: 'newValue'}, function(err){...});
它只需要一次数据库调用。考虑到您remove在搜索和删除之前不执行任何操作,请使用此选项。
remove()已被弃用。使用deleteOne(),deleteMany()或bulkWrite()。
我使用的代码
TeleBot.deleteMany({chatID: chatID}, function (err, _) {
                if (err) {
                    return console.log(err);
                }
            });