Skip to content

Commit c430376

Browse files
authored
DRIVERS-2228 Revert to using the count command for estimatedDocumentCount (#1187)
1 parent e91f0ec commit c430376

20 files changed

+97
-1987
lines changed

source/atlas-data-lake-testing/tests/estimatedDocumentCount.json

+2-17
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,9 @@
1515
{
1616
"command_started_event": {
1717
"command": {
18-
"aggregate": "driverdata",
19-
"pipeline": [
20-
{
21-
"$collStats": {
22-
"count": {}
23-
}
24-
},
25-
{
26-
"$group": {
27-
"_id": 1,
28-
"n": {
29-
"$sum": "$count"
30-
}
31-
}
32-
}
33-
]
18+
"count": "driverdata"
3419
},
35-
"command_name": "aggregate",
20+
"command_name": "count",
3621
"database_name": "test"
3722
}
3823
}

source/atlas-data-lake-testing/tests/estimatedDocumentCount.yml

+2-5
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ tests:
1313
-
1414
command_started_event:
1515
command:
16-
aggregate: *collection_name
17-
pipeline:
18-
- $collStats: { count: {} }
19-
- $group: { _id: 1, n: { $sum: $count }}
20-
command_name: aggregate
16+
count: *collection_name
17+
command_name: count
2118
database_name: *database_name

source/crud/crud.rst

+41-28
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,8 @@ Read
179179
/**
180180
* Gets an estimate of the count of documents in a collection using collection metadata.
181181
*
182-
* See "Count API Details" section below.
182+
* See "Count API Details" section below for implementation and documentation
183+
* requirements.
183184
*/
184185
estimatedDocumentCount(options: Optional<EstimatedDocumentCountOptions>): Int64;
185186
@@ -412,9 +413,8 @@ Read
412413
* The comment can be any valid BSON type for server versions 4.4.14 and above.
413414
* For server versions between 4.4.0 and 4.4.14 string comment is supported.
414415
* Servers versions below 4.4.0 do not support comment for count command,
415-
* which is used to implement estimatedDocumentCount for server versions
416-
* versions less than 4.9.0. Therefore, providing a comment may result
417-
* in a server-side error.
416+
* which is used to implement estimatedDocumentCount. Therefore, providing a
417+
* comment may result in a server-side error.
418418
*/
419419
comment: Optional<any>;
420420
}
@@ -754,30 +754,39 @@ older sharded clusters.
754754
estimatedDocumentCount
755755
~~~~~~~~~~~~~~~~~~~~~~
756756

