|
When a query's projection includes fields that are covered by an index, the covering index is not used.
Notice in this example that the first query is indexOnly: false, but the second (hinted) query is indexOnly: true.
c = db.c;
|
c.drop();
|
|
c.ensureIndex({_id: 1, a: 1});
|
|
c.save({_id: "i", a: "x"});
|
|
c.find({_id: "i"}, {a: 1}).explain();
|
/*
|
{
|
"cursor" : "BtreeCursor _id_",
|
"isMultiKey" : false,
|
"n" : 1,
|
"nscannedObjects" : 1,
|
"nscanned" : 1,
|
"nscannedObjectsAllPlans" : 1,
|
"nscannedAllPlans" : 1,
|
"scanAndOrder" : false,
|
"indexOnly" : false,
|
"nYields" : 0,
|
"nChunkSkips" : 0,
|
"millis" : 0,
|
"indexBounds" : {
|
"_id" : [
|
[
|
"i",
|
"i"
|
]
|
]
|
},
|
"server" : "..."
|
}
|
*/
|
|
c.find({_id: "i"}, {a: 1}).hint({_id: 1, a: 1}).explain();
|
/*
|
{
|
"cursor" : "BtreeCursor _id_1_a_1",
|
"isMultiKey" : false,
|
"n" : 1,
|
"nscannedObjects" : 1,
|
"nscanned" : 1,
|
"nscannedObjectsAllPlans" : 1,
|
"nscannedAllPlans" : 1,
|
"scanAndOrder" : false,
|
"indexOnly" : true,
|
"nYields" : 0,
|
"nChunkSkips" : 0,
|
"millis" : 0,
|
"indexBounds" : {
|
"_id" : [
|
[
|
"i",
|
"i"
|
]
|
],
|
"a" : [
|
[
|
{
|
"$minElement" : 1
|
},
|
{
|
"$maxElement" : 1
|
}
|
]
|
]
|
},
|
"server" : "..."
|
}
|
*/
|
|