Skip to content

Commit bfc42d0

Browse files
authored
Merge pull request #2529 from bagerard/fix_performance_issue_count
Use estimated_documents_count OR documents_count based on query
2 parents ff701bd + b5be012 commit bfc42d0

File tree

3 files changed

+13
-11
lines changed

3 files changed

+13
-11
lines changed

docs/changelog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Development
1111
- Add tests against MongoDB 6.0 and MongoDB 7.0 in the pipeline
1212
- Fix validate() not being called when inheritance is used in EmbeddedDocument and validate is overriden #2784
1313
- Add support for readPreferenceTags in connection parameters #2644
14+
- Use estimated_documents_count OR documents_count when count is called, based on the query #2529
1415

1516
Changes in 0.27.0
1617
=================

mongoengine/pymongo_support.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,12 @@ def count_documents(
3737
# count_documents appeared in pymongo 3.7
3838
if PYMONGO_VERSION >= (3, 7):
3939
try:
40-
return collection.count_documents(filter=filter, **kwargs)
40+
if not filter and set(kwargs) <= {"max_time_ms"}:
41+
# when no filter is provided, estimated_document_count
42+
# is a lot faster as it uses the collection metadata
43+
return collection.estimated_document_count(**kwargs)
44+
else:
45+
return collection.count_documents(filter=filter, **kwargs)
4146
except OperationFailure as err:
4247
if PYMONGO_VERSION >= (4,):
4348
raise
@@ -46,15 +51,10 @@ def count_documents(
4651
# with .count but are no longer working with count_documents (i.e $geoNear, $near, and $nearSphere)
4752
# fallback to deprecated Cursor.count
4853
# Keeping this should be reevaluated the day pymongo removes .count entirely
49-
message = str(err)
50-
if not (
51-
"not allowed in this context" in message
52-
and (
53-
"$where" in message
54-
or "$geoNear" in message
55-
or "$near" in message
56-
or "$nearSphere" in message
57-
)
54+
if (
55+
"$geoNear, $near, and $nearSphere are not allowed in this context"
56+
not in str(err)
57+
and "$where is not allowed in this context" not in str(err)
5858
):
5959
raise
6060

tests/test_context_managers.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
switch_db,
1515
)
1616
from mongoengine.pymongo_support import count_documents
17+
from tests.utils import MongoDBTestCase
1718

1819

19-
class TestContextManagers:
20+
class TestContextManagers(MongoDBTestCase):
2021
def test_set_write_concern(self):
2122
connect("mongoenginetest")
2223

0 commit comments

Comments
 (0)