@@ -230,9 +230,9 @@ def formfield(self, **kwargs):
230
230
231
231
232
232
class Array (Func ):
233
- def as_mql (self , compiler , connection , as_path = False ):
233
+ def as_mql_expr (self , compiler , connection ):
234
234
return [
235
- expr .as_mql (compiler , connection , as_path = as_path )
235
+ expr .as_mql (compiler , connection , as_path = False )
236
236
for expr in self .get_source_expressions ()
237
237
]
238
238
@@ -254,24 +254,16 @@ def __init__(self, lhs, rhs):
254
254
class ArrayContains (ArrayRHSMixin , FieldGetDbPrepValueMixin , Lookup ):
255
255
lookup_name = "contains"
256
256
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 ):
264
265
lhs_mql = process_lhs (self , compiler , connection , as_path = False )
265
266
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 }
275
267
return {
276
268
"$and" : [
277
269
{"$ne" : [lhs_mql , None ]},
@@ -285,19 +277,16 @@ def as_mql(self, compiler, connection, as_path=False):
285
277
class ArrayContainedBy (ArrayRHSMixin , FieldGetDbPrepValueMixin , Lookup ):
286
278
lookup_name = "contained_by"
287
279
288
- def as_mql (self , compiler , connection , as_path = False ):
280
+ def as_mql_expr (self , compiler , connection ):
289
281
lhs_mql = process_lhs (self , compiler , connection , as_path = False )
290
282
value = process_rhs (self , compiler , connection , as_path = False )
291
- expr = {
283
+ return {
292
284
"$and" : [
293
285
{"$ne" : [lhs_mql , None ]},
294
286
{"$ne" : [value , None ]},
295
287
{"$setIsSubset" : [lhs_mql , value ]},
296
288
]
297
289
}
298
- if as_path :
299
- return {"$expr" : expr }
300
- return expr
301
290
302
291
303
292
@ArrayField .register_lookup
@@ -344,36 +333,30 @@ def get_subquery_wrapping_pipeline(self, compiler, connection, field_name, expr)
344
333
},
345
334
]
346
335
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 }}
352
340
341
+ def as_mql_expr (self , compiler , connection ):
353
342
lhs_mql = process_lhs (self , compiler , connection , as_path = False )
354
343
value = process_rhs (self , compiler , connection , as_path = False )
355
- expr = {
344
+ return {
356
345
"$and" : [
357
346
{"$ne" : [lhs_mql , None ]},
358
347
{"$size" : {"$setIntersection" : [value , lhs_mql ]}},
359
348
]
360
349
}
361
- if as_path :
362
- return {"$expr" : expr }
363
- return expr
364
350
365
351
366
352
@ArrayField .register_lookup
367
353
class ArrayLenTransform (Transform ):
368
354
lookup_name = "len"
369
355
output_field = IntegerField ()
370
356
371
- def as_mql (self , compiler , connection , as_path = False ):
357
+ def as_mql_expr (self , compiler , connection , as_path = False ):
372
358
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 }}
377
360
378
361
379
362
@ArrayField .register_lookup
@@ -398,15 +381,20 @@ def __init__(self, index, base_field, *args, **kwargs):
398
381
self .index = index
399
382
self .base_field = base_field
400
383
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 ):
405
396
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 ]}
410
398
411
399
@property
412
400
def output_field (self ):
@@ -428,7 +416,7 @@ def __init__(self, start, end, *args, **kwargs):
428
416
self .start = start
429
417
self .end = end
430
418
431
- def as_mql (self , compiler , connection , as_path = False ):
419
+ def as_mql_expr (self , compiler , connection ):
432
420
lhs_mql = process_lhs (self , compiler , connection )
433
421
return {"$slice" : [lhs_mql , self .start , self .end ]}
434
422
0 commit comments