All Products
Search
Document Center

:Compatibility changes in MongoDB 4.2

Last Updated:Apr 27, 2025

This topic describes compatibility changes in MongoDB 4.2.

To view the official MongoDB compatibility change documentation, visit Legacy Documentation.

Removal of the MMAPv1 storage engine

MongoDB 4.2 removes support for the MMAPv1 storage engine.

If using the MMAPv1 storage engine in MongoDB 4.0, you must change the storage engine to WiredTiger before upgrading your instance to MongoDB 4.2.

MMAPv1-specific options

MongoDB 4.2 removes the following MMAPv1-specific options:

Removed option

Removed command line

storage.mmapv1.journal.commitIntervalMs

storage.mmapv1.journal.debugFlags

mongod --journalOptions

storage.mmapv1.nsSize

mongod --nssize

storage.mmapv1.preallocDataFiles

mongod --noprealloc

storage.mmapv1.quota.enforced

mongod --quota

storage.mmapv1.quota.maxFilesPerDB

mongod --quotaFiles

storage.mmapv1.smallFiles

mongod --smallfiles

storage.repairPath

mongod --repairpath

replication.secondaryIndexPrefetch

mongod --replIndexPrefetch

Note

Starting from MongoDB 4.2, MongoDB deprecates the preceding options and command lines. If using WiredTiger, remove MMAPv1-specific options.

MMAPv1-specific parameters

MongoDB 4.2 removes the following MMAPv1-specific parameters:

  • newCollectionsUsePowerOf2Sizes

  • replIndexPrefetch

MMAPv1-specific command

MongoDB 4.2 removes the MMAPv1-specific command touch.

MMAPv1-specific options for commands and methods

MongoDB 4.2 removes the following MMAPv1-specific options:

  • noPadding and usePowerOf2Sizes from the collMod command.

  • verbose from the collStats command.

  • flags from the create command.

  • paddingFactor, paddingBytes, and preservePadding from the db.createCollection() command.

MongoDB 4.2 ignores the MMAPv1-specific option async from the fsync command.

Removal or deprecation of commands and methods

Deprecation of the group command

Starting from MongoDB 4.2, MongoDB deprecates the group command (deprecated in MongoDB 3.4) and its mongo shell helper function db.collection.group().

Use db.collection.aggregate() along with the $group stage instead.

Deprecation of the eval command

Starting from MongoDB 4.2, MongoDB deprecates the eval command (deprecated in MongoDB 3.0).

The mongo shell helper functions db.eval() and db.collection.copyTo() can only be run when you connect to an instance that runs MongoDB 4.0 or earlier.

Deprecation of the copydb and clone commands

Starting from MongoDB 4.2, MongoDB deprecates the copydb and clone commands. The mongo shell helper functions db.copyDatabase() and db.cloneDatabase() can only be run when you connect to an instance that runs MongoDB 4.0 or earlier.

Instead, use mongodump and mongorestore (along with the mongorestore options --nsFrom and --nsTo) or a driver for writing a script.

For example, to copy the test database to the examples database on the same instance, perform the following steps:

  1. Use mongodump to dump the test database to an archived file named mongodump-test-db.

    mongodump --archive="mongodump-test-db" --db=test
  2. Use mongorestore along with the --nsFrom and --nsTo parameters to restore data from the archived file.

    mongorestore --archive="mongodump-test-db" --nsFrom='test.*' --nsTo='examples.*'
Note

You can add parameters as needed, such as --uri, --host, --username.

Alternatively, instead of using the archived file, utilize mongodump to dump the test database to the standard output stream and then pipe it into mongorestore.

mongodump --archive --db=test | mongorestore --archive  --nsFrom='test.*' --nsTo='examples.*'

Deprecation of the parallelCollectionScan command

Starting from MongoDB 4.2, MongoDB deprecates the parallelCollectionScan command.

Removal of the maxScan option

Starting from MongoDB 4.2, MongoDB removes the maxScan option from the find command and the mongo shell helper function cursor.maxScan(). Use the maxTimeMS option of the find command or the mongo shell method cursor.maxTimeMS() instead.

Deprecation of the geoNear command

Starting from MongoDB 4.2, MongoDB deprecates the geoNear command. Use the $geoNear aggregation stage instead.

