Mongoose: DeprecationWarning: collection.count is deprecated

Created on 8 Aug 2018  路  4Comments  路  Source: Automattic/mongoose

Do you want to request a feature or report a bug?

Bug.

What is the current behavior?

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

If the current behavior is a bug, please provide the steps to reproduce.

I am not sure where this comes from, but is probably an internal library that mongoose is using.

What is the expected behavior?

No warning.

Please mention your node.js, mongoose and MongoDB version.
Node: v8.11.3
Mongoose: 5.2.7
MongoDB: 3.6

Most helpful comment

This isn't fixed, though. This is inherently a problem with what @NewEraCracker pointed out. For example, using findById will reproduce this error in [email protected] with [email protected] and [email protected].

Problem:
node_modules/mongoose/lib/query.js:2086

this._collection.count(conds, options, utils.tick(callback));

Fix:

this._collection.countDocuments(conds, options, utils.tick(callback));

All 4 comments

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

estimatedDocumentCount docs here
countDocuments docs here

This isn't fixed, though. This is inherently a problem with what @NewEraCracker pointed out. For example, using findById will reproduce this error in [email protected] with [email protected] and [email protected].

Problem:
node_modules/mongoose/lib/query.js:2086

this._collection.count(conds, options, utils.tick(callback));

Fix:

this._collection.countDocuments(conds, options, utils.tick(callback));

I changed the "this._collection.count (conds, options, utils.tick (callback));"
by "this._collection.collection.countDocuments (conds, options, utils.tick (callback));" and it worked for me.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

simonxca picture simonxca  路  3Comments

gustavomanolo picture gustavomanolo  路  3Comments

CodeurSauvage picture CodeurSauvage  路  3Comments

Igorpollo picture Igorpollo  路  3Comments

Soviut picture Soviut  路  3Comments