|
Hi Roshan –
I believe the example queries are correct.
You can verify via the following:
> db.inventory.insert( { item: "cookies", type: "food" } )
|
> db.inventory.ensureIndex( { type: 1, item: 1 } )
|
Run the first query with the explain and you can see that the indexOnly field is true:
> db.inventory.find( { type: "food", item:/^c/ },
|
... { item: 1, _id: 0 } ).explain()
|
{
|
"cursor" : "BtreeCursor type_1_item_1 multi",
|
"isMultiKey" : false,
|
"n" : 1,
|
"nscannedObjects" : 0,
|
"nscanned" : 1,
|
"nscannedObjectsAllPlans" : 0,
|
"nscannedAllPlans" : 1,
|
"scanAndOrder" : false,
|
"indexOnly" : true,
|
"nYields" : 0,
|
"nChunkSkips" : 0,
|
"millis" : 0,
|
"indexBounds" : {
|
"type" : [
|
[
|
"food",
|
"food"
|
]
|
],
|
"item" : [
|
[
|
"c",
|
"d"
|
],
|
[
|
/^c/,
|
/^c/
|
]
|
]
|
},
|
"server" : "bartleby.local:27019"
|
}
|
The second query has indexOnly equal to false, because in the projection document, we specify return the item field, but we do not explicitly state to not return the _id field with the _id: 0 specification
> db.inventory.find( { type: "food", item:/^c/ },
|
... { item: 1 } ).explain()
|
{
|
"cursor" : "BtreeCursor type_1_item_1 multi",
|
"isMultiKey" : false,
|
"n" : 1,
|
"nscannedObjects" : 1,
|
"nscanned" : 1,
|
"nscannedObjectsAllPlans" : 1,
|
"nscannedAllPlans" : 1,
|
"scanAndOrder" : false,
|
"indexOnly" : false,
|
"nYields" : 0,
|
"nChunkSkips" : 0,
|
"millis" : 0,
|
"indexBounds" : {
|
"type" : [
|
[
|
"food",
|
"food"
|
]
|
],
|
"item" : [
|
[
|
"c",
|
"d"
|
],
|
[
|
/^c/,
|
/^c/
|
]
|
]
|
},
|
"server" : "bartleby.local:27019"
|
}
|
Hope this helps.
|