Navigation

Downgrade 4.2 Standalone to 4.0

MongoDB 4.2 is currently in development as part of the 4.1 development series.

警告

While the 4.1-dev-series are available, these versions of MongoDB are for testing purposes only and not for production use.

The following page summarizes various considerations for downgrading to 4.0. For specific instructions for your deployment type, see:

Downgrade Path

Once upgraded to 4.2, if you need to downgrade, we recommend downgrading to the latest patch release of 4.0.

Create Backup

Optional but Recommended. Create a backup of your database.

Downgrade Feature Compatibility Version (fCV)

Before downgrading the binaries, you must downgrade the featureCompatibilityVersion (fCV) to "4.0". For specific instructions to downgrade the fCV, see:

Remove Backwards Incompatible Features

Before downgrading the binaries, remove all persisted 4.2 features that are incompatible with 4.0.

The following provides a summary. For detailed instructions appropriate for your deployment type, see:

Unique Index Version

For featureCompatibilityVersion (fCV) set to "4.2", MongoDB uses a new internal format for unique indexes that is incompatible with 4.0. The new format applies to both existing unique indexes as well as newly created/rebuilt unique indexes. Use the following script to drop and recreate all unique indexes.

// A script to rebuild unique indexes after downgrading fcv 4.2 to 4.0.
// Run this script to drop and recreate unique indexes
// for backwards compatibility with 4.0.

// Obtain a list of unique indexes for both v:1 and v:2
var unique_idx_v1 = [];
var unique_idx_v2 = [];
db.adminCommand("listDatabases").databases.forEach(function(d){
   let mdb = db.getSiblingDB(d.name);
   mdb.getCollectionInfos().forEach(function(c){
      let currentCollection = mdb.getCollection(c.name);
      currentCollection.getIndexes().forEach(function(i){
         if (i.unique){
            if (i.v === 1) {
               unique_idx_v1.push(i);
            }
            else {
               unique_idx_v2.push(i);
            }
            return;
         }
      });
   });
});

printjson(unique_idx_v1);
printjson(unique_idx_v2);

// Drop and recreate all v:1 indexes
for (let idx of unique_idx_v1) {
   let [dbName, collName] = idx.ns.split(".");
   let res = db.getSiblingDB(dbName).runCommand({dropIndexes: collName, index: idx.name});
   assert.commandWorked(res);
   res = db.getSiblingDB(dbName).runCommand({
      createIndexes: collName,
      indexes: [{"key": idx.key, "name": idx.name, "unique": true, "v": 1}]
   });
   assert.commandWorked(res);
}

// Drop and recreate all v:2 indexes
for (let idx of unique_idx_v2) {
   let [dbName, collName] = idx.ns.split(".");
   let res = db.getSiblingDB(dbName).runCommand({dropIndexes: collName, index: idx.name});
   assert.commandWorked(res);
   res = db.getSiblingDB(dbName).runCommand({
      createIndexes: collName,
      indexes: [{"key": idx.key, "name": idx.name, "unique": true, "v": 2}]
   });
   assert.commandWorked(res);
}

Index Key Size

Starting in MongoDB 4.2, for featureCompatibilityVersion (fCV) set to "4.2" or greater, MongoDB removes the Index Key Limit. For fCV set to "4.0", the limit still applies.

If you have an index with keys that exceed the Index Key Limit once fCV is set to "4.0", consider changing the index to a hashed index or to indexing a computed value. You can also temporarily use failIndexKeyTooLong set to false before resolving the problem. However, with failIndexKeyTooLong set to false, queries that use these indexes can return incomplete results.

Index Name Length

Starting in MongoDB 4.2, for featureCompatibilityVersion (fCV) set to "4.2" or greater, MongoDB removes the Index Key Limit. For fCV set to "4.0", the limit still applies.

If you have an index with a name that exceeds the Index Name Length once fCV is set to "4.0", drop and recreate the index with a shorter name.

db.collection.dropIndex( <name | index specification> )

db.collection.createIndex(
   { <index specification> },
   { name: <shorter name> }
}