Before you attempt any downgrade, familiarize yourself with the content of this document.
Downgrade Path
Important
Before you upgrade or downgrade a replica set, ensure all replica set members are running. If you do not, the upgrade or downgrade will not complete until all members are started.
If you need to downgrade from 4.4, downgrade to the latest patch release of 4.2.
MongoDB only supports single-version downgrades. You cannot downgrade to a release that is multiple versions behind your current release. You may downgrade a 4.4-series to a 4.2-series deployment, however, further downgrading that 4.2-series deployment to a 4.0-series deployment is not supported.
Warning
Downgrade Floor
If you need to downgrade from version 4.4, downgrade to 4.2.6 or a later version. You cannot downgrade to 4.2.5 or an earlier version.
Create Backup
Optional but Recommended. Create a backup of your database.
Prerequisites
Remove Incompatible Features
To downgrade from 4.4 to 4.2, you must remove incompatible features that are persisted and/or update incompatible configuration settings.
1. Namespace Length
Starting in MongoDB 4.4:
For featureCompatibilityVersion set to
"4.4"or greater, MongoDB raises the limit on collection/view namespace to 255 bytes. For a collection or a view, the namespace includes the database name, the dot (.) separator, and the collection/view name (e.g.<database>.<collection>),For featureCompatibilityVersion set to
"4.2"or earlier, the maximum length of the collection/view namespace remains 120 bytes.
Before downgrading, resolve any collections or views with namespaces that exceed the 120-byte Namespace Length limit for Feature Compatibility Version (fCV) 4.2.
To determine if you have any collections or views with namespaces
that exceed the 120-byte limit, connect mongo shell
to a mongos instance and run:
db.adminCommand("listDatabases").databases.forEach(function(d){ let mdb = db.getSiblingDB(d.name); mdb.getCollectionInfos( ).forEach(function(c){ namespace = d.name + "." + c.name namespacelenBytes = encodeURIComponent(namespace).length if (namespacelenBytes > 120) { print (c.type.toUpperCase() + " namespace exceeds 120 bytes:: " + namespace ) } } ) })
If any collection or view namespace exceeds 120 bytes, then prior to downgrading the fCV:
For unsharded collections, rename the collections using the
renameCollectioncommand.For sharded collections:
Use
$mergeto a new sharded collection with a shorter name, then drop the original collection.Use
mongoexportandmongoimportinto a new collection with a shorter name, then drop the original collection.
For views, use
db.createView()to recreate the view using a shorter name, then drop the original view.
2. Downgrade Feature Compatibility Version (fCV)
Tip
Downgrading to featureCompatibilityVersion (fCV) : "4.2" implicitly performs a
replSetReconfigon each shard to remove thetermfield from the configuration document and blocks until the new configuration propagates to a majority of replica set members.
To downgrade the featureCompatibilityVersion of your sharded
cluster:
Downgrade the
featureCompatibilityVersionto"4.2".db.adminCommand({setFeatureCompatibilityVersion: "4.2"}) The
setFeatureCompatibilityVersioncommand performs writes to an internal system collection and is idempotent. If for any reason the command does not complete successfully, retry the command on themongosinstance.Note
While
setFeatureCompatibilityVersionis running on the sharded cluster, chunk migrations, splits, and merges can fail withConflictingOperationInProgress.To ensure that all members of the sharded cluster reflect the updated
featureCompatibilityVersion, connect to each shard replica set member and each config server replica set member and check thefeatureCompatibilityVersion:Tip
For a sharded cluster that has access control enabled, to run the following command against a shard replica set member, you must connect to the member as a shard local user.
db.adminCommand( { getParameter: 1, featureCompatibilityVersion: 1 } ) All members should return a result that includes:
"featureCompatibilityVersion" : { "version" : "4.2" } If any member returns a
featureCompatibilityVersionof"4.4", wait for the member to reflect version"4.2"before proceeding.
Note
Arbiters do not replicate the admin.system.version collection.
Because of this, arbiters always have a feature compatibility version equal
to the downgrade version of the binary, regardless of the fCV value of the
replica set.
For example, an arbiter in a MongoDB 4.4 cluster, has an fCV value of 4.2.
For more information on the returned featureCompatibilityVersion
value, see View FeatureCompatibilityVersion.
3. Remove fCV 4.4 Persisted Features
The following steps are necessary only if fCV has ever been set to
"4.4".
Remove all persisted 4.4 features that are incompatible with 4.2. These include:
- Compound Hashed Indexes
Remove all compound hashed indexes.
Use
db.collection.getIndexes()to identify any compound hashed indexes in a collection and usedb.collection.dropIndex()to remove those indexes.If any collection is sharded using the compound hashed index, use the guidance listed under
Compound Hashed Shard Keysbelow.- Compound Hashed Shard Keys
Remove any sharded collections which were sharded using a compound hashed shard key.
Use
sh.status()to identify sharded collections with a compound hashed shard key and remove those collections.If the collection is empty or if the collection does contain data that requires preservation, use
db.collection.drop()to drop the collection.If the collection has data that requires preservation, back up the collection first before using
db.collection.drop()to drop the collection. After downgrading the cluster, restore the data into the cluster.
4. Remove 4.4 Features
Remove all persisted features that use 4.4 features. These include but are not limited to:
If any view definitions that include 4.4 operators, such as
$unionWithor$function. See also New Aggregation Operators.
5. Update Missing Shard Keys
In MongoDB 4.4, shard key fields can be missing from documents in a sharded collection.
If you downgrade to 4.2 and a sharded collection contains documents
missing shard key fields, 4.2 mongos instances do not
return documents that are missing the shard key fields. To avoid
this situation, update the documents to includes the missing shard
key fields before you downgrade.
To find documents that are missing any part of the shard key, use the
$exists operator.
For example, if the collection contacts has the shard key {
zipcode: 1 }, to find documents without the zipcode field:
db.contacts.find( { zipcode: { $exists: false } } )
To set the missing shard key field to null for these documents,
you can use the db.collection.updateMany() method:
db.contacts.updateMany( { zipcode: { $exists: false } }, { $set: { zipcode: null } } )
If setting the missing shard key field to a non-null value, you
cannot use the db.collection.updateMany() method and you
must perform the update within a transaction or as a retryable
write. For details, see Set the Missing Shard Key Fields.
6. Update Shard Keys That Exceed 512 bytes
Changed in version 4.4: In version 4.4, MongoDB removes the 512-byte limit on the shard key size. However, for MongoDB 4.2 and earlier, a shard key cannot exceed 512 bytes. Update any shard key value that exceed the 512-byte size limit to be within the 512 byte size limit. To update a document's shard key value, see Change a Document's Shard Key Value.
Confirm Clean Shutdown
When a step prompts you to stop your deployment, ensure your
mongod instance is
shut down.
Procedure
Downgrade a Sharded Cluster
Warning
Before proceeding with the downgrade procedure, ensure that all
members, including delayed replica set members in the sharded
cluster, reflect the prerequisite changes. That is, check the
featureCompatibilityVersion and the removal of incompatible
features for each node before downgrading.
Download the latest 4.2 binaries.
Using either a package manager or a manual download, get the latest release in the 4.2 series. If using a package manager, add a new repository for the 4.2 binaries, then perform the actual downgrade process.
Important
Before you upgrade or downgrade a replica set, ensure all replica set members are running. If you do not, the upgrade or downgrade will not complete until all members are started.
If you need to downgrade from 4.4, downgrade to the latest patch release of 4.2.
Disable the Balancer.
Connect a mongo shell to a mongos instance in
the sharded cluster, and run sh.stopBalancer() to
disable the balancer:
sh.stopBalancer()
Note
If a migration is in progress, the system will complete the
in-progress migration before stopping the balancer. You can run
sh.isBalancerRunning() to check the balancer's current
state.
To verify that the balancer is disabled, run
sh.getBalancerState(), which returns false if the balancer
is disabled:
sh.getBalancerState()
For more information on disabling the balancer, see Disable the Balancer.
Downgrade each shard, one at a time.
Downgrade the shards one at a time.
Downgrade the shard's secondary members one at a time:
Run the following command from the
mongoshell to perform a clean shutdown, or refer to StopmongodProcesses for additional ways to safely terminate themongodprocess:db.adminCommand( { shutdown: 1 } ) Replace the 4.4 binary with the 4.2 binary and restart.
Wait for the member to recover to
SECONDARYstate before downgrading the next secondary member. To check the member's state, connect amongoshell to the shard and runrs.status()method.Repeat to downgrade for each secondary member.
Downgrade the shard arbiter, if any.
Skip this step if the replica set does not include an arbiter.
Run the following command from the
mongoshell to perform a clean shutdown, or refer to StopmongodProcesses for additional ways to safely terminate themongodprocess:db.adminCommand( { shutdown: 1 } ) Delete the contents of the arbiter data directory. The
storage.dbPathconfiguration setting or--dbpathcommand line option specify the data directory of the arbitermongod.rm -rf /path/to/mongodb/datafiles/* Replace the 4.4 binary with the 4.2 binary and restart.
Wait for the member to recover to
ARBITERstate. To check the member's state, connect amongoshell to the member and runrs.status()method.
Downgrade the shard's primary.
Step down the replica set primary. Connect a
mongoshell to the primary and users.stepDown()to step down the primary and force an election of a new primary:rs.stepDown() Run
rs.status().rs.status() When the status shows that the primary has stepped down and another member has assumed
PRIMARYstate, proceed.Run the following command from the
mongoshell to perform a clean shutdown of the stepped-down primary, or refer to StopmongodProcesses for additional ways to safely terminate themongodprocess:db.adminCommand( { shutdown: 1 } ) Replace the 4.4 binary with the 4.2 binary and restart.
Repeat for the remaining shards.
Downgrade the config servers.
Downgrade the secondary members of the config servers replica set (CSRS) one at a time:
Run the following command from the
mongoshell to perform a clean shutdown, or refer to StopmongodProcesses for additional ways to safely terminate themongodprocess:db.adminCommand( { shutdown: 1 } ) Replace the 4.4 binary with the 4.2 binary and restart.
Wait for the member to recover to
SECONDARYstate before downgrading the next secondary member. To check the member's state, connect amongoshell to the shard and runrs.status()method.Repeat to downgrade for each secondary member.
Step down the config server primary.
Connect a
mongoshell to the primary and users.stepDown()to step down the primary and force an election of a new primary:rs.stepDown() Run
rs.status().rs.status() When the status shows that the primary has stepped down and another member has assumed
PRIMARYstate, proceed.Run the following command from the
mongoshell to perform a clean shutdown of the stepped-down primary, or refer to StopmongodProcesses for additional ways to safely terminate themongodprocess:db.adminCommand( { shutdown: 1 } ) Replace the 4.4 binary with the 4.2 binary and restart.
Re-enable the balancer.
Once the downgrade of sharded cluster components is complete, connect
a mongo shell to a mongos and
re-enable the balancer.
sh.startBalancer()
The mongo shell method sh.startBalancer()
also enables auto-splitting for the sharded cluster.