757-
On server versions greater than or equal to 4.9.0 (wire version 12 or higher),
758-
the estimatedDocumentCount function is implemented using the ``$collStats``
759-
aggregate pipeline stage with ``$group`` to gather results from multiple shards.
760-
As documented above, the only supported option is maxTimeMS::
761-
762-
pipeline = [
763-
{ '$collStats': { 'count': {} } },
764-
{ '$group': { '_id': 1, 'n': { '$sum': '$count' } } }
765-
]
766-
767-
Similar to the count command, the estimated count of documents is returned
768-
in the ``n`` field. Implementations can assume that the document containing
769-
the single result of the aggregation pipeline is contained in the first batch of
770-
the server's reply to the aggregate command. It is not necessary to execute a getMore
771-
operation to ensure that the result is available.
772-
773-
In the event this aggregation is run against a non-existent namespace, a NamespaceNotFound(26)
774-
error will be returned during execution. Drivers MUST interpret the server error code 26 as
775-
a ``0`` count.
776-
777-
For server versions less than 4.9.0 (wire version 11 or under), the estimatedDocumentCount
778-
function is implemented using the ``count`` command with no query filter, skip,
779-
limit, or other options that would alter the results. Once again, the only supported
780-
option is maxTimeMS.
757+
The estimatedDocumentCount function is implemented using the ``count`` command
758+
with no query filter, skip, limit, or other options that would alter the
759+
results. The only supported options are listed in the
760+
``EstimatedDocumentCountOptions`` type defined above.
761+
762+
Drivers MUST document that, due to an oversight in versions 5.0.0-5.0.7 of
763+
MongoDB, the ``count`` command, which estimatedDocumentCount uses in its
764+
implementation, was not included in v1 of the Stable API, and so users of the
765+
Stable API with estimatedDocumentCount are recommended to upgrade their server
766+
version to 5.0.8+ or set ``apiStrict: false`` to avoid encountering errors.
767+
768+
Drivers MUST document that the ``count`` server command is used to implement
769+
estimatedDocumentCount and that users can find more information via
770+
`Count: Behavior <https://www.mongodb.com/docs/manual/reference/command/count/#behavior>`_.
771+
772+
The 5.0-compat versions of many drivers updated their estimatedDocumentCount
773+
implementations to use the ``$collStats`` aggregation stage instead of the
774+
``count`` command. This had the unintended consequence of breaking
775+
estimatedDocumentCount on views, and so the change was seen as a
776+
backwards-incompatible regression and reverted. The release notes for the driver
777+
versions that include the reversion from ``$collStats`` back to ``count`` MUST
778+
document the following:
779+
780+
- The 5.0-compat release accidentally broke estimatedDocumentCount on views by
781+
changing its implementation to use ``aggregate`` and a ``$collStats`` stage
782+
instead of the ``count`` command.
783+
- The new release is fixing estimatedDocumentCount on views by reverting back to
784+
using ``count`` in its implementation.
785+
- Due to an oversight, the ``count`` command was omitted from the Stable API in
786+
server versions 5.0.0 - 5.0.7 and 5.1.0 - 5.3.1, so users of the Stable API
787+
with estimatedDocumentCount are recommended to upgrade their MongoDB clusters
788+
to 5.0.8 or 5.3.2 (if on Atlas) or set ``apiStrict: false`` when constructing
789+
their MongoClients.
781790

782791
~~~~~~~~~~~~~~
783792
countDocuments
@@ -2354,9 +2363,13 @@ Q: Where is ``singleBatch`` in FindOptions?
23542363
Q: Why are client-side errors raised for some unsupported options?
23552364
Server versions before 3.4 were inconsistent about reporting errors for unrecognized command options and may simply ignore them, which means a client-side error is the only way to inform users that such options are unsupported. For unacknowledged writes using OP_MSG, a client-side error is necessary because the server has no chance to return a response (even though a 3.6+ server is otherwise capable of reporting errors for unrecognized options). For unacknowledged writes using legacy opcodes (i.e. OP_INSERT, OP_UPDATE, and OP_DELETE), the message body has no field with which to express these options so a client-side error is the only mechanism to inform the user that such options are unsupported. The spec does not explicitly refer to unacknowledged writes using OP_QUERY primarily because a response document is always returned and drivers generally would not consider using OP_QUERY precisely for that reason.
23562365

2366+
Q: Why does reverting to using ``count`` instead of ``aggregate`` with ``$collStats`` for estimatedDocumentCount not require a major version bump in the drivers, even though it might break users of the Stable API?
2367+
SemVer `allows <https://semver.org/#what-if-i-inadvertently-alter-the-public-api-in-a-way-that-is-not-compliant-with-the-version-number-change-ie-the-code-incorrectly-introduces-a-major-breaking-change-in-a-patch-release>`_ for a library to include a breaking change in a minor or patch version if the change is required to fix another accidental breaking change introduced in a prior version and is not expected to further break a large number of users. Given that the original switch to ``$collStats`` was a breaking change due to it not working on views, the number of users using estimatedDocumentCount with ``apiStrict: true`` is small, and the server is back-porting the addition of ``count`` to the Stable API, it was decided that this change was acceptable to make in minor version releases of the drivers per the aforementioned allowance in the SemVer spec.
2368+
23572369
Changes
23582370
=======
23592371

2372+
* 2022-04-21: Revert to using the ``count`` command for ``estimatedDocumentCount``
23602373
* 2022-02-18: Add let to BulkWriteOptions.
23612374
* 2022-02-10: Specified that ``getMore`` command must explicitly send inherited comment.
23622375
* 2022-02-01: Add comment attribute to all helpers.

source/crud/tests/unified/estimatedDocumentCount-comment.json

