@@ -69,12 +69,14 @@ def _get_replace_expr(self, sub_expr, group, alias):
6969 if getattr (sub_expr , "distinct" , False ):
7070 # If the expression should return distinct values, use $addToSet to
7171 # deduplicate.
72- rhs = sub_expr .as_mql (self , self .connection , resolve_inner_expression = True )
72+ rhs = sub_expr .as_mql (
73+ self , self .connection , resolve_inner_expression = True , as_expr = True
74+ )
7375 group [alias ] = {"$addToSet" : rhs }
7476 replacing_expr = sub_expr .copy ()
7577 replacing_expr .set_source_expressions ([inner_column , None ])
7678 else :
77- group [alias ] = sub_expr .as_mql (self , self .connection )
79+ group [alias ] = sub_expr .as_mql (self , self .connection , as_expr = True )
7880 replacing_expr = inner_column
7981 # Count must return 0 rather than null.
8082 if isinstance (sub_expr , Count ):
@@ -302,9 +304,7 @@ def _compound_searches_queries(self, search_replacements):
302304 search .as_mql (self , self .connection ),
303305 {
304306 "$addFields" : {
305- result_col .as_mql (self , self .connection , as_path = True ): {
306- "$meta" : score_function
307- }
307+ result_col .as_mql (self , self .connection ): {"$meta" : score_function }
308308 }
309309 },
310310 ]
@@ -334,7 +334,7 @@ def pre_sql_setup(self, with_col_aliases=False):
334334 pipeline .extend (query .get_pipeline ())
335335 # Remove the added subqueries.
336336 self .subqueries = []
337- pipeline .append ({"$match" : { "$expr" : having } })
337+ pipeline .append ({"$match" : having })
338338 self .aggregation_pipeline = pipeline
339339 self .annotations = {
340340 target : expr .replace_expressions (all_replacements )
@@ -481,11 +481,11 @@ def build_query(self, columns=None):
481481 query .lookup_pipeline = self .get_lookup_pipeline ()
482482 where = self .get_where ()
483483 try :
484- expr = where .as_mql (self , self .connection ) if where else {}
484+ match_mql = where .as_mql (self , self .connection ) if where else {}
485485 except FullResultSet :
486486 query .match_mql = {}
487487 else :
488- query .match_mql = { "$expr" : expr }
488+ query .match_mql = match_mql
489489 if extra_fields :
490490 query .extra_fields = self .get_project_fields (extra_fields , force_expression = True )
491491 query .subqueries = self .subqueries
@@ -646,7 +646,9 @@ def get_combinator_queries(self):
646646 for alias , expr in self .columns :
647647 # Unfold foreign fields.
648648 if isinstance (expr , Col ) and expr .alias != self .collection_name :
649- ids [expr .alias ][expr .target .column ] = expr .as_mql (self , self .connection )
649+ ids [expr .alias ][expr .target .column ] = expr .as_mql (
650+ self , self .connection , as_expr = True
651+ )
650652 else :
651653 ids [alias ] = f"${ alias } "
652654 # Convert defaultdict to dict so it doesn't appear as
@@ -710,16 +712,16 @@ def get_project_fields(self, columns=None, ordering=None, force_expression=False
710712 # For brevity/simplicity, project {"field_name": 1}
711713 # instead of {"field_name": "$field_name"}.
712714 if isinstance (expr , Col ) and name == expr .target .column and not force_expression
713- else expr .as_mql (self , self .connection )
715+ else expr .as_mql (self , self .connection , as_expr = True )
714716 )
715717 except EmptyResultSet :
716718 empty_result_set_value = getattr (expr , "empty_result_set_value" , NotImplemented )
717719 value = (
718720 False if empty_result_set_value is NotImplemented else empty_result_set_value
719721 )
720- fields [collection ][name ] = Value (value ).as_mql (self , self .connection )
722+ fields [collection ][name ] = Value (value ).as_mql (self , self .connection , as_expr = True )
721723 except FullResultSet :
722- fields [collection ][name ] = Value (True ).as_mql (self , self .connection )
724+ fields [collection ][name ] = Value (True ).as_mql (self , self .connection , as_expr = True )
723725 # Annotations (stored in None) and the main collection's fields
724726 # should appear in the top-level of the fields dict.
725727 fields .update (fields .pop (None , {}))
@@ -742,10 +744,10 @@ def _get_ordering(self):
742744 idx = itertools .count (start = 1 )
743745 for order in self .order_by_objs or []:
744746 if isinstance (order .expression , Col ):
745- field_name = order .as_mql (self , self .connection ).removeprefix ("$" )
747+ field_name = order .as_mql (self , self .connection , as_expr = True ).removeprefix ("$" )
746748 fields .append ((order .expression .target .column , order .expression ))
747749 elif isinstance (order .expression , Ref ):
748- field_name = order .as_mql (self , self .connection ).removeprefix ("$" )
750+ field_name = order .as_mql (self , self .connection , as_expr = True ).removeprefix ("$" )
749751 else :
750752 field_name = f"__order{ next (idx )} "
751753 fields .append ((field_name , order .expression ))
@@ -882,7 +884,7 @@ def execute_sql(self, result_type):
882884 )
883885 prepared = field .get_db_prep_save (value , connection = self .connection )
884886 if hasattr (value , "as_mql" ):
885- prepared = prepared .as_mql (self , self .connection )
887+ prepared = prepared .as_mql (self , self .connection , as_expr = True )
886888 values [field .column ] = prepared
887889 try :
888890 criteria = self .build_query ().match_mql
0 commit comments