Chapter 3: Index Operations Lab 3.1: Explain Output

Queries:

var exp = db.restaurants.explain(“executionStats”)
exp.find({ “address.state”: “NY”, stars: { $gt: 3, $lt: 4 } }).sort({ name: 1 }).hint(REDACTED)

Correct Answer, the index for that query using hint():

The output using.explain():

{
“queryPlanner”: {
“plannerVersion”: 1,
“namespace”: “m201.restaurants”,
“indexFilterSet”: false,
“parsedQuery”: “REDACTED”,
“winningPlan”: {
“stage”: “SORT”,
“sortPattern”: {
“name”: 1
},
“inputStage”: {
“stage”: “SORT_KEY_GENERATOR”,
“inputStage”: {
“stage”: “FETCH”,
“inputStage”: {
“stage”: “IXSCAN”,
“keyPattern”: “REDACTED”,
“indexName”: “REDACTED”,
“isMultiKey”: false,
“isUnique”: false,
“isSparse”: false,
“isPartial”: false,
“indexVersion”: 1,
“direction”: “forward”,
“indexBounds”: “REDACTED”
}
}
}
},
“rejectedPlans”:
},
“executionStats”: {
“executionSuccess”: true,
“nReturned”: 3335,
“executionTimeMillis”: 20,
“totalKeysExamined”: 3335,
“totalDocsExamined”: 3335,
“executionStages”: “REDACTED”
},
“serverInfo”: “REDACTED”,
“ok”: 1
}

My question, If this is a correct answer As was shown in videos, this is the correct use of prefixes and the output should not be sorted in memory, but it shows that it was sorted in memory.

Please, someone explain why this is not the case.

Hi @yar,

I have edited your post to remove the lab answers.

For detailed discussion regarding this, please check your discourse inbox and feel free to reply there.

Thanks,
Sonali

Please is there any explanation to this concern? Because it’s also giving me a serious headache. What I understand from what I’ve learnt so far is that, when index prefix is correctly used, there won’t be an in-memory sort, why is it different in this case? Why do we have in-memory sort when the correct answer depicts correct use of prefix?