MongoDB - Advantage of using 12 bytes string as un

2020-05-03 12:35发布

问题:

Is there any specific reason for MongoDB generating uuid as unique identifier instead of incremental values as unique identifier?.

回答1:

Incrementing values or sequences require a central point of reference which is a limiting factor for scaling. ObjectIDs are designed to be reasonably unique IDs that can be independently generated in a distributed environment with monotonically increasing values (a leading timestamp component) for approximate ordering.

ObjectIDs are typically generated by MongoDB drivers so there is no need to make a server round-trip to find the next available _id or wait for the server result of an insert operation to know what _id was allocated. If a driver or client application inserts a document without including an _id value, an ObjectID will be generated by the mongod server.

There is no strict requirement to use ObjectIDs in MongoDB: you can provide your own _id values if there is a more natural unique key for your data or you prefer an alternative primary key format.