Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I want to get the count of posts documents using:

db.collection('posts').count()

But, I got a warning:

DeprecationWarning: collection.count is deprecated, and will be removed in a future version. Use collection.countDocuments or collection.estimatedDocumentCount instead

Here is my mongodb nodejs driver version:

  "dependencies": {
    "mongodb": "^3.1.0"
  },
  "devDependencies": {
    "@types/mongodb": "^3.1.0",
    "chai": "^4.1.2",
    "mocha": "^5.1.1",
    "ts-node": "^7.0.0",
    "tslint": "^5.10.0",
    "typescript": "^2.9.2"
  }

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

How can I solve this warning?

like image 565
slideshowp2 Avatar asked Jul 03 '18 03:07

slideshowp2


People also ask

What is deprecation warning in MongoDB?

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.

What is deprecation warning in node JS?

DeprecationWarning errors are logged by the Node. js runtime when your code (or one of the dependencies in your code) calls a deprecated API. These warnings usually include a DEP deprecation code. They are logged using console. error and end up in your CloudWatch logs.

What is useUnifiedTopology true?

useUnifiedTopology: False by default. Set to true to opt in to using the MongoDB driver's new connection management engine. You should set this option to true , except for the unlikely case that it prevents you from maintaining a stable connection.

What is unified topology MongoDB?

The unified topology is the first step in a paradigm shift away from a concept of “connecting” to a MongoDB deployment using a connect method. Consider for a moment what it means to be connected to a replica set: do we trigger this state when connected to a primary?


2 Answers

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.

like image 159
Wan Bachtiar Avatar answered Nov 02 '22 05:11

Wan Bachtiar


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

like image 4
pranita mohite Avatar answered Nov 02 '22 04:11

pranita mohite