Why MongoDB has Objectid instead of auto incremental ID

I am from Oracle background.
I was surprised to see _id field with datatype Objectid.
Isn’t more costly to search for these long _id e.g,
_id: 5d76437dfa47924acd0f38a4 compared to id: 345
e.,g in a website, for sure, we are not going to pass in the URL such long _id value:

** I understand that we can use a function to generate auto incremental ID field.

Hi @M_fateh_almsaddi_29101,

First of all good question :slight_smile:

MongoDB do not restrict you from creating your own _id field. You can totally create your own _id field and assign any type of values that suits your application needs :slight_smile: such as auto-incremental ID field. For more information on how to choose a custom _id value in MongoDB, please refer our documentation.

Here are some important aspects of ObjectId which makes it important in MongoDB .

ObjectIds in MongoDB are small, likely unique, fast to generate, and ordered. It is 12 bytes long, which is made-up of several 3-5 bytes chains. Each chain represents and designates a specific aspect of the document’s identity.

  • The first 4 bytes represents the seconds since the unix epoch
  • The next 5 bytes are the random value
  • The last 3 bytes are a random counter value

All this combination is used to ensure we have unique _id field for each document because this field acts as a primary key in MongoDB documents. For this reason, we use ObjectId.

By default MongoDB creates an index on the _id field which makes the search operation fast. For more information on indexes in MongoDB, please refer our documentation on indexes.

However, when you are managing the _id field on your own then you will have to take care of some of the additional overheads that comes with the strategy that you are following.

For instance, let’s say you are using an auto-incremental _id field in MongoDB then let’s see some of the possible considerations that you might have to make.

  1. As you know, MongoDB is a distributed database and let’s say you are running your MongoDB instances on multiple servers then in that case you will have to ensure the auto-incrementing function is always assigning unique values for all writes that are happening on different servers.

  2. What will happen when two users submitting create requests at around the same time. If you are setting up your own ID generator using a basic number, you would assign the same ID to both - causing a write conflict and either surfacing an error to the user that they couldn’t have controlled or causing your app to lag as it keeps submitting the same request until it finds an ID that works.

  3. Are you planning to re-use those _id values for which you have deleted the corresponding documents. It might be the case when you are not retaining the data for a long time and doing a lot of deletes. In that case, you will have to keep track of the values which are available for re-use and subsequently you want to use an efficient data structure which does efficient push and pop of the data.

If your application is write intensive and let’s say a million documents is being inserted every minute or so then it would not be long before your auto-incremental id field will be 10-15 digits long.

In essence, before you make any decision you need to be aware of the pros and cons that the given choice has to offer. MongoDB leverages the benefits provided by ObjectId to remove some of the overheads that we have discussed above.

If you still have any query, please feel free to get back to us.

Happy Learning :slight_smile:

Shubham Ranjan
Curriculum Support Engineer


This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.