Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
69
rated 0 times [  70] [ 1]  / answers: 1 / hits: 48094  / 10 Years ago, tue, july 22, 2014, 12:00:00

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.


More From » node.js

 Answers
6

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.


[#70106] Sunday, July 20, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
sandy

Total Points: 59
Total Questions: 98
Total Answers: 98

Location: Falkland Islands
Member since Mon, Jul 13, 2020
4 Years ago
;