Uploaded image for project: 'Node.js Driver'
  1. Node.js Driver
  2. NODE-3878

Collection.count() should use the count db command

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 4.4.0
    • Affects Version/s: None
    • Component/s: None
    • Labels:
    • 3
    • Not Needed

      What problem are you facing?

      There are currently 3 ways to count documents:

      1. Collection.count(): has been deprecated for a while. Uses the db count command and behaves differently depending on the argument. It relies on collection metadata when no argument is passed or actually counts the documents matching the query when a query is passed
      2. Collection.countDocuments(): counts the documents with an aggregation
      3. Collection.estimatedDocumentCount(): relies on collection metadata to return an estimated count of the docs

      mongosh offers the corresponding shell API to users, which is just a pass-through to the driver.

      We recently discovered that at least in the most recent version of the driver, the semantics of Collection.count() has changed: it now always behaves like Collection.countDocuments(), which means that it might unexpectedly trigger a collection scan.

      This unexpected behavior may cause performance degradations in production clusters as it's quite common for customers to have scripts for the legacy shell with calls to count() that are being ported to mongosh and that under the hood will behave very differently from what they expect.

      We currently have about 17% of the 13k daily active users that use count() on a daily basis so we will want to fix this sooner rather than later.

      What driver and relevant dependency versions are you using?

      ^4.3.0

      Steps to reproduce?

      Check what count() does.

            Assignee:
            bailey.pearson@mongodb.com Bailey Pearson
            Reporter:
            massimiliano.marcon@mongodb.com Massimiliano Marcon
            Neal Beeken
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: