This ticket will entail removing MongoCollection.count and implementing two new MongoCollection methods, estimatedDocumentCount and countDocuments.
Both of those methods have already been sketched out in MongoCollection+Read.swift, but the bodies need to be filled in. For each method, we should:
- Ensure that the public API and the options types match those laid out in the CRUD specification
- Create a new Operation type for each method (use CountOperation as an example)
- Have each operation call through to the corresponding count method in libmongoc (see collection documentation here.)
Additionally, we should pull in the new spec test files for these count methods and add them to the CRUD test runner.
Some more context for this change below.
Drivers supporting MongoDB 4.0 must deprecate the count() helper and add two new helpers - estimatedDocumentCount() and countDocuments(). Both helpers are supported with MongoDB 2.6+.
The names of the new helpers were chosen to make it clear how they behave and exactly what they do. The estimatedDocumentCount helper returns an estimate of the count of documents in the collection using collection metadata, rather than counting the documents or consulting an index. The countDocuments helper counts the documents that match the provided query filter using an aggregation pipeline.
The count() helper is deprecated. It has always been implemented using the count command. The behavior of the count command differs depending on the options passed to it and the topology in use and may or may not provide an accurate count. When no query filter is provided the count command provides an estimate using collection metadata. Even when provided with a query filter the count command can return inaccurate results with a sharded cluster if orphaned documents exist or if a chunk migration is in progress. The countDocuments helper avoids these sharded cluster problems entirely when used with MongoDB 3.6+, and when using `Primary` read preference with older sharded clusters.
The following spec change describes the necessary changes in detail, including implementation notes and yml / json spec tests: