-
Type: Improvement
-
Resolution: Duplicate
-
Priority: Major - P3
-
None
-
Affects Version/s: None
-
Component/s: Aggregation Framework
-
Fully Compatible
-
Query 10 (02/22/16), Query 11 (03/14/16), Query 12 (04/04/16)
An aggregation pipeline using $match should be optimized so that the $match is moved as close as possible to the front of the pipeline, reducing the number of documents passing through each subsequent stage.
Work is planned for the following optimizations, and is tracked in these tickets:
- $match and $unwind:
SERVER-20506 - $match and $lookup:
SERVER-21612 - $match and $geoNear: SERVER-23466
- $match and $project:
SERVER-19153
Old Description
This is an optimization we can apply in the future.
Applying matches before other operations allows less work to be done in the pipeline because documents will have been filtered out.
Matches can't be pushed earlier than just anything. For example, a $match can't be pushed ahead of a $skip or $limit, because it will change the results. On the other hand, a $match can be pushed ahead of a $project when it doesn't depend on a computed value in the project – field renames don't alter anything except the references in the $match.
Being able to push matches earlier will allow us to use indexes for the underlying query.
- duplicates
-
SERVER-19153 Conditionally push $match before $project
- Closed
-
SERVER-23466 $match should be moved into the query field of the $geoNear stage, where possible
- Backlog
-
SERVER-20506 Conditionally order $match with $unwind
- Closed
-
SERVER-21612 Combine post $lookup $match on looked up field
- Closed
- is related to
-
SERVER-447 new aggregation framework
- Closed
-
SERVER-20506 Conditionally order $match with $unwind
- Closed
- related to
-
SERVER-12376 $skip and $limit should be moved before $project
- Closed
-
SERVER-20506 Conditionally order $match with $unwind
- Closed