Skip to content

Commit 6e4e90a

Browse files
authored
PYTHON-3167 Revert to using the count command for estimated_document_count (#934)
Resolves PYTHON-2885, PYTHON-3166, PYTHON-3224, and PYTHON-3219.
1 parent 05b55e8 commit 6e4e90a

29 files changed

+847
-1563
lines changed

.evergreen/resync-specs.sh

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,24 @@ cpjson () {
6868

6969
for spec in "$@"
7070
do
71+
# Match the spec dir name, the python test dir name, and/or common abbreviations.
7172
case "$spec" in
72-
bson*corpus)
73+
atlas-data-lake-testing|data_lake)
74+
cpjson atlas-data-lake-testing/tests/ data_lake
75+
;;
76+
bson-corpus|bson_corpus)
7377
cpjson bson-corpus/tests/ bson_corpus
7478
;;
75-
max*staleness)
79+
max-staleness|max_staleness)
7680
cpjson max-staleness/tests/ max_staleness
7781
;;
78-
connection*string)
82+
collection-management|collection_management)
83+
cpjson collection-management/tests/ collection_management
84+
;;
85+
connection-string|connection_string)
7986
cpjson connection-string/tests/ connection_string/test
8087
;;
81-
change*streams)
88+
change-streams|change_streams)
8289
cpjson change-streams/tests/ change_streams/
8390
;;
8491
client-side-encryption|csfle|fle)
@@ -87,32 +94,29 @@ do
8794
cpjson client-side-encryption/external/ client-side-encryption/external
8895
cpjson client-side-encryption/limits/ client-side-encryption/limits
8996
;;
90-
cmap|CMAP)
97+
cmap|CMAP|connection-monitoring-and-pooling)
9198
cpjson connection-monitoring-and-pooling/tests cmap
9299
rm $PYMONGO/test/cmap/wait-queue-fairness.json # PYTHON-1873
93100
;;
94-
command*monitoring)
101+
apm|APM|command-monitoring|command_monitoring)
95102
cpjson command-monitoring/tests command_monitoring
96103
;;
97104
crud|CRUD)
98105
cpjson crud/tests/ crud
99106
;;
100-
load*balancer)
107+
load-balancers|load_balancer)
101108
cpjson load-balancers/tests load_balancer
102109
;;
103-
initial-dns-seedlist-discovery|srv_seedlist)
110+
srv|SRV|initial-dns-seedlist-discovery|srv_seedlist)
104111
cpjson initial-dns-seedlist-discovery/tests/ srv_seedlist
105112
;;
106-
old_srv_seedlist)
107-
cpjson initial-dns-seedlist-discovery/tests srv_seedlist
108-
;;
109-
retryable*reads)
113+
retryable-reads|retryable_reads)
110114
cpjson retryable-reads/tests/ retryable_reads
111115
;;
112-
retryable*writes)
116+
retryable-writes|retryable_writes)
113117
cpjson retryable-writes/tests/ retryable_writes
114118
;;
115-
sdam|SDAM)
119+
sdam|SDAM|server-discovery-and-monitoring|discovery_and_monitoring)
116120
cpjson server-discovery-and-monitoring/tests/errors \
117121
discovery_and_monitoring/errors
118122
cpjson server-discovery-and-monitoring/tests/rs \
@@ -126,10 +130,10 @@ do
126130
cpjson server-discovery-and-monitoring/tests/load-balanced \
127131
discovery_and_monitoring/load-balanced
128132
;;
129-
sdam*monitoring)
133+
sdam-monitoring|sdam_monitoring)
130134
cpjson server-discovery-and-monitoring/tests/monitoring sdam_monitoring
131135
;;
132-
server*selection)
136+
server-selection|server_selection)
133137
cpjson server-selection/tests/ server_selection
134138
;;
135139
sessions)
@@ -140,13 +144,13 @@ do
140144
cpjson transactions-convenient-api/tests/ transactions-convenient-api
141145
rm $PYMONGO/test/transactions/legacy/errors-client.json # PYTHON-1894
142146
;;
143-
unified)
147+
unified|unified-test-format)
144148
cpjson unified-test-format/tests/ unified-test-format/
145149
;;
146-
uri|uri*options)
150+
uri|uri-options|uri_options)
147151
cpjson uri-options/tests uri_options
148152
;;
149-
stable-api)
153+
stable-api|versioned-api)
150154
cpjson versioned-api/tests versioned-api
151155
;;
152156
*)

