I get my first fail in all the developer path courses because I am getting IXSCAN from a query that is told otherwise.
db.people.find({ "first_name": { $gt: "J" } }).sort({ "address.city": -1 })
No, this query doesn’t use equality on the index prefix. When using an index for filtering and sorting the query must include equality conditions on all the prefix keys that precede the sort keys. Moreover, on the sort predicate it skipped the next key in the prefix “address.state”.
I have switched to mongoDB 5.0.4 and this is what this query’s explain returns.
{
"explainVersion" : "1",
"queryPlanner" : {
"namespace" : "m201.people",
"indexFilterSet" : false,
"parsedQuery" : {
"first_name" : {
"$gt" : "J"
}
},
"queryHash" : "602B7D73",
"planCacheKey" : "3694C554",
"maxIndexedOrSolutionsReached" : false,
"maxIndexedAndSolutionsReached" : false,
"maxScansToExplodeReached" : false,
"winningPlan" : {
"stage" : "FETCH",
"inputStage" : {
"stage" : "SORT",
"sortPattern" : {
"address.city" : -1
},
"memLimit" : 104857600,
"type" : "default",
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"first_name" : 1,
"address.street" : -1,
"address.city" : -1,
"ssn" : 1
},
"indexName" : "faas",
"isMultiKey" : false,
"multiKeyPaths" : {
"first_name" : [ ],
"address.street" : [ ],
"address.city" : [ ],
"ssn" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"first_name" : [
"(\"J\", {})"
],
"address.street" : [
"[MaxKey, MinKey]"
],
"address.city" : [
"[MaxKey, MinKey]"
],
"ssn" : [
"[MinKey, MaxKey]"
]
}
}
}
},
"rejectedPlans" : [ ]
},
"command" : {
"find" : "people",
"filter" : {
"first_name" : {
"$gt" : "J"
}
},
"sort" : {
"address.city" : -1
},
"$db" : "m201"
},
"serverInfo" : {
"host" : "Souls-LX",
"port" : 27017,
"version" : "5.0.4",
"gitVersion" : "62a84ede3cc9a334e8bc82160714df71e7d3a29e"
},
"serverParameters" : {
"internalQueryFacetBufferSizeBytes" : 104857600,
"internalQueryFacetMaxOutputDocSizeBytes" : 104857600,
"internalLookupStageIntermediateDocumentMaxSizeBytes" : 104857600,
"internalDocumentSourceGroupMaxMemoryBytes" : 104857600,
"internalQueryMaxBlockingSortMemoryUsageBytes" : 104857600,
"internalQueryProhibitBlockingMergeOnMongoS" : 0,
"internalQueryMaxAddToSetBytes" : 104857600,
"internalDocumentSourceSetWindowFieldsMaxMemoryBytes" : 104857600
},
"ok" : 1
}
by the way, there is no “address.state” in this collection and I used “address.street” instead. however, it does not affect the result because query does not use it.