When a $sort stage is at the beginning of the pipeline, or close to the beginning, the aggregation subsystem attempts to execute it using the underlying PlanStage query execution tree. We are trying to push increasingly more execution into the PlanStage layer in order to move towards having a single, unified execution engine which will be easier to extend and maintain.
There is a special case where we don't push down $sort if it has "textScore" or "randVal" $meta expressions: see https://github.com/mongodb/mongo/blob/8ccaacce937ae51deef27c41e977aca0511fb338/src/mongo/db/pipeline/pipeline_d.cpp#L102-L120.
For "textScore", this restriction stems from the fact that the find and agg systems have different validity rules for $meta sort by text score. The agg subsystem permits $meta:textScore with no matching $meta projection:
The find subsystem, on the other hand, requires a matching "textScore" $meta projection:
We should eliminate this restriction, which would allow us to always push down "textScore" $meta sort.
The situation for "randVal" needs some investigation, but note
SERVER-43666. The ability to sort by "randVal" has existed internally for some time to support $sample, though I don't think we intend to support it as available to applications.