Home > Enterprise >  How many unique IDs can MongoDB generate?
How many unique IDs can MongoDB generate?

Time:08-22

I am currently worried and confused about how the MongoDB's unique ids work and what are the limitations of this.

Is the MongoDB's way of generating IDs is safe for generating millions or even billions of unique ids? And is there a possible scenario that the uid could be duplicated (if the limit is reached)?

CodePudding user response:

The short answer to your first question is YES.

MongoDB uses ObjectID to generate the unique identifier that is assigned to the _id field.

ObjectID is a 96-bit number

Use ObjectID as a unique identifier at mongodb site

Doing math with this, we could have 2^96 or 7.9228163e 28 unique id.

Secondly, the answer to your second question is YES too! The scenario that comes here is when you had used all 2^96 unique ids that mongo gave you.

If you going to build a super big application that literary exceeds the limit. You could set the _id on your own. Mongo would notice you whether if _id set is duplicated.

  • Related