Covered index and field $exist

60 views
Skip to first unread message

Aurelien B.

unread,
May 3, 2012, 8:51:28 AM5/3/12
to mongod...@googlegroups.com
Hello,

I have a collection with document similar to { A, B, C(optional) }
I build an index: { A:1 B:1 }
My aim is to test the availability in base of document, as fast as possible so I need only to know if I have a doc, what ever is the content.

1. When I query with ...find( { A:a, B:b, C:{$exists : false }, {_id:0, A:1} ),
 the explain() tells me that I am only hitting the index (btw this is what I want) 
but I doesnt understand how it can only hit index without scanning the docs to know presence or not of this C field, or the "indexOnly"  is setted to true because beginning of query start using index ?

2. About findOne() does it works exactly like a find().limit(1) (about the index hitting) ?
2 bis. What are performance between findOne() and .find().limit(1).count(true) ?

Best regards
Message has been deleted

Sid

unread,
May 3, 2012, 1:48:31 PM5/3/12
to mongodb-user
Hi,

1.  Looks like indexOnly is reporting true based on the fact that the
projection field is using an index and not that the query was resolved
using the index. In your 
case since A is the only field you project
(and there is an index on 
it), you are seeing the indexOnly value set
to true).
This seems like a bug and I have created a JIRA ticket to track this :
https://jira.mongodb.org/browse/SERVER-5759

2. Yes, they work the same way.

3. Generally speaking findOne is a better choice.

Aurelien B.

unread,
May 4, 2012, 4:15:45 AM5/4/12
to mongod...@googlegroups.com
Ok ! 
Thank you for your answers.
Reply all
Reply to author
Forward
0 new messages