I know, I know, hint
forces MongoDB to use a specific index.
I have two queries:
db.mycol.find({sourceId:ObjectId("596bac5a6f473e1a042bFFFF"),myFlag:false}).count()
db.mycol.find({sourceId:ObjectId("596bac5a6f473e1a042bFFFF"),myFlag:false}).hint("sourceId_-1_myFlag_-1").count()
Both of these commands take different time:
Without hint: 4-6 seconds
With hint : 0-1 second
However, both of the commands are using the same index:
{ "sourceId" : -1, "myFlag" : -1 }
When I execute db.mycol.aggregate( { $indexStats: { } } )
,
I can see the accesses.ops
count increases for the same index.
Even explain
shows me the same index will be used!