I have a collection of the form
{
"fieldA":"ValueA",
"fieldB":"ValueB"
}
Where fieldA always exists in the document but fieldB may or may not be in the document.
Lets also assume that I have an index on fieldA called fieldAIndex
Now with this index I would expected the below query to be fully covered by fieldAIndex
db.collection.find({"fieldA":"Value1"},{"_id":0,"fieldA":1})
And running with explain() confirms this to be the case:
{
"indexOnly" : true
}
However surprisingly when I run the below query:
db.collection.find({"fieldA":"Value1","fieldB":{"$exists":true}},{"_id":0,"fieldA":1})
explain also returns
{
"indexOnly" : true
}
Given that fieldB is not in the index how can the query return only from the index? Does the index hold information about fields that exists or is the explain returning incorrectly?
Thanks in advance,
Matt.
This behaviour is seen due to a problem in the way indexOnly is reported in explain(). It's due to be fixed in the 2.5.4 mongodb release (https://jira.mongodb.org/browse/SERVER-5759).
To be explicit, checking for the existence of a field not in the index should not return indexOnly=true in the explain(). Any query like this will not be covered by the index.
Thanks to jeffl for pointing me to this mongodb issue.