in mongo shell, Doing this
db.listing.find(
{'p_key': 'DhpPsG'}
).count()
and
db.listing.find({'$query': {'p_key': 'DhpPsG'}}).count()
has different result. The former gives result of 1313 while the latter gives 0.
--------
Same as for $hint and hint(). Doing this
db.listing.find({'$min':
{'_id': '5301fc8327f4f0578d70b24ff6bcc8dd', 'created_on': ISODate("2012-12-26T05:14:35.834Z")}
, '$query': {'p_key': 'DhpPsG'}}).hint(
{'p_key': 1}
).sort([("created_on", 1), ("_id", 1)]).explain()
uses the index as I expected but doing this
db.listing.find({'$min':
{'_id': '5301fc8327f4f0578d70b24ff6bcc8dd', 'created_on': ISODate("2012-12-26T05:14:35.834Z")}
, '$query':
{'p_key': 'DhpPsG'}
, '$hint':{'p_key': 1}}).sort([("created_on", 1), ("_id", 1)]).explain()
does not use the index as I expected.
The result of former is:
{
"cursor" : "BtreeCursor p_key_1",
"isMultiKey" : false,
"n" : 0,
"nscannedObjects" : 83260,
"nscanned" : 83260,
"nscannedObjectsAllPlans" : 83260,
"nscannedAllPlans" : 83260,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 379,
"indexBounds" : {
"p_key" : [
[
{
"$minElement" : 1
}
,
{
"$maxElement" : 1
}
]
]
},
"server" : "mbp.local:27017"
}
the result of latter is:
{
"cursor" : "BasicCursor",
"isMultiKey" : false,
"n" : 0,
"nscannedObjects" : 83260,
"nscanned" : 83260,
"nscannedObjectsAllPlans" : 83260,
"nscannedAllPlans" : 83260,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 1,
"nChunkSkips" : 0,
"millis" : 620,
"indexBounds" : {
},
"server" : "mbp.local:27017"
}