Index Versions

MongoDB v2.0 uses a new data structure for its indexes. The new {v:1} indexes are, on average, 25% smaller than the old {v:0} indexes. This usually results in significantly increased performance. In addition, signed (pre 1970) dates are supported in {v:1} indexes.

Backward-compatibility with {v:0} indexes is maintained. Thus, you can upgrade to MongoDB v2.0 seamlessly. However, to get the benefits of the new index format, you'll need to reindex.

Converting an Existing Index to {v:1} format

All operations that create a new index will result in a {v:1} index by default.

  • Reindexing will result in a replacement of any {v:0} index of with a {v:1} type.
    • Due to SERVER-3866, reindexing on a secondary does not work in <=v2.0.0. Do not reindex on a secondary as the drop phase of reindex is done and then the rest of the work aborts. See jira for workaround.
  • A compact command invocation will convert all indexes for the given collection to {v:1} type.

In v2.0.1, the following operations will convert {v:0} indexes to {v:1} type:

Rolling Back to MongoDB < v2.0

While MongoDB v2.0 supports the old index format, old versions will not support the new format. If you need to roll back to an old version, the server will run, but queries and other operations involving the newer indexes will log and return an error. Thus, you'll need to recreate any new index you'd like to use on an old server.

Versions >= 1.8.3 are aware of the index version field, but version <= 1.8.2 are not. So if you rollback a [v:1} index to 1.8.2 and re-index it its version will still be marked {v: 1} although it actual version is {v: 0}. Then if you upgrade again to 2.0 this index won't work even though they are marked as {v: 1} in db.system.indexes. So if you have to rollback to a version <= 1.8.2, you must delete the index then create it again (instead of simply re-indexing).

Building a {v:0} Index

You can still create a {v:0} index with MongoDB v2.0. To do so, add the option {v:0} in the index creation command. For example in the mongo shell:

> // defaults to a v:1 index 
> db.foo.ensureIndex({name:1})
> db.system.indexes.find()
{ "v" : 1, "key" : { "_id" : 1 }, "ns" : "mydb.foo", "name" : "_id_" }
{ "v" : 1, "key" : { "name" : 1 }, "ns" : "mydb.foo", "name" : "name_1" }
> db.foo.dropIndex({name:1})
{ "nIndexesWas" : 2, "ok" : 1 }

> // create a v:0 index 
> db.foo.ensureIndex({name:1},{v:0})
> db.system.indexes.find()
{ "v" : 1, "key" : { "_id" : 1 }, "ns" : "mydb.foo", "name" : "_id_" }
{ "v" : 0, "key" : { "name" : 1 }, "ns" : "mydb.foo", "name" : "name_1" }

Follow @mongodb

MongoDB Pittsburgh - May 15
MongoNYC - May 23
MongoDB Paris - Jun 14
MongoDB UK - Jun 20
MongoDC - June 26


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

PLEASE POST QUESTIONS IN THE USER GROUPS FORUM. Post non-question comments and helpful hints here.

blog comments powered by Disqus