@@ -123,14 +123,14 @@ def extra_where(self, compiler, connection): # noqa: ARG001
123
123
raise NotSupportedError ("QuerySet.extra() is not supported on MongoDB." )
124
124
125
125
126
- def join (self , compiler , connection , pushed_expression = None ):
127
- """
128
- Generate a MongoDB $lookup stage for a join. Optionally accepts a
129
- pushed_expression, which is a filter expression involving fields from the
130
- joined collection, and can be pushed into the lookup pipeline for
131
- optimization.
126
+ def join (self , compiler , connection , pushed_filter_expression = None ):
132
127
"""
128
+ Generate a MongoDB $lookup stage for a join.
133
129
130
+ `pushed_filter_expression` is a Where expression involving fields from the
131
+ joined collection which can be pushed from the WHERE ($match) clause to the
132
+ JOIN ($lookup) clause to improve performance.
133
+ """
134
134
parent_template = "parent__field__"
135
135
136
136
def _get_reroot_replacements (expression ):
@@ -176,34 +176,32 @@ def _get_reroot_replacements(expression):
176
176
self .parent_alias , lhs , compiler .collection_name , rhs
177
177
)
178
178
lhs_fields .append (lhs .as_mql (compiler , connection ))
179
- # In the lookup stage, the reference to this column doesn't include
180
- # the collection name.
179
+ # In the lookup stage, the reference to this column doesn't include the
180
+ # collection name.
181
181
rhs_fields .append (rhs .as_mql (compiler , connection ))
182
182
# Handle any join conditions besides matching field pairs.
183
183
extra = self .join_field .get_extra_restriction (self .table_alias , self .parent_alias )
184
-
185
184
extra_conditions = []
186
185
if extra :
187
186
replacements = _get_reroot_replacements (extra )
188
187
extra_conditions .append (
189
188
extra .replace_expressions (replacements ).as_mql (compiler , connection )
190
189
)
191
-
192
- # pushed_expression is a filter expression from the outer WHERE clause
193
- # that involves fields from the joined (right-hand) table and possibly the
194
- # outer (left-hand) table.
195
- # If it can be safely evaluated within the $lookup pipeline
196
- # (e.g., field comparisons like right.status = left.id), it is
197
- # "pushed down" into the join's $match stage to reduce the volume of
198
- # joined documents. This only applies to inner joins, as pushing
199
- # filters into a left join can change the semantics of the result.
200
- # LEFT JOINs may rely on null checks to detect missing RHS.
201
- if pushed_expression and self .join_type == INNER :
202
- rerooted_replacement = _get_reroot_replacements (pushed_expression )
190
+ # pushed_filter_expression is a filter expression from the outer WHERE
191
+ # clause that involves fields from the joined (right-hand) table and
192
+ # possibly the outer (left-hand) table. If it can be safely evaluated
193
+ # within the $lookup pipeline (e.g., field comparisons like
194
+ # right.status = left.id), it is "pushed" into the join's $match stage to
195
+ # reduce the volume of joined documents. This only applies to INNER JOINs,
196
+ # as pushing filters into a LEFT JOIN can change the semantics of the
197
+ # result. LEFT JOINs may rely on null checks to detect missing RHS.
198
+ if pushed_filter_expression and self .join_type == INNER :
199
+ rerooted_replacement = _get_reroot_replacements (pushed_filter_expression )
203
200
extra_conditions .append (
204
- pushed_expression .replace_expressions (rerooted_replacement ).as_mql (compiler , connection )
201
+ pushed_filter_expression .replace_expressions (rerooted_replacement ).as_mql (
202
+ compiler , connection
203
+ )
205
204
)
206
-
207
205
lookup_pipeline = [
208
206
{
209
207
"$lookup" : {
0 commit comments