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() with updateOne(), updateMany(), or replaceOne()
  • Replace remove() with deleteOne() or deleteMany().
  • Replace count() with countDocuments(), unless you want to count how many documents are in the whole collection (no filter). In the latter case, use estimatedDocumentCount().

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.

  1. DeprecationWarning: collection.remove is deprecated. Use deleteOne,
  2. 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).

  1. // Replace this:
  2. MyModel.remove({ foo: 'bar' });
  3. // With this:
  4. MyModel.deleteMany({ foo: 'bar' });
  5. // Replace this:
  6. MyModel.remove({ answer: 42 }, { single: true });
  7. // With this:
  8. 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.

  1. collection.update is deprecated. Use updateOne, updateMany, or bulkWrite
  2. instead.
  1. // Replace this:
  2. MyModel.update({ foo: 'bar' }, { answer: 42 });
  3. // With this:
  4. MyModel.updateOne({ foo: 'bar' }, { answer: 42 });
  5. // If you use `overwrite: true`, you should use `replaceOne()` instead:
  6. MyModel.update(filter, update, { overwrite: true });
  7. // Replace with this:
  8. MyModel.replaceOne(filter, update);
  9. // If you use `multi: true`, you should use `updateMany()` instead:
  10. MyModel.update(filter, update, { multi: true });
  11. // Replace with this:
  12. MyModel.updateMany(filter, update);

count()

The MongoDB server has deprecated the count() function in favor of two separate functions, countDocuments() and estimatedDocumentCount().

  1. 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().

  1. // Replace this:
  2. MyModel.count({ answer: 42 });
  3. // With this:
  4. MyModel.countDocuments({ answer: 42 });
  5. // If you're counting all documents in the collection, use
  6. // `estimatedDocumentCount()` instead.
  7. MyModel.count();
  8. // Replace with:
  9. MyModel.estimatedDocumentCount();
  10. // Replace this:
  11. MyModel.find({ answer: 42 }).count().exec();
  12. // With this:
  13. MyModel.find({ answer: 42 }).countDocuments().exec();
  14. // Replace this:
  15. MyModel.find().count().exec();
  16. // With this, since there's no filter
  17. MyModel.find().estimatedDocumentCount().exec();