Skip to content

Commit 349dc1d

Browse files
committed
handle str value as $literal
1 parent 0e0cdb0 commit 349dc1d

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

django_mongodb_backend/expressions/builtins.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,9 @@ def when(self, compiler, connection):
211211

212212
def value(self, compiler, connection, as_expr=False): # noqa: ARG001
213213
value = self.value
214-
if isinstance(value, (list, int)) and as_expr:
215-
# Wrap lists & numbers in $literal to prevent ambiguity when Value
216-
# appears in $project.
214+
if isinstance(value, (list, int, str)) and as_expr:
215+
# Wrap lists, numbers, and strings in $literal to avoid ambiguity when Value
216+
# is used in queries' aggregate or update_many's $set.
217217
return {"$literal": value}
218218
if isinstance(value, Decimal):
219219
return Decimal128(value)

tests/model_fields_/test_embedded_model_array.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,18 @@ def test_nested_array_index_expr(self):
327327
},
328328
{
329329
"$concat": [
330-
{"$ifNull": ["Z", ""]},
331-
{"$ifNull": ["acarias", ""]},
330+
{
331+
"$ifNull": [
332+
{"$literal": "Z"},
333+
{"$literal": ""},
334+
]
335+
},
336+
{
337+
"$ifNull": [
338+
{"$literal": "acarias"},
339+
{"$literal": ""},
340+
]
341+
},
332342
]
333343
},
334344
]

0 commit comments

Comments
 (0)