[SERVER-33753] count without predicate should be sharding aware Created: 08/Mar/18  Updated: 07/Jun/23

Status: Backlog
Project: Core Server
Component/s: Querying
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Ian Boros Assignee: Backlog - Query Execution
Resolution: Unresolved Votes: 2
Labels: query-44-grooming
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to SERVER-3645 Sharded collection counts (on primary... Closed
Assigned Teams:
Query Execution
Operating System: ALL
Participants:
Linked BF Score: 156

 Description   

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: { } } } ] )



 Comments   
Comment by Lucas [ 06/Sep/19 ]

Hello. I would like to see this being implemented.

Thanks.

Generated at Thu Feb 08 04:34:29 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.