HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

When is the _id index used?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
the_idusedwhenindex

Problem

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()?

Solution

In MongoDB, documents stored in a collection require a unique _id
field that acts as a primary key. MongoDB uses ObjectIds as the
default value for the _id field if the _id field is not specified;
i.e. if a document does not contain a top-level _id field, the MongoDB
driver adds the _id field that holds an ObjectId. In addition, if the
mongod receives a document to insert that does not contain an _id
field, mongod will add the _id field that holds an ObjectId.

ObjectId is a 12-byte BSON type, constructed using:

a 4-byte value representing the seconds since the Unix epoch,
a 3-byte machine identifier,
a 2-byte process id, and
a 3-byte counter, starting with a random value.


MongoDB clients should add an _id field with a unique ObjectId. Using ObjectIds for the _id field provides the following additional benefits:

  • In the mongo shell, you can access the creation time of the ObjectId,


using the getTimestamp() method.

  • Sorting on an _id field that stores


ObjectId values is roughly equivalent to sorting by creation time.

Alternatives-
The following are common options for storing values for _id:

  • Use an ObjectId.



  • Use a natural unique identifier, if available. This saves space and avoids an additional index.



  • Generate an auto-incrementing number. See Create an Auto-Incrementing Sequence Field.



  • Generate a UUID in your application code. For a more efficient storage of the UUID values in the collection and in the _id index, store the UUID as a value of the BSON BinData type.



  • Index keys that are of the BinData type are more efficiently stored in the index if:


the binary subtype value is in the range of 0-7 or 128-135, and
the length of the byte array is: 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, or 32.

  • Use your driver’s BSON UUID facility to generate UUIDs. Be aware that driver implementations may implement UUID serialization and deserialization logic differently, which may not be fully compatible with other drivers. See your driver documentation for information concerning UUID interoperability.



for more detail-
See MongoDB

Code Snippets

ObjectId is a 12-byte BSON type, constructed using:

a 4-byte value representing the seconds since the Unix epoch,
a 3-byte machine identifier,
a 2-byte process id, and
a 3-byte counter, starting with a random value.

Context

StackExchange Database Administrators Q#115444, answer score: 3

Revisions (0)

No revisions yet.