doc/changelog.rst

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,37 @@
11
Changelog
22
=========
33

4+
Changes in Version 4.2
5+
----------------------
6+
7+
Bug fixes
8+
.........
9+
10+
- Fixed a bug where :meth:`~pymongo.collection.Collection.estimated_document_count`
11+
would fail with a "CommandNotSupportedOnView" error on views (`PYTHON-2885`_).
12+
13+
Unavoidable breaking changes
14+
............................
15+
16+
- :meth:`~pymongo.collection.Collection.estimated_document_count` now always uses
17+
the `count`_ command. Due to an oversight in versions 5.0.0-5.0.8 of MongoDB,
18+
the count command was not included in V1 of the :ref:`versioned-api-ref`.
19+
Users of the Stable API with estimated_document_count are recommended to upgrade
20+
their server version to 5.0.9+ or set :attr:`pymongo.server_api.ServerApi.strict`
21+
to ``False`` to avoid encountering errors (`PYTHON-3167`_).
22+
23+
.. _count: https://mongodb.com/docs/manual/reference/command/count/
24+
25+
Issues Resolved
26+
...............
27+
28+
See the `PyMongo 4.2 release notes in JIRA`_ for the list of resolved issues
29+
in this release.
30+
31+
.. _PYTHON-2885: https://jira.mongodb.org/browse/PYTHON-2885
32+
.. _PYTHON-3167: https://jira.mongodb.org/browse/PYTHON-3167
33+
.. _PyMongo 4.2 release notes in JIRA: https://jira.mongodb.org/secure/ReleaseNote.jspa?projectId=10004&version=33196
34+
435
Changes in Version 4.1.1
536
-------------------------
637

