Skip to content

Commit b75351c

Browse files
committed
edits
1 parent f5d6107 commit b75351c

File tree

4 files changed

+20
-15
lines changed

4 files changed

+20
-15
lines changed

django_mongodb/compiler.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,9 @@ def _make_result(self, entity, columns, converters, tuple_expected=False):
8585
field = col.field
8686
column_alias = getattr(col, "alias", None)
8787
obj = (
88+
# Use the related object...
8889
entity.get(column_alias, {})
89-
# Does this column refer to an object for select_related()?
90+
# ...if this column refers to an object for select_related().
9091
if column_alias is not None and column_alias != self.collection_name
9192
else entity
9293
)

django_mongodb/expressions.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ def case(self, compiler, connection):
4343

4444

4545
def col(self, compiler, connection): # noqa: ARG001
46+
# Add the column's collection's alias for columns in joined collections.
4647
prefix = f"{self.alias}." if self.alias != compiler.collection_name else ""
4748
return f"${prefix}{self.target.column}"
4849

django_mongodb/features.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class DatabaseFeatures(BaseDatabaseFeatures):
7878
"model_fields.test_jsonfield.TestQuerying.test_order_grouping_custom_decoder",
7979
"model_fields.test_jsonfield.TestQuerying.test_ordering_by_transform",
8080
"model_fields.test_jsonfield.TestQuerying.test_ordering_grouping_by_key_transform",
81-
# DecimalField lookup with F expressoin crashes:
81+
# DecimalField lookup with F expression crashes:
8282
# decimal.InvalidOperation: [<class 'decimal.ConversionSyntax'>]
8383
"lookup.tests.LookupTests.test_lookup_rhs",
8484
# Wrong results in queries with multiple tables.
@@ -228,8 +228,8 @@ def django_test_expected_failures(self):
228228
"expressions_case.tests.CaseExpressionTests.test_filter_with_aggregation_in_value",
229229
"expressions_case.tests.CaseExpressionTests.test_m2m_exclude",
230230
"expressions_case.tests.CaseExpressionTests.test_m2m_reuse",
231-
"lookup.tests.LookupQueryingTests.test_aggregate_combined_lookup",
232231
"lookup.test_decimalfield.DecimalFieldLookupTests",
232+
"lookup.tests.LookupQueryingTests.test_aggregate_combined_lookup",
233233
"from_db_value.tests.FromDBValueTest.test_aggregation",
234234
"timezones.tests.LegacyDatabaseTests.test_query_aggregation",
235235
"timezones.tests.LegacyDatabaseTests.test_query_annotation",

django_mongodb/query.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ def __init__(self, compiler, columns):
4242
self.columns = columns
4343
self._negated = False
4444
self.ordering = []
45-
self.collection_name = self.compiler.collection_name
4645
self.collection = self.compiler.get_collection()
46+
self.collection_name = self.compiler.collection_name
4747
self.mongo_query = getattr(compiler.query, "raw_query", {})
4848
self.lookup_pipeline = None
4949

@@ -137,6 +137,7 @@ def join(self, compiler, connection):
137137
join_fields = self.join_fields or self.join_cols
138138
lhs_fields = []
139139
rhs_fields = []
140+
# Add a join condition for each pair of joining fields.
140141
for lhs, rhs in join_fields:
141142
if isinstance(lhs, str):
142143
lhs_mql = lhs
@@ -159,16 +160,20 @@ def join(self, compiler, connection):
159160
"$lookup": {
160161
# The right-hand table to join.
161162
"from": self.table_name,
162-
# Define the current pipeline variables to be used in the match
163-
# expression of the pipeline.
163+
# The pipeline variables to be matched in the pipeline's
164+
# expression.
164165
"let": {
165166
f"{parent_template}{i}": parent_field
166167
for i, parent_field in enumerate(lhs_fields)
167168
},
168169
"pipeline": [
169170
{
170-
# Match all the conditions: self.table_name.field1 = parent_table.field1
171-
# and self.table_name.field2 = parent_table.field2 and ...
171+
# Match the conditions:
172+
# self.table_name.field1 = parent_table.field1
173+
# AND
174+
# self.table_name.field2 = parent_table.field2
175+
# AND
176+
# ...
172177
"$match": {
173178
"$expr": {
174179
"$and": [
@@ -184,13 +189,11 @@ def join(self, compiler, connection):
184189
}
185190
},
186191
]
187-
188-
# To avoid missing data when using the unwind operator,
189-
# an empty collection should be added if the join is not an inner join.
190-
# For inner joins, rows with empty arrays are removed,
191-
# as the unwind operator unrolls or unnests the array and removes the row if it is empty.
192-
# This is the expected behavior for inner joins.
193-
# However, for left outer joins (LOUTER), an empty collection is returned instead.
192+
# To avoid missing data when using $unwind, an empty collection is added if
193+
# the join isn't an inner join. For inner joins, rows with empty arrays are
194+
# removed, as $unwind unrolls or unnests the array and removes the row if
195+
# it's empty. This is the expected behavior for inner joins. For left outer
196+
# joins (LOUTER), however, an empty collection is returned.
194197
if self.join_type != INNER:
195198
lookup_pipeline.append(
196199
{

0 commit comments

Comments
 (0)