For the $geoNear stage, its options are similar to those in the deprecated geoNear command with the following exceptions:

  • The output of the deprecated geoNear command includes the dis field that contains the distance information.

    For the $geoNear stage, specify the distance field name in distanceField.

  • The deprecated geoNear command accepts a boolean value for the includeLocs option to include the loc field.

    For the $geoNear stage, specify the location field name in includeLocs.

  • The output of the deprecated geoNear command includes avgDistance and maxDistance.

    You can use the aggregation pipeline to return avgDistance and maxDistance. Specially, include the group stage to calculate avgDistance and maxDistance after the geoNear stage.

    db.places.aggregate([
       { $geoNear: { near: <...>, distanceField: "dis", includeLocs: "loc", spherical: true, ... } },
       { $group: { _id: null, objectsLoaded: { $sum: 1 }, maxDistance:
             { $max: "$dis" }, avgDistance: { $avg: "$dis" } } }
    
    ])

Deprecation of the repairDatabase command

Starting from MongoDB 4.2, MongoDB deprecates the repairDatabase command, the mongo shell helper function db.repairDatabase(), and the repairDatabase permission.

Alternatives:

  • To compress data on a mongod, use the compact command.

  • To rebuild indexes on a standalone instance, use the reIndex command or its helper function db.collection.reIndex().

  • To restore data for a standalone instance, use mongod --repair.

Deprecation of the getPrevError command

Starting from MongoDB 4.2, MongoDB deprecates the getPrevError command and its mongo shell helper function db.getPrevError().

Deprecation of the cloneCollection command

Starting from MongoDB 4.2, MongoDB deprecates the cloneCollection command and its mongo shell helper function db.cloneCollection().

Instead, use the mongoexport and mongoimport commands or a drive for writing a script.

Deprecation of PlanCache commands

Starting from MongoDB 4.2, MongoDB deprecates the following items:

  • The PlanCache.getPlansByQuery() method and the planCacheListPlans command.

    To get the cached query plans for a shape, use the $planCacheStats aggregation stage instead.

  • The PlanCache.listQueryShapes() method and the planCacheListQueryShapes command.

    To list the cached query shapes, use the $planCacheStats aggregation stage instead.

Aggregation

$out stage limits

$out and views

A view definition pipeline cannot contain the $out stage. If you have an existing view that contains the $out stage, you cannot create another view from the existing view.

For existing views that contain the $out stage, you can either delete and recreate a view without the $out stage or replace the view definition with a new pipeline that does not contain the $out stage.

$out and $lookup

The $lookup stage cannot include the $out stage in its nested fields.

$out and read concern levels

The $out stage cannot be used in conjunction with the "linearizable" read concern level.

$out and the explain command

If the current aggregation pipeline contains the $out stage, you cannot run the db.collection.explain() helper function or the explain command in executionStats or allPlansExecution mode.

If the current aggregation pipeline contains the $out stage, to view executionStats or allPlansExecution information, run explain without the out stage to return explain output for the preceding stages.

Alternatively, you can run explain in queryPlanner mode on the aggregation pipeline that contains the $out stage.

$out and "majority " read concern level

Staring from MongoDB 4.2, you can specify the "majority" read concern level for an aggregation operation that contains the $out stage.

Removal of the limit and num options from $geoNear

Starting from MongoDB 4.2, MongoDB removes the limit and num options from $geoNear, along with the default limits of 100 documents. To limit the results of $geoNear, use the $geoNear stage with the $limit stage.

For example, the following sample aggregation where the $geoNear stage includes the num option is no longer valid in MongoDB 4.2.

db.places.aggregate([
   {
     $geoNear: {
        near: { type: "Point", coordinates: [ -73.99279 , 40.719296 ] },
        distanceField: "distance",
        num: 5,                 // Not supported in 4.2
        spherical: true
     }
   }
])

Instead, you can rewrite the aggregation command in the following format.

db.places.aggregate([
   {
     $geoNear: {
        near: { type: "Point", coordinates: [ -73.99279 , 40.719296 ] },
        distanceField: "distance",
        spherical: true
     }
   },
   { $limit: 5 }
])

Transactions

Starting from MongoDB 4.2:

  • You cannot specify killCursors as the first operation in a transaction, or write data to capped collections in A transaction. However, you still can read data from capped collections in a transaction.

  • MongoDB removes the total transaction size limit of 16 MB . MongoDB 4.2 creates as many oplog entries as necessary to encapsulate all write operations in a transaction. Earlier versions create a single entry for all write operations in a transaction, thereby ispecifying the total transaction size limit of 16 MB.

Change streams

Availability

Starting from MongoDB 4.2, change streams are available regardless of whether the "majority" read concern level supports them.

Prior to MongoDB 4.2, change streams are available only if "majority" read concern support is enabled (default).

Default collation

