QueryPlannerAnalysis::tryPushdownProjectBeneathSort checks whether a projection preserves all sort fields before pushing it below a sort. However, meta sort components (e.g. {$meta: "randVal"}, {}{$meta: "geoNearDistance"}{}) don't read document fields, the field name is a syntactic placeholder, so the projection check is irrelevant and the optimisation is always safe. As a result, queries like db.coll.find({}, {_id: 1}).sort({p: {$meta: "randVal")}} produce PROJECTION + SORT + COLLSCAN instead of the optimal SORT + PROJECTION + COLLSCAN. The fix is to skip any non-numeric sort component in the legality check. No correctness impact, both plan shapes return the same results. A QueryPlannerTest unit test and JS integration test using explain() should be added to guard the regression.
- is related to
-
SERVER-119464 Aggregation pipeline with $match: { $text }, $sort, $project gives an error
-
- In Code Review
-