Skip to content

Commit 1b0e2db

Browse files
committed
doc edits
1 parent dd6de36 commit 1b0e2db

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

django_mongodb/aggregates.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from .query_utils import process_lhs
99

10+
# Aggregates whose MongoDB aggregation name differ from Aggregate.function.lower().
1011
MONGO_AGGREGATIONS = {Count: "sum"}
1112

1213

@@ -35,9 +36,9 @@ def aggregate(
3536

3637
def count(self, compiler, connection, resolve_inner_expression=False, **extra_context): # noqa: ARG001
3738
"""
38-
When resolve_inner_expression is True, return the argument as MQL that
39-
resolves as a value. This is used to count different elements, so the inner
40-
values are returned to be pushed into a set.
39+
When resolve_inner_expression=True, return the MQL that resolves as a
40+
value. This is used to count different elements, so the inner values are
41+
returned to be pushed into a set.
4142
"""
4243
if not self.distinct or resolve_inner_expression:
4344
if self.filter:
@@ -61,10 +62,10 @@ def count(self, compiler, connection, resolve_inner_expression=False, **extra_co
6162
if resolve_inner_expression:
6263
return inner_expression
6364
return {"$sum": inner_expression}
64-
# If distinct=True or resolve_inner_expression=False, sum the size
65-
# of the set.
65+
# If distinct=True or resolve_inner_expression=False, sum the size of the
66+
# set.
6667
lhs_mql = process_lhs(self, compiler, connection)
67-
# Subtract 1 if None is in the set (it shouldn't have been counted).
68+
# None shouldn't be counted, so subtract 1 if it's present.
6869
exits_null = {"$cond": {"if": {"$in": [{"$literal": None}, lhs_mql]}, "then": -1, "else": 0}}
6970
return {"$add": [{"$size": lhs_mql}, exits_null]}
7071

django_mongodb/compiler.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,19 @@ def __init__(self, *args, **kwargs):
2727
self.aggregation_pipeline = None
2828

2929
def _get_group_alias_column(self, expr, annotation_group_idx):
30-
"""
31-
Generate a dummy field for use in ids fields in $group.
32-
33-
If the column is a composite expression (not a single column),
34-
it creates an auxiliary field and pass it through the flow.
35-
When a foreign field is needed in the ids, escape the dot (.) using GROUP_SEPARATOR.
36-
"""
30+
"""Generate a dummy field for use in the ids fields in $group."""
3731
replacement = None
3832
if isinstance(expr, Col):
3933
col = expr
4034
else:
35+
# If the column is a composite expression, creates a field for it.
4136
alias = f"__annotation_group{next(annotation_group_idx)}"
4237
col = self._get_column_from_expression(expr, alias)
4338
replacement = col
4439
if self.collection_name == col.alias:
4540
return col.target.column, replacement
41+
# If this is a foreign field, replace the normal dot (.) with
42+
# GROUP_SEPARATOR since FieldPath field names may not contain '.'.
4643
return f"{col.alias}{self.GROUP_SEPARATOR}{col.target.column}", replacement
4744

4845
def _get_column_from_expression(self, expr, alias):

0 commit comments

Comments
 (0)