Starting from MongoDB 4.2, change streams use simple binary comparisons unless an explicit collation is provided. Prior to MongoDB 4.2, change streams created for a single collection db.collection.watch() inherit the default collation of the collection.

Resume token modification

Starting from MongoDB 4.2, if a change stream aggregation pipeline modifies the _id field of an event, change streams throw an exception.

Addtion of file descriptors required for incoming connections

Starting from MongoDB 4.2, incoming connections to a mongod or mongos instance require two file descriptors. Prior to MongoDB 4.2, incoming connections only require one file descriptor.

Before upgrading from MongoDB 4.0 to 4.2, you may need to add the value of your open files ulimit setting (-n).

MongoDB tools

FIPS mode

Starting from MongoDB 4.2, MongoDB removes the --sslFIPSMode option from the following programs:

  • mongodump

  • mongoexport

  • mongofiles

  • mongoimport

  • mongorestore

  • mongostat

  • mongotop

The preceding programs use FIPS-compliant connections when the FIPS mode is enabled in the mongod and mongos instances.

Extended JSON v2

Tool

Description

bsondump

Uses the Extended JSON v2.0 (Canonical Mode) format.

mongodump

Uses the Extended JSON v2.0 (Canonical Mode) format for metadata output. Requires mongorestore V4.2 or higher to support the Extended JSON v2.0 (Canonical or Relaxed Mode) format.

Note

In general, use corresponding versions of mongodump and mongorestore. To restore data files created by a specific version of mongodump, use the corresponding version of mongorestore.

mongoexport

Generates output data in Extended JSON v2.0 (Relaxed Mode) format by default.

If --jsonFormat is set to canonical, mongoexport generates output data in the Extended JSON v2.0 (Canonical Mode) format.

mongoimport

Expects imported data to be in the Extended JSON v2.0 (Canonical or Relaxed Mode) format by default.

If the --legacy option is specified, mongoimport can recognize data in the Extended JSON v1.0 format.

Note

In general, the versions of mongoexport and mongoimport must be consistent. To import data created by mongoexport, use the corresponding version of mongoimport.

--query option

Starting from MongoDB 4.2, the --query option for mongodump and mongoexport must be in the Extended JSON v2 format (Relaxed or Strict Mode), including enclosing field names and operators in quotes. Example:

mongoexport -d=test -c=records -q='{ "a": { "$gte": 3 }, "date": { "$lt": { "$date": "2016-01-01T00:00:00.000Z" } } }' --out=exportdir/myRecords.json

Prior to MongoDB 4.2, the --query option uses the Extended JSON v1 format where field names and operators do not need to be enclosed in quotes.

mongoexport -d=test -c=records -q='{ a: { $gte: 3 }, date: { $lt: { "$date": "2016-01-01T00:00:00.000Z" } } }' --out=exportdir/myRecords.json

Replica set state changes

Primary step down

Starting from MongoDB 4.2, replSetStepDown (or replSetReconfig that causes a step down) no longer closes all client connections. However, writes that are still in progress are killed.

Prior to MongoDB 4.0, replSetStepDown closes all client connections during the step down.

ROLLBACK state

Starting fromMongoDB 4.2, MongoDB kills all user operations when a member enters the ROLLBACK state.

Retryable writes enabled in MongoDB 4.2-compatible drivers by default

Starting from MongoDB 3.6, MongoDB supports retryable writes, but most official MongoDB 3.6 and 4.0-compatible drivers disable this feature by default. For these drivers, retryable writes can be enabled for connections by specifying the retryWrites=true option in connection strings.

Official MongoDB 4.2 and later-compatible drivers enable retryable writes by default. Applications upgraded to MongoDB 4.2-compatible drivers that require retryable writes may omit the retryWrites=true option. To disable retryable writes, you must include the retryWrites=false option in connection strings.

Note

The local database does not support retryable writes. Applications that write data to the local database encounter write errors upon upgrading to MongoDB 4.2-compatible drivers unless retryable writes are explicitly disabled.

General changes

Indexes

Limits on the reIndex command

Starting from MongoDB 4.2, MongoDB imposes stricter limits on the reIndex command or the db.collection.reIndex() function to disallow reIndex and db.collection.reIndex() on a mongos.

Limits on db.collection.dropIndex()

You cannot use db.collection.dropIndex() to drop all non-_id indexes. Use db.collection.dropIndexes() instead.

Error message for creating duplicate indexes

Starting from MongoDB 4.2, the createIndexes command and the mongo shell helper functions db.collection.createIndex() and db.collection.createIndexes() return an error if you create an index with one name and then try to create the same index again with another name.

