-
Type: Bug
-
Resolution: Unresolved
-
Priority: 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: { } } } ] )
- is related to
-
SERVER-3645 Sharded collection counts (on primary) can report too many results
- Closed