Reputation: 101
I'm developing a backend for a REST API. It uses MongoDB as a database. I am using MongoDB 5.3.3.
I would like to remove a collection of items, upon API request, but received the above error message:
DeprecationWarning: collection.remove is deprecated. Use deleteOne, deleteMany, or bulkWrite instead
How do I completely drop the collection?
Upvotes: 6
Views: 13904
Reputation: 1
This problem occurs in new versions of Mongoose. If you want to use Model.remove() method, you should change the current Mongoose version to "mongoose": "^5.10.14" or lower. Read more information about it at Deprecation Warnings.
Upvotes: 0
Reputation: 15
Use deleteOne or deleteMany accordingly. I have used deleteOne() instead of remove() function to delete items from delete api from mongodb collections. It worked for me.
app.delete('/todo/:item', function(req, res){
Todo.find({item: req.params.item.replace(/\-/g, " ")}).deleteOne(function(err, data){
if(err) throw err;
res.json(data);
});
});
Upvotes: 0
Reputation: 2092
What worked for me when I wanted to drop a specific collection is:
mongoose.connection.dropCollection(specificCollection.name)
// or you could use the collection's name exactly such as 'user'
The mongoose.connection.dropCollection()
method takes in the exact name for a collection which you probably defined when you made the schema.
Probably, the specific model you have defined somewhere in your code is something like:
mongoose.model('User', UserSchema)
Note that it is case insensitive, 'User'
is the same as 'user'
.
Upvotes: 1