0

An ObjectId in MongoDB is a 12-byte BSON type. In the 12-byte structure, the first 4 bytes of the ObjectId represent the time in seconds since the UNIX epoch. The next 3 bytes of the ObjectId represent the machine identifier. The next 2 bytes of the ObjectId represent the process ID. And the last 3 bytes of the ObjectId represent a random counter value.

These 12 bytes altogether uniquely identifies a document within the MongoDB collection and serves as a primary key for that document. ObjectId is the default value of _id field of each document and its complexity helps to fetch a unique _id field for a particular document in the MongoDB.

A client want to use the same identifier but for some reason they can't use the 5813eed6e6893b80c9ae5bba 24 long identifier, so in a moment of desperation the tec lead sugest cut off the 4 first digit 5813 -- eed6e6893b80c9ae5bba, but I dont know how a good idea is this and what is the probability of collision if this idea is apply, we are assuming the 4 first digit are corresponding to "the random counter value part" as expressed in the docs.

So my question is which is the probability of collision of this shorted id eed6e6893b80c9ae5bba ? if it is a bad idea, how to convert the id into a equivalent of 20 characters long ?.

Andrés Muñoz
  • 225
  • 2
  • 10
  • You can basically put whatever you want in the `_id`, as long as it's "unique". Asking for a rundown of all the "collision possibilties" is pretty broad in scope. Note that you cannot "replace" the `_id` field as it is immutable, so the best you can do is add a different field as the unique key, or indeed rewrite the entire collection. As for "cut off the beginning"! Considering that's the "timestamp" portion which is doing "some effort" towards uniqueness, then that's the worst possible part to remove. – Neil Lunn Nov 13 '18 at 06:11
  • Basically though, this is a debate which has been "done to death" already, so I'm loathe to see why it needs to be addressed "again"! Take a look at the very URI in the address bar of your question here for a very atypical example of how the world deals with "short identifiers to keep the client happy". – Neil Lunn Nov 13 '18 at 06:14

0 Answers0