Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wonder: Is MongoDB _id unique by default?

Tags:

mongodb

Is MongoDB _id unique by default, or do I have to set it to unique?

like image 995
techbech Avatar asked Mar 29 '12 20:03

techbech


1 Answers

For the most part, _id in mongodb is unique enough. There is one edge case where it's possible to generate a duplicate id though:

If you generate more than 16,777,215 object ids in the same single second, while running in one process while on the same machine then you will get a duplicate id due to how object ids are generated. If you change any one of those things like generating an id in a different process or on a different machine, or at a different unix time, then you won't get a duplicate.

Let me know if you ever manage to pull this off with a realistic use case. Apparently google only gets around 70,000 searches a second and those are all processed on different machines.

like image 87
Max Avatar answered Sep 28 '22 00:09

Max