[JAVA-2885] Implement new count API Created: 07/Jun/18  Updated: 28/Oct/23  Resolved: 18/Jun/18

Status: Closed
Project: Java Driver
Component/s: Query Operations
Affects Version/s: None
Fix Version/s: 3.8.0

Type: New Feature Priority: Major - P3
Reporter: Rathi Gnanasekaran Assignee: Ross Lawley
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
is depended on by DRIVERS-501 Implement new count API Closed
Epic Link: Java MongoDB 4.0 Support

 Description   

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.



 Comments   
Comment by Githook User [ 19/Jun/18 ]

Author:

{'username': 'rozza', 'name': 'Ross Lawley', 'email': 'ross.lawley@gmail.com'}

Message: Fix Count hint tests

Added version check

JAVA-2885
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/af121ae1ff09f5d9014b088c2b47f396bfa0c2b6

Comment by Githook User [ 18/Jun/18 ]

Author:

{'username': 'rozza', 'name': 'Ross Lawley', 'email': 'ross.lawley@gmail.com'}

Message: Count API updates

count() method deprecated
estimatedDocumentCount() methods added
documentCount() methods added

JAVA-2885
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/3e2776942313c771511d468395fcb9a4716238f3

Comment by Ross Lawley [ 15/Jun/18 ]

PR: https://github.com/rozza/mongo-java-driver/pull/279

Generated at Thu Feb 08 08:58:18 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.