Skip to content

Commit 00fb06b

Browse files
committed
Polish the wrapping logic
1 parent 09083d3 commit 00fb06b

File tree

2 files changed

+2
-5
lines changed

2 files changed

+2
-5
lines changed

django_mongodb_backend/compiler.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ def _build_aggregation_pipeline(self, ids, group):
240240
if not ids:
241241
pipeline.append({"$group": {"_id": None, **group}})
242242
# If ids is empty, a global group-by is applied
243-
self.wrap_for_global_aggregation = True
243+
self.wrap_for_global_aggregation = not bool(self.having)
244244
else:
245245
group["_id"] = ids
246246
pipeline.append({"$group": group})
@@ -326,7 +326,7 @@ def pre_sql_setup(self, with_col_aliases=False):
326326
pipeline.extend(query.get_pipeline())
327327
# Remove the added subqueries.
328328
self.subqueries = []
329-
self.having_match_mql = having
329+
pipeline.append({"$match": having})
330330
self.aggregation_pipeline = pipeline
331331
self.annotations = {
332332
target: expr.replace_expressions(all_replacements)

django_mongodb_backend/query.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ def __init__(self, compiler):
5050
self.lookup_pipeline = None
5151
self.project_fields = None
5252
self.aggregation_pipeline = compiler.aggregation_pipeline
53-
self.having = compiler.having_match_mql
5453
self.search_pipeline = compiler.search_pipeline
5554
self.extra_fields = None
5655
self.combinator_pipeline = None
@@ -96,8 +95,6 @@ def get_pipeline(self):
9695
if self.wrap_for_global_aggregation:
9796
# Add an empty extra document to handle default values on empty results
9897
pipeline.append({"$unionWith": {"pipeline": [{"$documents": [{}]}]}})
99-
if self.having:
100-
pipeline.append({"$match": self.having})
10198
if self.project_fields:
10299
pipeline.append({"$project": self.project_fields})
103100
if self.combinator_pipeline:

0 commit comments

Comments
 (0)