I believe its the way the Query Engine in MongoDB works. Since your first criteria's project matrix satisfies almost everything it does not need to do anything more.
Here are the indexes on the collection:
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"ns" : "test.colTest",
"name" : "_id_"
},
{
"v" : 1,
"key" : {
"fieldA" : 1
},
"ns" : "test.colTest",
"name" : "fieldA_1"
}
]
Plus you have not done your research correctly. Try adding more documents to the collection and your question will fail.
db.colTest.insert({fieldA:"1"})
Run the query:
db.colTest.find({"fieldA":"value1","fieldB":{"$exists":true}}).explain()
Here is the result
{
"cursor" : "BtreeCursor fieldA_1",
"isMultiKey" : false,
"n" : 1,
"nscannedObjects" : 1,
"nscanned" : 1,
"nscannedObjectsAllPlans" : 1,
"nscannedAllPlans" : 1,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 0,
"indexBounds" : {
"fieldA" : [
[
"value1",
"value1"
]
]
},
"server" : "local:27017"
}
Additionally, when you query it differently the result of explain() changes again.
db.colTest.find({"fieldB":{"$exists":true}, "fieldA":"value1"}).explain()
Here is the output:
{
"cursor" : "BtreeCursor fieldA_1",
"isMultiKey" : false,
"n" : 1,
"nscannedObjects" : 1,
"nscanned" : 1,
"nscannedObjectsAllPlans" : 1,
"nscannedAllPlans" : 1,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 0,
"indexBounds" : {
"fieldA" : [
[
"value1",
"value1"
]
]
},
"server" : "local:27017"
}
Please read through documentation, here is the reference:
http://docs.mongodb.org/manual/reference/method/cursor.explain/
I think more reliable way of making sure your indexes are being used is to look at "n", "nscanned" and "nscannedObjects". These fields will give you a more clear picture.
Hope this helps!