by
8. December 2009 18:57
If you come(as i do) from SQL world the first thing about indexes you may ask about is auto incremented indexes. Personally i used it as an unique id in relationships such as author->article. However in MongoDB we can use extremely useful built-in, auto generated, unique ids (such an id always appended to the document with key “_id”).
In SQL word id is usually an 4 byte integer, but in Mongo it’s 12 bytes long.
According to the documentation(http://www.mongodb.org/display/DOCS/Object+IDs) ObjectId generator uses increment.
Anyway, some very interesting links:
http://jira.mongodb.org/browse/SERVER-195
Very detailed discussion on relations for SQL guys
Note: in mongodb-csharp ObjectID type is called Oid.
UPDATE
Although ObjectIds are unique you can’t (at least not always) use it for (date) sorting. If you insert records from many machines (or processes) MongoDb doesn’t guarantee that they will constantly grow since internally it uses memcmp function to compare _ids
When the _ids compared memcmp firstly compares time when they created. Usually ObjectID generator have 1 second resolution. If time is the same then machine ids compared. In mongodb-csharp driver for example, machine id is the first 3 bytes of HostName md5 hash. As you can see the result of comparison even at this point is unpredictable.
66723259-67ca-474d-850f-df71e2885488|0|.0
Tags: mongodb
IT