pymongo/collection.py

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1707,34 +1707,25 @@ def estimated_document_count(self, comment: Optional[Any] = None, **kwargs: Any)
17071707
command.
17081708
- `**kwargs` (optional): See list of options above.
17091709
1710+
.. versionchanged:: 4.2
1711+
This method now always uses the `count`_ command. Due to an oversight in versions
1712+
5.0.0-5.0.8 of MongoDB, the count command was not included in V1 of the
1713+
:ref:`versioned-api-ref`. Users of the Stable API with estimated_document_count are
1714+
recommended to upgrade their server version to 5.0.9+ or set
1715+
:attr:`pymongo.server_api.ServerApi.strict` to ``False`` to avoid encountering errors.
17101716
17111717
.. versionadded:: 3.7
1718+
.. _count: https://mongodb.com/docs/manual/reference/command/count/
17121719
"""
17131720
if "session" in kwargs:
17141721
raise ConfigurationError("estimated_document_count does not support sessions")
17151722
if comment is not None:
17161723
kwargs["comment"] = comment
17171724

17181725
def _cmd(session, server, sock_info, read_preference):
1719-
if sock_info.max_wire_version >= 12:
1720-
# MongoDB 4.9+
1721-
pipeline = [
1722-
{"$collStats": {"count": {}}},
1723-
{"$group": {"_id": 1, "n": {"$sum": "$count"}}},
1724-
]
1725-
cmd = SON([("aggregate", self.__name), ("pipeline", pipeline), ("cursor", {})])
1726-
cmd.update(kwargs)
1727-
result = self._aggregate_one_result(
1728-
sock_info, read_preference, cmd, collation=None, session=session
1729-
)
1730-
if not result:
1731-
return 0
1732-
return int(result["n"])
1733-
else:
1734-
# MongoDB < 4.9
1735-
cmd = SON([("count", self.__name)])
1736-
cmd.update(kwargs)
1737-
return self._count_cmd(session, sock_info, read_preference, cmd, collation=None)
1726+
cmd = SON([("count", self.__name)])
1727+
cmd.update(kwargs)
1728+
return self._count_cmd(session, sock_info, read_preference, cmd, collation=None)
17381729

17391730
return self._retryable_non_cursor_read(_cmd, None)
17401731

test/collection_management/timeseries-collection.json

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,6 @@
8282
"databaseName": "ts-tests"
8383
}
8484
},
85-
{
86-
"commandStartedEvent": {
87-
"command": {
88-
"listCollections": 1
89-
},
90-
"databaseName": "ts-tests"
91-
}
92-
},
9385
{
9486
"commandStartedEvent": {
9587
"command": {
@@ -204,14 +196,6 @@
204196
"databaseName": "ts-tests"
205197
}
206198
},
207-
{
208-
"commandStartedEvent": {
209-
"command": {
210-
"listCollections": 1
211-
},
212-
"databaseName": "ts-tests"
213-
}
214-
},
215199
{
216200
"commandStartedEvent": {
217201
"command": {
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
{
2+
"description": "aggregate-allowdiskuse",
3+
"schemaVersion": "1.0",
4+
"createEntities": [
5+
{
6+
"client": {
7+
"id": "client0",
8+
"observeEvents": [
9+
"commandStartedEvent"
10+
]
11+
}
12+
},
13+
{
14+
"database": {
15+
"id": "database0",
16+
"client": "client0",
17+
"databaseName": "crud-tests"
18+
}
19+
},
20+
{
21+
"collection": {
22+
"id": "collection0",
23+
"database": "database0",
24+
"collectionName": "coll0"
25+
}
26+
}
27+
],
28+
"initialData": [
29+
{
30+
"collectionName": "coll0",
31+
"databaseName": "crud-tests",
32+
"documents": []
33+
}
34+
],
35+
"tests": [
36+
{
37+
"description": "Aggregate does not send allowDiskUse when value is not specified",
38+
"operations": [
39+
{
40+
"object": "collection0",
41+
"name": "aggregate",
42+
"arguments": {
43+
"pipeline": [
44+
{
45+
"$match": {}
46+
}
47+
]
48+
}
49+
}
50+
],
51+
"expectEvents": [
52+
{
53+
"client": "client0",
54+
"events": [
55+
{
56+
"commandStartedEvent": {
57+
"command": {
58+
"aggregate": "coll0",
59+
"pipeline": [
60+
{
61+
"$match": {}
62+
}
63+
],
64+
"allowDiskUse": {
65+
"$$exists": false
66+
}
67+
},
68+
"commandName": "aggregate",
69+
"databaseName": "crud-tests"
70+
}
71+
}
72+
]
73+
}
74+
]
75+
},
76+
{
77+
"description": "Aggregate sends allowDiskUse false when false is specified",
78+
"operations": [
79+
{
80+
"object": "collection0",
81+
"name": "aggregate",
82+
"arguments": {
83+
"pipeline": [
84+
{
85+
"$match": {}
86+
}
87+
],
88+
"allowDiskUse": false
89+
}
90+
}
91+
],
92+
"expectEvents": [
93+
{
94+
"client": "client0",
95+
"events": [
96+
{
97+
"commandStartedEvent": {
98+
"command": {
99+
"aggregate": "coll0",
100+
"pipeline": [
101+
{
102+
"$match": {}
103+
}
104+
],
105+
"allowDiskUse": false
106+
},
107+
"commandName": "aggregate",
108+
"databaseName": "crud-tests"
109+
}
110+
}
111+
]
112+
}
113+
]
114+
},
115+
{
116+
"description": "Aggregate sends allowDiskUse true when true is specified",
117+
"operations": [
118+
{
119+
"object": "collection0",
120+
"name": "aggregate",
121+
"arguments": {
122+
"pipeline": [
123+
{
124+
"$match": {}
125+
}
126+
],
127+
"allowDiskUse": true
128+
}
129+
}
130+
],
131+
"expectEvents": [
132+
{
133+
"client": "client0",
134+
"events": [
135+
{
136+
"commandStartedEvent": {
137+
"command": {
138+
"aggregate": "coll0",
139+
"pipeline": [
140+
{
141+
"$match": {}
142+
}
143+
],
144+
"allowDiskUse": true
145+
},
146+
"commandName": "aggregate",
147+
"databaseName": "crud-tests"
148+
}
149+
}
150+
]
151+
}
152+
]
153+
}
154+
]
155+
}

0 commit comments

Comments
 (0)