{
   "ok" : 0,
   "errmsg" : "Index with name: x_1 already exists with a different name",
   "code" : 85,
   "codeName" : "IndexOptionsConflict"
}

If you perform this operation in MongoDB 4.0 and earlier versions, MongoDB does not create the index again but still returns a message that includes the following information.

{
   "numIndexesBefore" : 2,
   "numIndexesAfter" : 2,
   "note" : "all indexes already exist",
   "ok" : 1
}

Hashed indexes on PowerPC

For hashed indexes, MongoDB 4.2 ensures that the hash value of the floating-point value 263 on PowerPC is consistent with other platforms. Prior to MongoDB 4.2, the hash value of the floating-point value 263 on PowerPC is inconsistent with other platforms.

Although hashed indexes on a field with floating-point values greater than 253 are unsupported settings, clients can still insert documents where the indexed filed has the value 263.

If a sharded cluster instance that runs MongoDB 4.0 on PowerPC has a shard key with hash value 263, additional considerations must be taken before upgrading the instance to MongoDB 4.2.

min()/max()

Starting from MongoDB 4.2, when specifying min()/max() for a db.collection.find() operation, you must explicitly specify an index for min()/max() unless the find() query is an equality condition on the _id field { _id: }.

Similarly, when specifying min/max in the find command, you must also explicitly specify the hint for the min/max index.

Prior to MongoDB 4.0, you can choose whether to explicitly specify the min()/max() index regardless of the query conditions. If you do not explicitly specify a hint in MongoDB 4.0 and earlier versions, MongoDB selects an index based on the index boundaries. However, if multiple indexes exist on the same fields but with different sorting orders, the index selection may be ambiguous.

CurrentOp

When reporting "getmore" operations, the $currentOp aggregation stage, the currentOp command, and db.currentOp() now return the originatingCommand field as a nested field of the new cursor field. Prior to MongoDB 4.2, originatingCommand is a top-level field in the "getmore" document.

Server status

The opcounters and opcountersRepl metrics returned by serverStatus and db.serverStatus are now 64-bit integers (NumberLong) instead of 32-bit integers (NumberInt).

Logging

  • When logging to syslog, the message text format now includes the component information. Example:

    ...  ACCESS   [repl writer worker 5] Unsupported modification to roles collection ...

    In versions earlier than MongoDB 4.2, syslog message texts do not include the component information. Example:

    ... [repl writer worker 1] Unsupported modification to roles collection ...
  • Starting from MongoDB 4.2, log retrieving commands truncate any events that contain more than 1,024 characters. In versions earlier than MongoDB 4.2, the commands truncate after 512 characters.

  • Starting from MongoDB 4.2, MongoDB records the debug log level. For example, if the log level is 2, MongoDB records D2.

    Prior to MongoDB 4.2, MongoDB log messages only specifies D as the debug level.

Wire protocol

Starting from MongoDB 4.2, MongoDB no longer supports OP_COMMAND and the corresponding OP_COMMANDREPLY write protocol.

Killcursors changes

Transactions

Starting from MongoDB 4.2, you cannot specify killCursors as the first operation in a transaction.

Permissions

Starting from MongoDB 4.2, you can always terminate your cursors, regardless of whether you has the killCursors permission. Therefore, the killCursors permission is no longer effective in MongoDB 4.2 and later versions.

In MongoDB 3.6.3 to 4.0.x, you require the killCursors permission to terminate your cursors when access control is enabled.

Removal of the AsyncRequestsSenderUseBaton parameter

Starting from MongoDB 4.2, MongoDB removes the AsyncRequestsSenderUseBaton parameter and always enables performance enhancement controlled by this parameter.

Stricter validation of the count syntax

Starting from MongoDB 4.2, MongoDB implements a stricter validation of option names for the count command. If an unknown option name is specified, the command reports an error.

Prior to MongoDB 4.2, MongoDB ignores invalid option names.

Causally consistent sessions

Starting from MongoDB 4.2, the following commands no longer support afterClusterTime:

  • dbHash command

  • mapReduce command

  • validate command

Therefore, these operations cannot be associated with causally consistent sessions.

Removal of the fastmodinsert metric

MongoDB 4.2 removes the deprecated fastmodinsert metric from various outputs, including the explain executionStats and the profiler output.

Map-Reduce

Starting from MongoDB 4.2, MongoDB deprecates the following items:

  • The map-reduce option to create a sharded collection and the support for the use of the sharded option for map-reduce. To output to a sharded collection, create a sharded collection first. MongoDB 4.2 also deprecates the replacement of an existing sharded collection.

  • Explicit specification of the nonAtomic: false option for map-reduce.

