Are Mongodb’s "id" unique among collections?

Asked

Viewed 51 times

3

It is possible that the same ObjectId is generated for a document in two different collections in the same database?

  • 1

    You want to know if you can duplicate or if it’s likely to happen by a system failure?

  • For a failure. Can create the business rule from this assumption (duplicity possible or not).

  • 1

    Right! The best approach would be not to consider the assumption of duplicity. Even because the possibility of this happening is extremely remote, almost never going to happen. To happen this is the same as to repeat the Datetime. Improbable!!!

2 answers

5


According to official documentation, Objectids are small, "probably" unique, fast-generation and ordered values.

This "probably" documentation would be more related to the impossibility of ensuring that these ids can never be repeated, mainly by the nature of Mongodb’s architecture in facilitating distributed databases. But I haven’t come across any reports of use that would have generated a disability.

Still from documentation Objectid values are 12 bytes long, consisting of:

  • a date/time stamp (timestamp) value of 4 bytes, representing the creation of Objectid, measured in seconds
  • a random value of 5 bytes
  • a 3-byte increment counter initialized with a random value

In this way it is possible to verify why the composition of the Objectid makes it extremely difficult a duplicity, it is not infallible, but it is very rare.

The documentation further describes that the BSON format itself is little-endian, the timestamp and counter values are big-endian, with the most significant bytes appearing first in the byte sequence.

To check the official documentation Mongodb Objectid - BSON Types

0

Browser other questions tagged

You are not signed in. Login or sign up in order to post.