-
Type: New Feature
-
Resolution: Fixed
-
Priority: Major - P3
-
None
-
Component/s: None
-
None
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:
https://github.com/mongodb/specifications/commit/99ef4e71c68811912da49b451408764eba73015c
The reference implementation for this change is provided by the Python driver and is in code review now.
- causes
-
DRIVERS-2728 Document that countDocuments() uses $match and may not support the same filters as find/count commands
- Implementing
- depends on
-
CDRIVER-2691 Implement new count API
- Closed
-
CSHARP-2293 Implement new count API
- Closed
-
CXX-1594 Implement new count API
- Closed
-
GODRIVER-453 Implement new count API
- Closed
-
JAVA-2885 Implement new count API
- Closed
-
MOTOR-241 Implement new count API
- Closed
-
NODE-1501 Implement new count API
- Closed
-
PHPLIB-354 Implement new count API
- Closed
-
PYTHON-1580 Implement new count API
- Closed
-
RUBY-1354 Implement new count API
- Closed
-
RUST-65 Implement new count API
- Closed
- related to
-
DRIVERS-2518 Add rationale for why the filter parameter is required for countDocuments
- Backlog