mongodb-nodejs-driver, DeprecationWarning: collection.count is deprecated

As you figured out, starting from MongoDB Node.JS driver v3.1 the count() method has been deprecated and will be replaced with :

  • Collection.countDocuments()
  • Collection.estimatedDocumentCount()

These methods have been added to node-mongodb-native package itself. For example, via the MongoDB Node.JS driver you should be able to do:

db.collection("posts").countDocuments(
  {}, // filters
  {}, // options
  function(error, result) {
    console.log(result);
  }
);

See also NODE-1501

There is no countDocuments or estimatedDocumentCount in index.d.ts file.

This is because the TypeScript definitions for the mongodb npm package has not been updated to include the two new methods. The list of types is actually maintained separately by community via DefinitelyTyped (GitHub: DefinitelyTyped)

I have submitted a pull request DefinitelyTyped #27008 to add the new methods. Once approved and published you should be able to see the typed definitions.


The underlying mongodb driver has deprecated the .count() method.You should use .estimatedDocumentCount() or .countDocuments() instead.