Database Reference
In-Depth Information
},
"allPlans" : [
{
"cursor" : "BtreeCursor close_1",
"indexBounds" : {
"close" : [
[
100,
1.7976931348623157e+308
]
]
}
},
{
"cursor" : "BtreeCursor stock_symbol_1",
"indexBounds" : {
"stock_symbol" : [
[
"GOOG",
"GOOG"
]
]
}
},
{
"cursor" : "BasicCursor",
"indexBounds" : {
}
}
]
}
You'll see right away that the query plan chooses the index on
{stock_symbol:
1}
to
fulfill the query. Lower down, the
allPlans
key points to a list that includes two addi-
tional query plans: one for the index on
{close:
1}
, and the other a collection scan
with a
BasicCursor
.
It's understandable why the optimizer rejects the collection scan, but it might be
less clear why the index on
{close
:1}
doesn't satisfy. You can use
hint()
to find out.
hint()
forces the query optimizer to use a particular index:
query = {stock_symbol: "GOOG", close: {$gt: 100}}
db.values.find(query).hint({close: 1}).explain()
{
"cursor" : "BtreeCursor close_1",
"nscanned" : 5299,
"n" : 730,
"millis" : 36,
"indexBounds" : {
"close" : [
[
200,
1.7976931348623157e+308
]
]
}
}