7
7
8
8
from .query_utils import process_lhs
9
9
10
+ # Aggregates whose MongoDB aggregation name differ from Aggregate.function.lower().
10
11
MONGO_AGGREGATIONS = {Count : "sum" }
11
12
12
13
@@ -35,9 +36,9 @@ def aggregate(
35
36
36
37
def count (self , compiler , connection , resolve_inner_expression = False , ** extra_context ): # noqa: ARG001
37
38
"""
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.
41
42
"""
42
43
if not self .distinct or resolve_inner_expression :
43
44
if self .filter :
@@ -61,10 +62,10 @@ def count(self, compiler, connection, resolve_inner_expression=False, **extra_co
61
62
if resolve_inner_expression :
62
63
return inner_expression
63
64
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.
66
67
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 .
68
69
exits_null = {"$cond" : {"if" : {"$in" : [{"$literal" : None }, lhs_mql ]}, "then" : - 1 , "else" : 0 }}
69
70
return {"$add" : [{"$size" : lhs_mql }, exits_null ]}
70
71
0 commit comments