Deprecation Warnings
There are several deprecations in the MongoDB Node.js driver that Mongoose users should be aware of. Mongoose provides options to work around these deprecation warnings, but you need to test whether these options cause any problems for your application. Please report any issues on GitHub.
Summary
To fix all deprecation warnings, follow the below steps:
- Replace
update()
withupdateOne()
,updateMany()
, orreplaceOne()
- Replace
remove()
withdeleteOne()
ordeleteMany()
. - Replace
count()
withcountDocuments()
, unless you want to count how many documents are in the whole collection (no filter). In the latter case, useestimatedDocumentCount()
.
Read below for more a more detailed description of each deprecation warning.
remove()
The MongoDB driver’s remove()
function is deprecated in favor of deleteOne()
and deleteMany()
. This is to comply with the MongoDB CRUD specification, which aims to provide a consistent API for CRUD operations across all MongoDB drivers.
DeprecationWarning: collection.remove is deprecated. Use deleteOne,
deleteMany, or bulkWrite instead.
To remove this deprecation warning, replace any usage of remove()
with deleteMany()
, unless you specify the single
option to remove()
. The single
option limited remove()
to deleting at most one document, so you should replace remove(filter, { single: true })
with deleteOne(filter)
.
// Replace this:
MyModel.remove({ foo: 'bar' });
// With this:
MyModel.deleteMany({ foo: 'bar' });
// Replace this:
MyModel.remove({ answer: 42 }, { single: true });
// With this:
MyModel.deleteOne({ answer: 42 });
update()
Like remove()
, the update()
function is deprecated in favor of the more explicit updateOne()
, updateMany()
, and replaceOne()
functions. You should replace update()
with updateOne()
, unless you use the multi
or overwrite
options.
collection.update is deprecated. Use updateOne, updateMany, or bulkWrite
instead.
// Replace this:
MyModel.update({ foo: 'bar' }, { answer: 42 });
// With this:
MyModel.updateOne({ foo: 'bar' }, { answer: 42 });
// If you use `overwrite: true`, you should use `replaceOne()` instead:
MyModel.update(filter, update, { overwrite: true });
// Replace with this:
MyModel.replaceOne(filter, update);
// If you use `multi: true`, you should use `updateMany()` instead:
MyModel.update(filter, update, { multi: true });
// Replace with this:
MyModel.updateMany(filter, update);
count()
The MongoDB server has deprecated the count()
function in favor of two separate functions, countDocuments()
and estimatedDocumentCount()
.
DeprecationWarning: collection.count is deprecated, and will be removed in a future version. Use collection.countDocuments or collection.estimatedDocumentCount instead
The difference between the two is countDocuments()
can accept a filter parameter like find()
. The estimatedDocumentCount()
function is faster, but can only tell you the total number of documents in a collection. You cannot pass a filter
to estimatedDocumentCount()
.
To migrate, replace count()
with countDocuments()
unless you do not pass any arguments to count()
. If you use count()
to count all documents in a collection as opposed to counting documents that match a query, use estimatedDocumentCount()
instead of countDocuments()
.
// Replace this:
MyModel.count({ answer: 42 });
// With this:
MyModel.countDocuments({ answer: 42 });
// If you're counting all documents in the collection, use
// `estimatedDocumentCount()` instead.
MyModel.count();
// Replace with:
MyModel.estimatedDocumentCount();
// Replace this:
MyModel.find({ answer: 42 }).count().exec();
// With this:
MyModel.find({ answer: 42 }).countDocuments().exec();
// Replace this:
MyModel.find().count().exec();
// With this, since there's no filter
MyModel.find().estimatedDocumentCount().exec();