|
Hi Matt –
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 :
note: the projection document {item: 1, _id: 0} indicates that the item field should return and that the _id field should not return ( 0 in the projection document suppresses the field). When you specify {item: 1}, this automatically suppresses all other field except for the _id field, so you must include _id: 0 in the projection document as well to suppress the field from the result set
> 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.
|