+4-60
Original file line numberDiff line numberDiff line change
@@ -47,65 +47,10 @@
4747
],
4848
"tests": [
4949
{
50-
"description": "estimatedDocumentCount with document comment - post 4.9",
50+
"description": "estimatedDocumentCount with document comment",
5151
"runOnRequirements": [
5252
{
53-
"minServerVersion": "4.9.0"
54-
}
55-
],
56-
"operations": [
57-
{
58-
"name": "estimatedDocumentCount",
59-
"object": "collection0",
60-
"arguments": {
61-
"comment": {
62-
"key": "value"
63-
}
64-
},
65-
"expectResult": 3
66-
}
67-
],
68-
"expectEvents": [
69-
{
70-
"client": "client0",
71-
"events": [
72-
{
73-
"commandStartedEvent": {
74-
"command": {
75-
"aggregate": "coll0",
76-
"pipeline": [
77-
{
78-
"$collStats": {
79-
"count": {}
80-
}
81-
},
82-
{
83-
"$group": {
84-
"_id": 1,
85-
"n": {
86-
"$sum": "$count"
87-
}
88-
}
89-
}
90-
],
91-
"comment": {
92-
"key": "value"
93-
}
94-
},
95-
"commandName": "aggregate",
96-
"databaseName": "edc-comment-tests"
97-
}
98-
}
99-
]
100-
}
101-
]
102-
},
103-
{
104-
"description": "estimatedDocumentCount with document comment - pre 4.9",
105-
"runOnRequirements": [
106-
{
107-
"minServerVersion": "4.4.14",
108-
"maxServerVersion": "4.8.99"
53+
"minServerVersion": "4.4.14"
10954
}
11055
],
11156
"operations": [
@@ -141,11 +86,10 @@
14186
]
14287
},
14388
{
144-
"description": "estimatedDocumentCount with string comment - pre 4.9",
89+
"description": "estimatedDocumentCount with string comment",
14590
"runOnRequirements": [
14691
{
147-
"minServerVersion": "4.4.0",
148-
"maxServerVersion": "4.8.99"
92+
"minServerVersion": "4.4.0"
14993
}
15094
],
15195
"operations": [

source/crud/tests/unified/estimatedDocumentCount-comment.yml

+3-27
Original file line numberDiff line numberDiff line change
@@ -24,39 +24,16 @@ initialData:
2424
- { _id: 3, x: 33 }
2525

2626
tests:
27-
- description: "estimatedDocumentCount with document comment - post 4.9"
28-
runOnRequirements:
29-
- minServerVersion: "4.9.0"
30-
operations:
31-
- name: estimatedDocumentCount
32-
object: *collection0
33-
arguments:
34-
comment: &documentComment { key: "value"}
35-
expectResult: 3
36-
expectEvents:
37-
- client: *client0
38-
events:
39-
- commandStartedEvent:
40-
command:
41-
aggregate: *collection0Name
42-
pipeline: &pipeline
43-
- $collStats: { count: {} }
44-
- $group: { _id: 1, n: { $sum: $count }}
45-
comment: *documentComment
46-
commandName: aggregate
47-
databaseName: *database0Name
48-
49-
- description: "estimatedDocumentCount with document comment - pre 4.9"
27+
- description: "estimatedDocumentCount with document comment"
5028
runOnRequirements:
5129
# https://jira.mongodb.org/browse/SERVER-63315
5230
# Server supports count with comment of any type for comment starting from 4.4.14.
5331
- minServerVersion: "4.4.14"
54-
maxServerVersion: "4.8.99"
5532
operations:
5633
- name: estimatedDocumentCount
5734
object: *collection0
5835
arguments:
59-
comment: *documentComment
36+
comment: &documentComment { key: "value"}
6037
expectResult: 3
6138
expectEvents:
6239
- client: *client0
@@ -68,10 +45,9 @@ tests:
6845
commandName: count
6946
databaseName: *database0Name
7047

71-
- description: "estimatedDocumentCount with string comment - pre 4.9"
48+
- description: "estimatedDocumentCount with string comment"
7249
runOnRequirements:
7350
- minServerVersion: "4.4.0"
74-
maxServerVersion: "4.8.99"
7551
operations:
7652
- name: estimatedDocumentCount
7753
object: *collection0

0 commit comments

Comments
 (0)