'How does it work when executing the $regex query in Mongodb?

I have a question about Mongodb Index.

As far as I know, when the query is executed, it is found in the data sorted by the selected Index

db.HASH_TAG.find({"hashtag" : { $regex : "^Test"}}).sort({htseq:1}).hint("hashtag_1_htseq_1").explain("executionStats");

========================= explain===============================

executionStages  :{
  "stage" : "SORT", 
  "inputStage" : {
     "stage" : "SORT_KEY_GENERATOR",
     "inputStage" : {
             "stage" : "FETCH",
              "inputStage" : {
                        "stage" : "IXSCAN",
                        "indexName" : "hashtag_1_htseq_1", 
                         "keyPattern" : {
                            "hashtag" : 1.0, 
                            "htseq" : 1.0
                        }, 
                  }
          }
     }
}

Please explain why SORT_KEY_GENERATOR occurs in the input stage.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source