17

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?

pdeva
  • 43,605
  • 46
  • 133
  • 171
  • 1
    it is guaranteed to be unique at least I have never seen a real collision. There is plenty of documentation of what exactly is encoded it in. – Asya Kamsky Apr 23 '13 at 05:19
  • 1
    Even GUID is not guarantee its uniqueness but the way they form they are likely to be unique all the time. – Devesh Apr 23 '13 at 05:39

4 Answers4

8

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.

Sammaye
  • 43,242
  • 7
  • 104
  • 146
4

It is unique in most of the requirement and it is consists of timestamp , unique identifier of the machine (hash of the machine host) , process Identifier and in last the increment number. http://docs.mongodb.org/manual/reference/object-id/

Devesh
  • 4,500
  • 1
  • 17
  • 28
2

The chance of an ID collision is theoretically close enough to zero that it can be presumed for typical web apps. Many real-world systems (Mongo or not) do rely on this property of GUIDs, though it would not be a good assumption for safety/mission-critical systems.

In practical terms, there are indeed scenarios where it could go wrong if there's a misconfiguration or third-party library bug. Those shouldn't rule out the concept, but important to be aware of those risks and avoid them where possible.

Some good analysis here of practical issues that could cause a collision. In particular:

Some Mongo drivers use random numbers instead of incrementing numbers for the counter bytes. In these cases, there is a 1/16,777,216 chance of generating a non-unique ID, but only if those two IDs are generated in the same second (i.e. before the time section of the ID updates to the next second), on the same machine, in the same process.

Community
  • 1
  • 1
mahemoff
  • 44,526
  • 36
  • 160
  • 222
0

ObjectId is explained in the doc here. It's not "guaranteed" to be unique because MongoDB does not enforce uniqueness to save time. It simply trusts that the complicated generation algorithm will probably never produce two identical ObjectIds in the same datastore. So technically it is not a GUID, but pretty much as good.

mattexx
  • 6,456
  • 3
  • 36
  • 47