Compatibility and Index Type Changes in MongoDB 2.4

In 2.4 MongoDB includes two new features related to indexes that usersupgrading to version 2.4 must consider, particularly with regard topossible downgrade paths. For more information on downgrades, seeDowngrade MongoDB from 2.4 to Previous Versions.

New Index Types

In 2.4 MongoDB adds two new index types: 2dsphere andtext. These index types do not exist in 2.2, and for eachdatabase, creating a 2dsphere or text index, will upgrade thedata-file version and make that database incompatible with 2.2.

If you intend to downgrade, you should always drop all 2dsphereand text indexes before moving to 2.2.

You can use the downgrade procedure to downgrade thesedatabases and run 2.2 if needed, however this will run a full databaserepair (as with repairDatabase) for all affecteddatabases.

Index Type Validation

In MongoDB 2.2 and earlier you could specify invalid index types thatdid not exist. In these situations, MongoDB would create an ascending(e.g. 1) index. Invalid indexes include index types specified bystrings that do not refer to an existing index type, and all numbersother than 1 and -1. [1]

In 2.4, creating any invalid index will result in an error.Furthermore, you cannot create a 2dsphere or text index on acollection if its containing database has any invalid index types.[1]

Example

If you attempt to add an invalid index in MongoDB 2.4, as in thefollowing:

  1. db.coll.ensureIndex( { field: "1" } )

MongoDB will return the following error document:

  1. {
  2. "err" : "Unknown index plugin '1' in index { field: \"1\" }"
  3. "code": 16734,
  4. "n": <number>,
  5. "connectionId": <number>,
  6. "ok": 1
  7. }
[1](1, 2) In 2.4, indexes that specify a type of"1" or "-1" (the strings "1" and "-1") will continueto exist, despite a warning on start-up. However, asecondary in a replica set cannot complete an initial syncfrom a primary that has a "1" or "-1" index. Avoid allindexes with invalid types.