Thoughts on MongoDB indexes and auto increment

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.

Tags:

IT

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen

About the author

Name: Ilya Khaprov (rus Илья Хапров)

Age: 25

Sex: Male

I'm a postgraduate (an "aspirant" in Russian terminology see Wikipedia for details) at Bryansk State Technical University.

I'm working with .Net since 2004. Also i like lisp.

My research interests lie in the area of Intelligence. In particular i am studing personal information filters, ontology learning, and some other stuff.

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in  anyway.

© Copyright 2011

Recent Visitors