@@ -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 
@@ -643,7 +643,9 @@ def get_combinator_queries(self):
643643 for  alias , expr  in  self .columns :
644644 # Unfold foreign fields. 
645645 if  isinstance (expr , Col ) and  expr .alias  !=  self .collection_name :
646-  ids [expr .alias ][expr .target .column ] =  expr .as_mql (self , self .connection )
646+  ids [expr .alias ][expr .target .column ] =  expr .as_mql (
647+  self , self .connection , as_expr = True 
648+  )
647649 else :
648650 ids [alias ] =  f"${ alias }  
649651 # Convert defaultdict to dict so it doesn't appear as 
@@ -707,16 +709,16 @@ def get_project_fields(self, columns=None, ordering=None, force_expression=False
707709 # For brevity/simplicity, project {"field_name": 1} 
708710 # instead of {"field_name": "$field_name"}. 
709711 if  isinstance (expr , Col ) and  name  ==  expr .target .column  and  not  force_expression 
710-  else  expr .as_mql (self , self .connection )
712+  else  expr .as_mql (self , self .connection ,  as_expr = True )
711713 )
712714 except  EmptyResultSet :
713715 empty_result_set_value  =  getattr (expr , "empty_result_set_value" , NotImplemented )
714716 value  =  (
715717 False  if  empty_result_set_value  is  NotImplemented  else  empty_result_set_value 
716718 )
717-  fields [collection ][name ] =  Value (value ).as_mql (self , self .connection )
719+  fields [collection ][name ] =  Value (value ).as_mql (self , self .connection ,  as_expr = True )
718720 except  FullResultSet :
719-  fields [collection ][name ] =  Value (True ).as_mql (self , self .connection )
721+  fields [collection ][name ] =  Value (True ).as_mql (self , self .connection ,  as_expr = True )
720722 # Annotations (stored in None) and the main collection's fields 
721723 # should appear in the top-level of the fields dict. 
722724 fields .update (fields .pop (None , {}))
@@ -739,10 +741,10 @@ def _get_ordering(self):
739741 idx  =  itertools .count (start = 1 )
740742 for  order  in  self .order_by_objs  or  []:
741743 if  isinstance (order .expression , Col ):
742-  field_name  =  order .as_mql (self , self .connection ).removeprefix ("$" )
744+  field_name  =  order .as_mql (self , self .connection ,  as_expr = True ).removeprefix ("$" )
743745 fields .append ((order .expression .target .column , order .expression ))
744746 elif  isinstance (order .expression , Ref ):
745-  field_name  =  order .as_mql (self , self .connection ).removeprefix ("$" )
747+  field_name  =  order .as_mql (self , self .connection ,  as_expr = True ).removeprefix ("$" )
746748 else :
747749 field_name  =  f"__order{ next (idx )}  
748750 fields .append ((field_name , order .expression ))
@@ -879,7 +881,7 @@ def execute_sql(self, result_type):
879881 )
880882 prepared  =  field .get_db_prep_save (value , connection = self .connection )
881883 if  hasattr (value , "as_mql" ):
882-  prepared  =  prepared .as_mql (self , self .connection )
884+  prepared  =  prepared .as_mql (self , self .connection ,  as_expr = True )
883885 values [field .column ] =  prepared 
884886 try :
885887 criteria  =  self .build_query ().match_mql 
0 commit comments