MongoDB Index – When is the _id Index Used?

indexmongodb

When is the _id index used by MongoDB?

I could not find anything specific about it in Index Introduction, Analyze Query Performance, and Query Plans.

When sorting by _id at the end of the query via .sort({_id: -1}), .explain() explicitly shows the index use. It might be that it is implicitly used in every COLLSCAN operation, though.

So is it just a normal index that is only accessed when sorting/querying by _id, or does it have hidden uses not shown by .explain()?

Best Answer

When sorting by _id at the end of the query via .sort({_id: -1}), .explain() explicitly shows the index use. It might be that it is implicitly used in every COLLSCAN operation, though.

A COLLSCAN operation is a collection scan, which indicates that no index is being used and the collection is being iterated in natural order.

If your query had no criteria but requested a sort by _id, the _id index would be the best candidate to return results in the expected order. In MongoDB 3.0 I would expect this to be indicated as an IXSCAN rather than a COLLSCAN.

So is it just a normal index that is only accessed when sorting/querying by _id

As you suspected, the _id index is just a normal index which is a candidate for being selected by the query planner when querying and/or sorting by _id.

does it have hidden uses not shown by .explain()

The explain() results shows relevant query planning information, but by default only include a summary of the winning plan. You can see more detailed information (such as the candidate plans evaluated) using .explain(true).

MongoDB 3.0's explain() actually has 3 verbosity modes:

  • queryPlanner: default
  • executionStats: more info on the winning plan
  • allPlansExecution (aka true): information on all plans

There are some other uses of the _id index which aren't exactly hidden but may not be obvious:

  • replication requires a unique _id index (historical note: in versions prior to MongoDB 2.2 it was possible to create and replicate capped collections without an _id index).

  • a snapshot cursor iterates a collection in _id order to ensure a query will not return a document multiple times.