Skip to content

Commit 4640324

Browse files
committed
allow QuerySet.count() to query multiple collections
1 parent 342d16d commit 4640324

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

django_mongodb/features.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
9191
"annotations.tests.NonAggregateAnnotationTestCase.test_annotation_with_m2m",
9292
"annotations.tests.NonAggregateAnnotationTestCase.test_chaining_annotation_filter_with_m2m",
9393
"annotations.tests.NonAggregateAnnotationTestCase.test_mti_annotations",
94-
"lookup.tests.LookupTests.test_lookup_collision",
9594
"expressions.test_queryset_values.ValuesExpressionsTests.test_values_list_expression",
9695
"expressions.test_queryset_values.ValuesExpressionsTests.test_values_list_expression_flat",
9796
"expressions.tests.IterableLookupInnerExpressionsTests.test_expressions_in_lookups_join_choice",
@@ -380,6 +379,7 @@ def django_test_expected_failures(self):
380379
"timezones.tests.NewDatabaseTests.test_query_datetimes_in_other_timezone",
381380
},
382381
"QuerySet.distinct() is not supported.": {
382+
"lookup.tests.LookupTests.test_lookup_collision_distinct",
383383
"update.tests.AdvancedTests.test_update_all",
384384
},
385385
"QuerySet.extra() is not supported.": {

django_mongodb/query.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,8 @@ def count(self, limit=None, skip=None):
6363
Return the number of objects that would be returned, if this query was
6464
executed, up to `limit`, skipping `skip`.
6565
"""
66-
kwargs = {}
67-
if limit is not None:
68-
kwargs["limit"] = limit
69-
if skip is not None:
70-
kwargs["skip"] = skip
71-
return self.collection.count_documents(self.mongo_query, **kwargs)
66+
result = list(self.get_cursor(count=True, limit=limit, skip=skip))
67+
return result[0]["__count"] if result else 0
7268

7369
def order_by(self, ordering):
7470
"""
@@ -95,7 +91,16 @@ def delete(self):
9591
return self.collection.delete_many(self.mongo_query, **options).deleted_count
9692

9793
@wrap_database_errors
98-
def get_cursor(self):
94+
def get_cursor(self, count=False, limit=None, skip=None):
95+
"""
96+
Return a pymongo CommandCursor that can be iterated on to give the
97+
results of the query.
98+
99+
If `count` is True, return a single document with the number of
100+
documents that match the query.
101+
102+
Use `limit` or `skip` to override those options of the query.
103+
"""
99104
if self.query.low_mark == self.query.high_mark:
100105
return []
101106
fields = {}
@@ -129,10 +134,16 @@ def get_cursor(self):
129134
pipeline.append({"$project": fields})
130135
if self.ordering:
131136
pipeline.append({"$sort": dict(self.ordering)})
132-
if self.query.low_mark > 0:
137+
if skip is not None:
138+
pipeline.append({"$skip": skip})
139+
elif self.query.low_mark > 0:
133140
pipeline.append({"$skip": self.query.low_mark})
134-
if self.query.high_mark is not None:
141+
if limit is not None:
142+
pipeline.append({"$limit": limit})
143+
elif self.query.high_mark is not None:
135144
pipeline.append({"$limit": self.query.high_mark - self.query.low_mark})
145+
if count:
146+
pipeline.append({"$group": {"_id": None, "__count": {"$sum": 1}}})
136147
return self.collection.aggregate(pipeline)
137148

138149

0 commit comments

Comments
 (0)