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

Multi tool use
mongodb-nodejs-driver, DeprecationWarning: collection.count is deprecated
I want to get my documents count using:
db.collection('posts').count()
db.collection('posts').count()
But, I got a waning:
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.
countDocuments
estimatedDocumentCount
index.d.ts
How can I solve this warning?
of course it's a right syntax. You can test it in mongo shell.
– novaline
Jul 3 at 6:10
1 Answer
1
As you figured out, starting from MongoDB Node.JS driver v3.1 the count()
method has been deprecated and will be replaced with :
count()
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)
mongodb
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.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Is this right syntax? I think you have problem in your query ,what is post inside bracket, if you want to count anything on field , first you need to find then count for ex:db.collectionName.find({filedName : filedValue}).count()
– Mohammad Raheem
Jul 3 at 5:19