Skip to content

Add warning to countDocuments() #6713

Closed
@bazineta

Description

@bazineta

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

Request a feature.

What is the current behavior?

The close similarity of count and countDocuments, and the seemingly benign nature of the countDocuments documentation, i.e., Counts number of matching documents in a database collection., will, I suspect, lead users to believe that they are functionally equivalent.

They are not; countDocuments performs a scan.

I suspect that in the majority of cases, estimatedDocumentCount is the appropriate replacement function.

A common situation is that one will replace the deprecated count with countDocuments and verify against a test environment. Presuming the test environment is, as is common, one with collections of a much lower document count than the corresponding production environment, there'll then be a false sense of security imparted. When the code is then promoted to production, perhaps with gigantic collections, the joy of watching the primary server page itself into the ground and a secondary takeover will be yours.

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

What is the expected behavior?

I think the documentation for countDocuments should be much, much more proactive in warning about this, and directing the user that estimatedDocumentCount is probably what they want instead.

Please mention your node.js, mongoose and MongoDB version.

Mongoose 5.2.3. Node and MongoDB, any supported version.

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsThis issue is due to a mistake or omission in the mongoosejs.com documentation

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions