-
Type: Task
-
Resolution: Fixed
-
Priority: Major - P3
-
Affects Version/s: None
-
Labels:None
Description
I left a detailed comment on the ticket describing the behavior changes made under this ticket:
The documentation for $meta will have to be updated to reflect these new behaviors: https://docs.mongodb.com/manual/reference/operator/projection/meta/ and https://docs.mongodb.com/manual/reference/method/cursor.sort/#metadata-sort.
We will also have to update the documentation for $natural: https://docs.mongodb.com/manual/reference/method/cursor.sort/#return-in-natural-order and https://docs.mongodb.com/manual/reference/operator/meta/natural/#metaOp._S_natural. In particular, we need to remove the note saying that $natural sort cannot be used against a view.
Description of Linked Ticket
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:
> db.c.drop() > db.c.createIndex({f: "text"}) { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "commitQuorum" : 1, "ok" : 1 } > db.c.aggregate([{$match: {$text: {$search: "foo"}}}, {$sort: {f: {$meta: "textScore"}}}]) // Success!
The find subsystem, on the other hand, requires a matching "textScore" $meta projection:
> db.c.find({$text: {$search: "foo"}}, {}).sort({f: {$meta: "textScore"}}) Error: error: { "ok" : 0, "errmsg" : "must have $meta projection for all $meta sort keys", "code" : 2, "codeName" : "BadValue" } > db.c.find({$text: {$search: "foo"}}, {f: {$meta: "textScore"}}).sort({f: {$meta: "textScore"}}) // Success!
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.
Scope of changes
Impact to Other Docs
MVP (Work and Date)
Resources (Scope or Design Docs, Invision, etc.)
- documents
-
SERVER-43816 Push $text and $sort with $meta in the sort pattern into the PlanStage layer
- Closed