Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-33753

count without predicate should be sharding aware

    • Type: Icon: Bug Bug
    • Resolution: Unresolved
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: Querying
    • Query Execution
    • ALL
    • 156

      A "fast count" (a count without a predicate) currently works by just reading collection metadata. While this is very fast, it may be inaccurate on sharded clusters if there are orphan documents.

      This is follow-on work after SERVER-3645, which made counts with predicates sharding aware. Although fixing this would cause the count command to be slower, users who need an extremely fast way to get an approximate number of documents in a collection could use $collStats (see below).

      Workarounds:

      In general, if one needs an accurate count of how many documents are in a collection, we do not recommend using the count command. Instead, we suggest using the $count aggregation stage, like this:

      db.foo.aggregate([{$count: "nDocs"}]);
      

      For users who need the performance of "fast count", and are okay with approximate results, we suggest using the $collStats aggregation stage instead of the count command:

      db.foo.aggregate( [ { $collStats: { count: { } } } ] )
      

            Assignee:
            backlog-query-execution [DO NOT USE] Backlog - Query Execution
            Reporter:
            ian.boros@mongodb.com Ian Boros
            Votes:
            2 Vote for this issue
            Watchers:
            14 Start watching this issue

              Created:
              Updated: