Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-43816

Push $sort with $meta in the sort pattern into the PlanStage layer



    • Type: Improvement
    • Status: Open
    • Priority: Major - P3
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: Backlog
    • Labels:
    • Sprint:
      Query 2019-12-16


      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.


          Issue Links



              • Assignee:
                david.storch David Storch
                david.storch David Storch
              • Votes:
                0 Vote for this issue
                2 Start watching this issue


                • Created: