Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are Mongodb ObjectIds GUIDs?

Tags:

guid

mongodb

The autogenerated BSON ID that is stored in the _id field of every document, is it a GUID? The documentation says its 'most likely unique', so I am a little confused. Why would they use an id that is not guaranteed to be unique?

like image 387
pdeva Avatar asked Apr 23 '13 04:04

pdeva


People also ask

Does MongoDB auto generate ID?

By default, MongoDB generates a unique ObjectID identifier that is assigned to the _id field in a new document before writing that document to the database. In many cases the default unique identifiers assigned by MongoDB will meet application requirements.

Is MongoDB ObjectId UUID?

Overview. By default, the MongoDB Java driver generates IDs of the type ObjectId. Sometimes, we may want to use another type of data as the unique identifier of an object, such as a UUID. However, the MongoDB Java driver can't generate UUIDs automatically.

Are MongoDB IDs predictable?

Mongo ObjectIds are generated in a predictable manner, the 12-byte ObjectId value consists of: a 4-byte value representing the seconds since the Unix epoch, a 3-byte machine identifier, a 2-byte process id, and.

Are MongoDB IDs sequential?

Although MongoDB does not support auto-increment sequence as a default feature like some relational databases, we can still achieve this functionality using a counter collection. The counter collection will have a single document that tracks the current unique identifier value.


1 Answers

Its uniqness is based upon probability. Unlike @mattexx answer:

It's not "guaranteed" to be unique because MongoDB does not enforce uniqueness to save time.

MongoDB DOES enforce uniqness on the ObjectId, it in fact has a unique index on the _id field. When talking about saving time, the ObjectId is historical in that manner since it was designed in the days when MongoDB did not ack any writes and needed a 99% chance of being able to insert a new unique record without the client waiting for an ack (ObjectIds are generated client side).

They are not GUIDs however they, as @Asya says, are guaranteed to have a high level of uniqness.

So long as time never moves backwards there is still a 99% chance it will be unique forever. Okay, as @Devesh says, there is a, 1 in 1 trillion (? haven't done the math), chance of even a GUID being duplicated but, again, I do not think you will reach that probability anytime soon.

like image 103
Sammaye Avatar answered Nov 16 '22 00:11

Sammaye