Optimizing Storage of Small Objects

MongoDB records have a certain amount of overhead per object (BSON document) in a collection.  This overhead is normally insignificant, but if your objects are tiny (just a few bytes, maybe one or two fields) it would not be.  Below are some suggestions on how to optimize storage efficiently in such situations.

Using the _id Field Explicitly

Mongo automatically adds an object ID to each document and sets it to a unique value.  Additionally this field in indexed.  For tiny objects this takes up significant space.

The best way to optimize for this is to use _id explicitly.  Take one of your fields which is unique for the collection and store its values in _id.  By doing so, you have explicitly provided IDs.  This will effectively eliminate the creation of a separate _id field.  If your previously separate field was indexed, this eliminates an extra index too.

Using Small Field Names

Consider a record

{ last_name : "Smith", best_score: 3.9 }

The strings "last_name" and "best_score" will be stored in each object's BSON.  Using shorter strings would save space:

{ lname : "Smith", score : 3.9 }

Would save 9 bytes per document.  This of course reduces expressiveness to the programmer and is not recommended unless you have a collection where this is of significant concern.

Field names are not stored in indexes as indexes have a predefined structure.  Thus, shortening field names will not help the size of indexes.  In general it is not necessary to use short field names.

Combining Objects

Fundamentally, there is a certain amount of overhead per document in MongoDB.  One technique is combining objects.  In some cases you may be able to embed objects in other objects, perhaps as arrays of objects.  If your objects are tiny this may work well, but will only make sense for certain use cases.


Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.

PLEASE POST QUESTIONS IN THE FORUMS: http://groups.google.com/group/mongodb-user. Post tips and clarifications here.

blog comments powered by Disqus