"_id" index & covered queries

Hi, I learned that every collection starts with an index on _id by default.

I also learned that for a query to be considered covered, all input and resultant fields must be part of an index. For example:

Given a single index {a: 1, b: 1}

db.test.find( { a: 'John', b: 'Karen' }, { _id: 0, a: 1, b: 1 } ) is covered

db.test.find( { a: 'John', b: 'Karen' }, { a: 1, b: 1 } ) is not covered because _id will appear in the output and is not indexed.

That said, as _id is indexed for all documents in a collection by default, why wouldn’t index intersection allow the second find() query to be covered (not have to run a FETCH stage)?

Covered also means that it will use the index alone to find the documents, it will do it all in RAM. In the second query it uses the index but it’s not covered because _id is not part of the same index. This is another rule of a Covered Query.

I see. Reading just this:

A covered query is a query that can be satisfied entirely using an index and does not have to examine any documents.

It wasn’t clear to me that an index meant precisely one index, but now it makes sense. Thanks!

Yes it’s not clear that the index must be a lone index and not multiple. I had a similar misunderstanding too.

1 Like