Is MongoDB _id unique by default, or do I have to set it to unique?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With