-
Type:
Improvement
-
Resolution: Unresolved
-
Priority:
Unknown
-
None
-
Affects Version/s: None
-
Component/s: None
-
None
-
None
-
Python Drivers
-
None
-
None
-
None
-
None
-
None
-
None
Context
Currently, $lookup stages are always defined using the let and pipeline form, even in simple join cases. When the join condition matches a single field comparison, we can simplify the stage by using the localField and foreignField form instead.
This refactor aims to improve readability and potentially optimize query execution in MongoDB by letting the engine handle the simpler $lookup variant directly.
Definition of done
- $lookup stages using a single equality condition are refactored to use localField and foreignField.
- Complex joins (those requiring multiple conditions or expressions) continue using let and pipeline.
Pitfalls
- Some $lookup conditions may appear simple but depend on expressions or type conversions that prevent safe use of localField/foreignField. These cases must remain in let/pipeline form.
- Be careful when fields involve embedded paths or aliases.
- Ensure backward compatibility with existing $lookup behavior, especially in nested or subquery lookups.