Skip to content

Commit a365829

Browse files
committed
implement array length
1 parent 5470fb5 commit a365829

File tree

3 files changed

+11
-15
lines changed

3 files changed

+11
-15
lines changed

django_mongodb/features.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,17 +94,17 @@ class DatabaseFeatures(BaseDatabaseFeatures):
9494
# int() argument must be a string, a bytes-like object or a real number, not 'list'
9595
"model_fields_.test_arrayfield.TestQuerying.test_index_annotation",
9696
# Wrong results
97-
"model_fields_.test_arrayfield.TestQuerying.test_in_as_F_object",
9897
"model_fields_.test_arrayfield.TestQuerying.test_index",
9998
"model_fields_.test_arrayfield.TestQuerying.test_index_chained",
10099
"model_fields_.test_arrayfield.TestQuerying.test_index_nested",
101-
"model_fields_.test_arrayfield.TestQuerying.test_len",
102-
"model_fields_.test_arrayfield.TestQuerying.test_len_empty_array",
103-
"model_fields_.test_arrayfield.TestQuerying.test_lt",
104100
"model_fields_.test_arrayfield.TestQuerying.test_order_by_slice",
105101
"model_fields_.test_arrayfield.TestQuerying.test_slice",
106102
"model_fields_.test_arrayfield.TestQuerying.test_slice_annotation",
107-
"model_fields_.test_arrayfield.TestQuerying.test_usage_in_subquery",
103+
# $lt treats null values as zero.
104+
"model_fields_.test_arrayfield.TestQuerying.test_lt",
105+
"model_fields_.test_arrayfield.TestQuerying.test_len",
106+
# None is $in None
107+
"model_fields_.test_arrayfield.TestQuerying.test_in_as_F_object",
108108
}
109109
# $bitAnd, #bitOr, and $bitXor are new in MongoDB 6.3.
110110
_django_test_expected_failures_bitwise = {

django_mongodb/fields/array.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
from django_mongodb.forms import SimpleArrayField
1111

12+
from ..query_utils import process_lhs
1213
from ..utils import prefix_validation_error
1314

1415
__all__ = ["ArrayField"]
@@ -277,16 +278,8 @@ class ArrayLenTransform(Transform):
277278
output_field = IntegerField()
278279

279280
def as_mql(self, compiler, connection):
280-
lhs, params = compiler.compile(self.lhs)
281-
# Distinguish NULL and empty arrays
282-
return (
283-
(
284-
"" # "CASE WHEN %(lhs)s IS NULL THEN NULL ELSE "
285-
# "coalesce(array_length(%(lhs)s, 1), 0) END"
286-
)
287-
% {},
288-
params * 2,
289-
)
281+
lhs_mql = process_lhs(self, compiler, connection)
282+
return {"$cond": {"if": {"$eq": [lhs_mql, None]}, "then": None, "else": {"$size": lhs_mql}}}
290283

291284

292285
@ArrayField.register_lookup

tests/model_fields_/models.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ class NullableIntegerArrayModel(models.Model):
5656
field_nested = ArrayField(ArrayField(models.IntegerField(null=True)), null=True)
5757
order = models.IntegerField(null=True)
5858

59+
def __str__(self):
60+
return str(self.field)
61+
5962

6063
class CharArrayModel(models.Model):
6164
field = ArrayField(models.CharField(max_length=10))

0 commit comments

Comments
 (0)