@@ -42,8 +42,8 @@ def __init__(self, compiler, columns):
42
42
self .columns = columns
43
43
self ._negated = False
44
44
self .ordering = []
45
- self .collection_name = self .compiler .collection_name
46
45
self .collection = self .compiler .get_collection ()
46
+ self .collection_name = self .compiler .collection_name
47
47
self .mongo_query = getattr (compiler .query , "raw_query" , {})
48
48
self .lookup_pipeline = None
49
49
@@ -137,6 +137,7 @@ def join(self, compiler, connection):
137
137
join_fields = self .join_fields or self .join_cols
138
138
lhs_fields = []
139
139
rhs_fields = []
140
+ # Add a join condition for each pair of joining fields.
140
141
for lhs , rhs in join_fields :
141
142
if isinstance (lhs , str ):
142
143
lhs_mql = lhs
@@ -159,16 +160,20 @@ def join(self, compiler, connection):
159
160
"$lookup" : {
160
161
# The right-hand table to join.
161
162
"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.
164
165
"let" : {
165
166
f"{ parent_template } { i } " : parent_field
166
167
for i , parent_field in enumerate (lhs_fields )
167
168
},
168
169
"pipeline" : [
169
170
{
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
+ # ...
172
177
"$match" : {
173
178
"$expr" : {
174
179
"$and" : [
@@ -184,13 +189,11 @@ def join(self, compiler, connection):
184
189
}
185
190
},
186
191
]
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.
194
197
if self .join_type != INNER :
195
198
lookup_pipeline .append (
196
199
{
0 commit comments