Mongoose Model.remove(回调)不会从我的集合中删除任何内容 [英] Mongoose Model.remove(callback) doesn't remove anything from my collection

查看:370
本文介绍了Mongoose Model.remove(回调)不会从我的集合中删除任何内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我的Mongoose数据库中删除所有内容,但似乎没有任何效果。

I'm trying to remove all content from my Mongoose database but nothing seems to work.

我试过了

# CoffeeScript
MyModel.find().remove((err) -> console.log('purge callback'))

# JavaScript
MyModel.find().remove(function() { console.log('purge callback') })

# CoffeeScript
MyModel.find().remove({}, (err) -> console.log('purge callback'))

# JavaScript
MyModel.find().remove({}, function() { console.log('purge callback') })

甚至删除 .find()步骤或添加 .exec()我的回调从不显示,我的数据仍然在这里。

Even removing the .find() step or adding a .exec() my callback never shows and my data are still here.

我很确定我的模型和连接都没问题:

I am pretty sure that my model and connection are ok:


  • 我可以在Mongo的日志中看到连接

  • 我可以通过在其他地方操纵相同的模型来添加文档

相关:如何使用Node.js Mongoose删除文档?

编辑

我的问题是由未显示的语法错误引起的。所选答案确实有效,上述代码也是如此。如果有必要,欢迎版主删除我的问题。

My problem was caused by a syntax mistake that wasn't displayed. The selected answer does work and so does the above code. Moderators are welcome to remove my question if it seems necessary.

推荐答案

这不是query对象,这里唯一有效的方法是 .remove()

It's not a "query" object as returned by Mongoose, the only valid method here is .remove():

MyModel.remove(function(err,removed) {

   // where removed is the count of removed documents
});

与以下内容相同:

MyModel.remove({}, function(err,removed) {

});

另外,您如何确定没有删除任何文件?可能正在寻找错误的收藏品。默认情况下,Mongoose会将集合名称复数化,除非您明确指定集合名称,如下所示:

Also, how are you determining no documents are removed? Possibly looking in the wrong collection. Mongoose pluralizes the collection name by default unless you explicitly specify the collection name as in:

mongoose.Model( "MyModel", myModelSchema, "mymodel" )

如果没有第三个参数或以其他方式指定模式,则隐含了集合名称是mymodels。因此,请检查您是否拥有正确的集合以及您希望删除文档的正确数据库连接。

Without that third argument or otherwise specifying on the schema the collection name is implied to be "mymodels". So check that you have the correct collection as well as the correct database connection where you expect the documents to be removed.

这篇关于Mongoose Model.remove(回调)不会从我的集合中删除任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