Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are MongoDB's ObjectIds generated?

Tags:

mongodb

Are they somewhat random?

I mean....would people be able to break them apart?

like image 232
TIMEX Avatar asked Apr 28 '11 11:04

TIMEX


People also ask

How is MongoDB's _id generated?

MongoDB uses ObjectIds as the default value of _id field of each document, which is generated while the creation of any document. The complex combination of ObjectId makes all the _id fields unique.

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.

How does the value of _id get assigned to a document?

_id is the primary key on documents in a collection; with it, documents (records) can be differentiated from each one another. _id is automatically indexed. Lookups specifying { _id: <someval> } refer to the _id index as their guide. By default the _id field is of type ObjectID, one of MongoDB's BSON types.

How an ObjectId type is composed in MongoDB?

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.


4 Answers

They are not random and can be easily predicted :

A BSON ObjectID is a 12-byte value consisting of a 4-byte timestamp (seconds since epoch), a 3-byte machine id, a 2-byte process id, and a 3-byte counter

http://www.mongodb.org/display/DOCS/Object+IDs

like image 159
Maxence Avatar answered Sep 24 '22 06:09

Maxence


Heres a javascript implementation of the MongoDB ObjectID (http://jsfiddle.net/icodeforlove/rN3zb/)

function ObjectIdDetails (id) {     return {         seconds: parseInt(id.slice(0, 8), 16),         machineIdentifier: parseInt(id.slice(8, 14), 16),         processId: parseInt(id.slice(14, 18), 16),         counter: parseInt(id.slice(18, 24), 16)     }; } 

So if you have enough of them they leak quite a bit of information about your infrastructure. And you also know the object creation dates for everything.

IE: how many servers do you have, and how many processes each server is running.

like image 27
Chad Scira Avatar answered Sep 22 '22 06:09

Chad Scira


Generation

They are usually generated on the client side by the driver itself. For example, in ruby, BSON::ObjectID can be used:

  • https://github.com/mongodb/bson-ruby/blob/master/lib/bson/object_id.rb#L369

You can also generate your own ObjectIds. This is particularly useful if you want to use business identifiers.

Breakability

  • When using driver generated ObjectIds, is low
  • When using own business Id, is slightly higher depending on their predictability (login, consecutives identifiers...)
like image 34
Oct Avatar answered Sep 24 '22 06:09

Oct


MongoDB database drivers by default generate an ObjectID identifier that is assigned to the _id field of each document. In many cases the ObjectID may be used as a unique identifier in an application.

ObjectID is a 96-bit number which is composed as follows:

a 4-byte value representing the seconds since the Unix epoch (which will not run out of seconds until the year 2106)

a 3-byte machine identifier (usually derived from the MAC address),

a 2-byte process id, and

a 3-byte counter, starting with a random value.

like image 26
jitendra rajput Avatar answered Sep 24 '22 06:09

jitendra rajput