Balancer status and auto splitting

Starting from MongoDB 4.2:

  • The balancerStart command and mongo shell helper methods sh.startBalancer() and sh.setBalancerState(true) also enable auto splitting.

    To disable auto splitting, use sh.disableAutoSplit().

  • The balancerStop command and mongo shell helper methods sh.stopBalancer() and sh.setBalancerState(false) also disable auto splitting.

    To enable auto splitting, use sh.enableAutoSplit().

The sh.enableBalancing(namespace) and sh.disableBalancing(namespace) methods no longer affect auto splitting.

Lock diagnostics reports

Starting from MongoDB 4.2, MongoDB reports information about the ReplicationStateTransition lock.

Additionally, MongoDB 4.2 separates ParallelBatchWriterMode lock information from global lock information. Earlier versions report ParallelBatchWriterMode lock information as part of the global lock information.

For operations that report lock information, see:

  • The serverStatus command and db.serverStatus() method.

  • The $currentOp aggregation pipeline stage, the currentOp command, and the db.currentOp() method.

Validation of query, sorting, or projection argument for findAndModify

Starting from MongoDB 4.2 (including V4.0.12+ and 3.6.14+), the findAndModify command and its associated mongo shell methods report an error when the specified query, sorting, or projection argument is not a document.

pRIOR TO MongoDB 4.2, the current operation considers a non-document query or sorting argument as an empty document {}.

DropDatabase and movePrimary

Starting from MongoDB 4.2:

  • If you drop a database and create another database with the same name, you must perform one of the following operations:

    • Restart all mongos instances and shard members, or

    • Run the flushRouterConfig command on all mongos instances and shard members before reading from or writing to the created database.

  • If you use the movePrimary command to move unsharded collections, you must perform one of the following operations:

    • Restart all mongos instances and shard members, or

    • Run the flushRouterConfig command on all mongos instances and shard members before reading from or writing to those collections.

This ensures that mongos and shard members flush their metadata caches. Otherwise, reads may miss data and writes might not go to the correct shard. Manual intervention is required for restoration.

Prior to MongoDB 4.2, you need only to restart mongos instances or run the flushRouterConfig command on mongos instances.

Removal of system.indexes and system.namespaces collections

Starting from MongoDB 4.2, MongoDB removes the system.indexes and system.namespaces collections (deprecated in MongoDB 3.0).

With the removal of these collections, built-in roles clusterManager, clusterMonitor, dbAdmin, read, restore, and other roles that inherit from these roles no longer are granted permissions to access the system.indexes and system.namespaces collections.

Necessarity of clearing data directories for downgrading arbiter

The arbiter data files in MongoDB 4.2 are incompatible with MongoDB 4.0. To downgrade from MongoDB 4.2 to 4.0, delete the arbiter data files first. An error may occur if you use data files in MongoDB 4.2 to run a arbiter in MongoDB 4.0.

Sharded collections and document replacement

Starting from MongoDB 4.2:

  • A document replacement operation such as replaceOne() or update() (when used with a replacement document) first attempts to locate a shard by using the query filter. If the operation cannot locate a shard by using the query filter, it attempts to utilize the replacement document. Prior to MongoDB 4.2, a document replacement operation only attempts to locate a shard by using the replacement document.

  • MongoDB deprecates the save() method. Use the insertOne() or replaceOne() methods instead. The save() method cannot be used on sharded collections that are not sharded by-_id. If you use the save() method, an error occurs.

  • For a document replacement operation with the setting upsert: true on a sharded collection, the filter must include an equality match on the shard key for the sharded collection.

MongoDB 4.2 feature compatibility

Some features in MongoDB 4.2 require not only 4.2 binaries but also the feature compatibility version (fCV) set to 4.2. Features:

  • Distributed transactions.

  • Removal of the index key length limits for MongoDB versions with fCV set to 4.2+. Additionally, the failIndexKeyTooLong parameter has no effect for MongoDB versions with fCV set to 4.2+, and only applies to MongoDB versions 2.6 to 4.0.

  • Removal of the index name length limits for MongoDB versions with fCV set to 4.2+.

  • New internal format for unique indexes. This new format applies to all existing unique indexes and newly created or rebuilt unique indexes.

  • Starting from MongoDB 4.2, you can no longer use type:0 as a synonym for exists:false.

  • MongoDB 4.2 adds wildcard indexes to support workloads where you query custom fields or various different fields exist in a collection.