Skip to content

Commit e44d236

Browse files
committed
Address review
1 parent f1b0878 commit e44d236

File tree

1 file changed

+51
-15
lines changed

1 file changed

+51
-15
lines changed

django_mongodb_backend/query_conversion/expression_converters.py

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,22 @@ def is_simple_value(cls, value):
1818
"""
1919
if isinstance(value, str) and value.startswith("$"):
2020
return False
21-
if isinstance(value, list | tuple | set):
21+
if isinstance(value, (list, tuple, set)): # noqa: UP038
2222
return all(cls.is_simple_value(v) for v in value)
2323
# TODO: Expand functionality to support `$getField` conversion
2424
return not isinstance(value, dict) or value is None
2525

26-
@classmethod
27-
def is_convertable_field_name(cls, field_name):
28-
"""Validate a field_name is one that can be represented in $match"""
29-
# This needs work and re-evaluation
30-
return (
31-
isinstance(field_name, str)
32-
and field_name.startswith("$")
33-
and not field_name[:1].isalnum()
34-
)
35-
3626

3727
class _EqExpressionConverter(_BaseExpressionConverter):
38-
"""Convert $eq operation to a $match compatible query."""
28+
"""Convert $eq operation to a $match compatible query.
29+
30+
For example::
31+
"$expr": {
32+
{"$eq": ["$status", "active"]}
33+
}
34+
is converted to::
35+
{"status": "active"}
36+
"""
3937

4038
@classmethod
4139
def convert(cls, eq_args):
@@ -55,7 +53,15 @@ def convert(cls, eq_args):
5553

5654

5755
class _InExpressionConverter(_BaseExpressionConverter):
58-
"""Convert $in operation to a $match compatible query."""
56+
"""Convert $in operation to a $match compatible query.
57+
58+
For example::
59+
"$expr": {
60+
{"$in": ["$category", ["electronics", "books"]]}
61+
}
62+
is converted to::
63+
{"category": {"$in": ["electronics", "books"]}}
64+
"""
5965

6066
@classmethod
6167
def convert(cls, in_args):
@@ -93,13 +99,43 @@ def convert(cls, combined_conditions):
9399

94100

95101
class _OrExpressionConverter(_LogicalExpressionConverter):
96-
"""Convert $or operation to a $match compatible query."""
102+
"""Convert $or operation to a $match compatible query.
103+
104+
For example::
105+
"$expr": {
106+
"$or": [
107+
{"$eq": ["$status", "active"]},
108+
{"$in": ["$category", ["electronics", "books"]]},
109+
]
110+
}
111+
is converted to::
112+
"$or": [
113+
{"status": "active"},
114+
{"category": {"$in": ["electronics", "books"]}},
115+
]
116+
"""
97117

98118
_logical_op = "$or"
99119

100120

101121
class _AndExpressionConverter(_LogicalExpressionConverter):
102-
"""Convert $and operation to a $match compatible query."""
122+
"""Convert $and operation to a $match compatible query.
123+
124+
For example::
125+
"$expr": {
126+
"$and": [
127+
{"$eq": ["$status", "active"]},
128+
{"$in": ["$category", ["electronics", "books"]]},
129+
{"$eq": ["$verified", True]},
130+
]
131+
}
132+
is converted to::
133+
"$and": [
134+
{"status": "active"},
135+
{"category": {"$in": ["electronics", "books"]}},
136+
{"verified": True},
137+
]
138+
"""
103139

104140
_logical_op = "$and"
105141

0 commit comments

Comments
 (0)