12

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

I have tried

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

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

And

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

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

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:

  • I can see the connections in Mongo's log
  • I can add documents by manipulating the same model elsewhere

Related: How do I remove documents using Node.js Mongoose?

EDIT

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.

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
AsTeR
  • 7,247
  • 14
  • 60
  • 99

2 Answers2

23

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
});

Which is the same as:

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

});

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" )

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.

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
  • 1
    As I said, I have tried that too but it don't work either. Moreover Mongoose documentation examples do not agree with you http://mongoosejs.com/docs/api.html#query_Query-remove . Thanks for your help anyway. – AsTeR Jul 22 '14 at 07:50
  • @AsTeR it works for everyone else. You do not show the syntax as shown above and therefore you must be doing something different. – Neil Lunn Jul 22 '14 at 07:53
  • I said that I did remove the `.find()` step. But you are right, stating the obvious is important. I am looking at my collection through MongoHub and a command line client. I have noticed the added "s" trick... – AsTeR Jul 22 '14 at 08:07
  • Holy cow ... the thing was wrap in many callback and I didn't get the message but I had a syntax error in the line above. – AsTeR Jul 22 '14 at 08:07
13

The function .remove works only on Mongoose document model instance.This is an example to remove one model :

Model.findOne({ field : 'toto'}, function (err, model) {
    if (err) {
        return;
    }
    model.remove(function (err) {
        // if no error, your model is removed
    });
});

But, if you would remove elements with specific query, you should use the function remove like the find function :

Model.remove({ title : 'toto' }, function (err) {
    // if no error, your models are removed
});
Kevin B
  • 94,570
  • 16
  • 163
  • 180
throrin19
  • 17,796
  • 4
  • 32
  • 52