@@ -230,9 +230,9 @@ def formfield(self, **kwargs):
230230
231231
232232class Array (Func ):
233- def as_mql (self , compiler , connection , as_path = False ):
233+ def as_mql_expr (self , compiler , connection ):
234234 return [
235- expr .as_mql (compiler , connection , as_path = as_path )
235+ expr .as_mql (compiler , connection , as_path = False )
236236 for expr in self .get_source_expressions ()
237237 ]
238238
@@ -254,24 +254,16 @@ def __init__(self, lhs, rhs):
254254class ArrayContains (ArrayRHSMixin , FieldGetDbPrepValueMixin , Lookup ):
255255 lookup_name = "contains"
256256
257- def as_mql (self , compiler , connection , as_path = False ):
258- if as_path and self .is_simple_expression ():
259- lhs_mql = process_lhs (self , compiler , connection , as_path = as_path )
260- value = process_rhs (self , compiler , connection , as_path = as_path )
261- if value is None :
262- return False
263- return {lhs_mql : {"$all" : value }}
257+ def as_mql_path (self , compiler , connection ):
258+ lhs_mql = process_lhs (self , compiler , connection , as_path = True )
259+ value = process_rhs (self , compiler , connection , as_path = True )
260+ if value is None :
261+ return False
262+ return {lhs_mql : {"$all" : value }}
263+
264+ def as_mql_expr (self , compiler , connection ):
264265 lhs_mql = process_lhs (self , compiler , connection , as_path = False )
265266 value = process_rhs (self , compiler , connection , as_path = False )
266- expr = {
267- "$and" : [
268- {"$ne" : [lhs_mql , None ]},
269- {"$ne" : [value , None ]},
270- {"$setIsSubset" : [value , lhs_mql ]},
271- ]
272- }
273- if as_path :
274- return {"$expr" : expr }
275267 return {
276268 "$and" : [
277269 {"$ne" : [lhs_mql , None ]},
@@ -285,19 +277,16 @@ def as_mql(self, compiler, connection, as_path=False):
285277class ArrayContainedBy (ArrayRHSMixin , FieldGetDbPrepValueMixin , Lookup ):
286278 lookup_name = "contained_by"
287279
288- def as_mql (self , compiler , connection , as_path = False ):
280+ def as_mql_expr (self , compiler , connection ):
289281 lhs_mql = process_lhs (self , compiler , connection , as_path = False )
290282 value = process_rhs (self , compiler , connection , as_path = False )
291- expr = {
283+ return {
292284 "$and" : [
293285 {"$ne" : [lhs_mql , None ]},
294286 {"$ne" : [value , None ]},
295287 {"$setIsSubset" : [lhs_mql , value ]},
296288 ]
297289 }
298- if as_path :
299- return {"$expr" : expr }
300- return expr
301290
302291
303292@ArrayField .register_lookup
@@ -344,36 +333,30 @@ def get_subquery_wrapping_pipeline(self, compiler, connection, field_name, expr)
344333 },
345334 ]
346335
347- def as_mql (self , compiler , connection , as_path = False ):
348- if as_path and self .is_simple_expression ():
349- lhs_mql = process_lhs (self , compiler , connection , as_path = True )
350- value = process_rhs (self , compiler , connection , as_path = True )
351- return {lhs_mql : {"$in" : value }}
336+ def as_mql_path (self , compiler , connection ):
337+ lhs_mql = process_lhs (self , compiler , connection , as_path = True )
338+ value = process_rhs (self , compiler , connection , as_path = True )
339+ return {lhs_mql : {"$in" : value }}
352340
341+ def as_mql_expr (self , compiler , connection ):
353342 lhs_mql = process_lhs (self , compiler , connection , as_path = False )
354343 value = process_rhs (self , compiler , connection , as_path = False )
355- expr = {
344+ return {
356345 "$and" : [
357346 {"$ne" : [lhs_mql , None ]},
358347 {"$size" : {"$setIntersection" : [value , lhs_mql ]}},
359348 ]
360349 }
361- if as_path :
362- return {"$expr" : expr }
363- return expr
364350
365351
366352@ArrayField .register_lookup
367353class ArrayLenTransform (Transform ):
368354 lookup_name = "len"
369355 output_field = IntegerField ()
370356
371- def as_mql (self , compiler , connection , as_path = False ):
357+ def as_mql_expr (self , compiler , connection , as_path = False ):
372358 lhs_mql = process_lhs (self , compiler , connection , as_path = False )
373- expr = {"$cond" : {"if" : {"$isArray" : lhs_mql }, "then" : {"$size" : lhs_mql }, "else" : None }}
374- if as_path :
375- return {"$expr" : expr }
376- return expr
359+ return {"$cond" : {"if" : {"$isArray" : lhs_mql }, "then" : {"$size" : lhs_mql }, "else" : None }}
377360
378361
379362@ArrayField .register_lookup
@@ -398,15 +381,20 @@ def __init__(self, index, base_field, *args, **kwargs):
398381 self .index = index
399382 self .base_field = base_field
400383
401- def as_mql (self , compiler , connection , as_path = False ):
402- if as_path and self .is_simple_column (self .lhs ):
403- lhs_mql = process_lhs (self , compiler , connection , as_path = as_path )
404- return f"{ lhs_mql } .{ self .index } "
384+ def is_simple_expression (self ):
385+ return self .is_simple_column
386+
387+ @property
388+ def is_simple_column (self ):
389+ return self .lhs .is_simple_column
390+
391+ def as_mql_path (self , compiler , connection ):
392+ lhs_mql = process_lhs (self , compiler , connection , as_path = True )
393+ return f"{ lhs_mql } .{ self .index } "
394+
395+ def as_mql_expr (self , compiler , connection ):
405396 lhs_mql = process_lhs (self , compiler , connection , as_path = False )
406- expr = {"$arrayElemAt" : [lhs_mql , self .index ]}
407- if as_path :
408- return {"$expr" : expr }
409- return expr
397+ return {"$arrayElemAt" : [lhs_mql , self .index ]}
410398
411399 @property
412400 def output_field (self ):
@@ -428,7 +416,7 @@ def __init__(self, start, end, *args, **kwargs):
428416 self .start = start
429417 self .end = end
430418
431- def as_mql (self , compiler , connection , as_path = False ):
419+ def as_mql_expr (self , compiler , connection ):
432420 lhs_mql = process_lhs (self , compiler , connection )
433421 return {"$slice" : [lhs_mql , self .start , self .end ]}
434422
0 commit comments