Latest Pre-release in branch 4.0
4.0.28
Released 04 Jan 2022
(4 years ago)
SoftwareMongoDB Server
Version4.0
Initial release4.0.0
21 Jun 2018
(8 years ago)
Latest release4.0.28-rc0
04 Jan 2022
(4 years ago)
End of life30 Apr 2022
(Ended 4 years ago)
Release noteshttps://www.mongodb.com/docs/manual/release-notes/4.0/
Documentationhttps://www.mongodb.com/docs/v4.0/
Downloadhttps://www.mongodb.com/try/download/community
MongoDB Server 4.0 ReleasesView full list

What Is New in MongoDB 4.0

MongoDB 4.0 is a major release that introduced significant enhancements in transactions, sharding, and tooling. It marks a pivotal step towards unifying the developer experience across the entire database.

Category Key Changes
Multi-Document ACID Transactions Support for transactions across multiple documents, collections, and databases.
Sharding Improvements Refreshed sharded cluster metadata, faster balancer, and default sharding on UUIDs.
Aggregation & Query Enhancements New operators like $toDouble, $convert, and support for type expressions in $type.
Security Audit log filtering by user and role, and SCRAM-SHA-256 for enhanced authentication.
Tooling & Ops MongoDB Shell (mongosh) improvements, new mongod parameters, and enhanced mongodump output.

How do transactions work in MongoDB 4.0?

MongoDB 4.0 delivers full multi-document ACID transactions. This is the headline feature that allows you to perform complex operations with atomicity guarantees across your entire dataset.

In practice, you can now wrap operations on multiple documents, even across different collections and databases, within a single transaction. The syntax is familiar, using session.startTransaction(), session.commitTransaction(), and session.abortTransaction(). This matters because it brings MongoDB's consistency model in line with traditional relational databases for complex workflows.

Code Example

const session = db.getMongo().startSession();
session.startTransaction();
try {
    db.accounts.updateOne( { _id: 1 }, { $inc: { balance: -100 } }, { session } );
    db.accounts.updateOne( { _id: 2 }, { $inc: { balance: 100 } }, { session } );
    session.commitTransaction();
} catch (error) {
    session.abortTransaction();
}
session.endSession();

What sharding improvements were made?

Sharding got a major performance and reliability overhaul. The balancer is now more efficient, and the config server metadata management was completely refreshed.

A key change is the new way sharded clusters track metadata. Instead of a single source of truth, the config servers now act as a replica set, which simplifies failover and improves consistency. The balancer also got smarter, making chunk migrations faster and less intrusive on cluster performance.

For developers, a subtle but important change is that new collections shard on UUIDs (_id) by default instead of ObjectIds. This prevents potential hotspots on inserts when using monotonically increasing shard keys.

What new aggregation features are available?

The aggregation framework received powerful new type conversion and expression operators. This makes data transformation pipelines more robust and flexible.

The new $convert operator and its helper aliases like $toDouble or $toString allow you to handle type casting directly within your aggregation stages. You can specify on-error and on-null behavior, which is crucial for cleaning up messy data. The $type operator was also enhanced to return the BSON type directly as a string within an expression.

Code Example

// Convert a field to a double, handling errors
db.sales.aggregate([
    {
        $project: {
            price: { $toDouble: "$priceString" },
            quantity: { $convert: {
                input: "$qty",
                to: "int",
                onError: 0,
                onNull: 0
            }}
        }
    }
])

Were there any security enhancements?

Yes, security was tightened with more granular audit logging and a stronger default authentication mechanism.

You can now filter the audit log output by specific users or roles, which makes it much easier to track actions for compliance without being overwhelmed by noise. Additionally, while SCRAM-SHA-1 is still supported, SCRAM-SHA-256 became available, offering a more secure hashing algorithm for password storage.

FAQ

Are transactions in MongoDB 4.0 exactly like in SQL databases?
They provide the same ACID guarantees but are implemented with a timeout (default 60 seconds) to prevent long-running operations from impacting performance. You must design your application logic with this timeout in mind.

Do I have to use the new UUID default for sharded collections?
No, it's just the new default. You can still explicitly specify a different shard key when creating a collection if your use case requires it.

Can I use transactions on a standalone MongoDB instance?
No. Multi-document transactions are only supported in replica set deployments, as they rely on the replication protocol for atomicity and durability.

What happens if my aggregation type conversion fails with $convert?
That's the beauty of it. You define the failure behavior. You can specify an onError value to substitute in case of a conversion error, or an onNull value if the input is null, keeping your pipeline from breaking.

Is it difficult to upgrade from 3.6 to 4.0?
The upgrade process is straightforward but has prerequisites. Your featureCompatibilityVersion must be set to "3.6" first, and you must upgrade through the correct version sequence (e.g., 3.4 -> 3.6 -> 4.0). Always test the upgrade in a staging environment first.

Releases In Branch 4.0

VersionRelease date
4.0.28-rc004 Jan 2022
(4 years ago)
4.0.2804 Jan 2022
(4 years ago)
4.0.27-rc030 Aug 2021
(5 years ago)
4.0.2730 Aug 2021
(5 years ago)
4.0.26-rc016 Jul 2021
(5 years ago)
4.0.2616 Jul 2021
(5 years ago)
4.0.25-rc104 Jun 2021
(5 years ago)
4.0.2504 Jun 2021
(5 years ago)
4.0.25-rc002 Jun 2021
(5 years ago)
4.0.24-rc113 Apr 2021
(5 years ago)
4.0.2413 Apr 2021
(5 years ago)
4.0.24-rc005 Apr 2021
(5 years ago)
4.0.23-rc009 Feb 2021
(5 years ago)
4.0.2309 Feb 2021
(5 years ago)
4.0.22-rc015 Dec 2020
(5 years ago)
4.0.2215 Dec 2020
(5 years ago)
4.0.21-rc116 Oct 2020
(5 years ago)
4.0.2116 Oct 2020
(5 years ago)
4.0.21-rc005 Oct 2020
(6 years ago)
4.0.20-rc007 Aug 2020
(6 years ago)
4.0.2007 Aug 2020
(6 years ago)
4.0.19-rc002 Jun 2020
(6 years ago)
4.0.1902 Jun 2020
(6 years ago)
4.0.18-rc006 Apr 2020
(6 years ago)
4.0.1806 Apr 2020
(6 years ago)
4.0.17-rc009 Mar 2020
(6 years ago)
4.0.1709 Mar 2020
(6 years ago)
4.0.16-rc029 Jan 2020
(6 years ago)
4.0.1629 Jan 2020
(6 years ago)
4.0.15-rc016 Jan 2020
(6 years ago)
4.0.1516 Jan 2020
(6 years ago)
4.0.14-rc111 Dec 2019
(6 years ago)
4.0.1411 Dec 2019
(6 years ago)
4.0.14-rc010 Dec 2019
(6 years ago)
4.0.13-rc002 Oct 2019
(7 years ago)
4.0.1302 Oct 2019
(7 years ago)
4.0.12-rc208 Aug 2019
(7 years ago)
4.0.1208 Aug 2019
(7 years ago)
4.0.12-rc106 Aug 2019
(7 years ago)
4.0.12-rc031 Jul 2019
(7 years ago)
4.0.11-rc019 Jul 2019
(7 years ago)
4.0.1119 Jul 2019
(7 years ago)
4.0.10-rc124 May 2019
(7 years ago)
4.0.1024 May 2019
(7 years ago)
4.0.10-rc022 May 2019
(7 years ago)
4.0.9-rc004 Apr 2019
(7 years ago)
4.0.904 Apr 2019
(7 years ago)
4.0.8-rc027 Mar 2019
(7 years ago)
4.0.827 Mar 2019
(7 years ago)
4.0.7-rc120 Mar 2019
(7 years ago)
4.0.720 Mar 2019
(7 years ago)
4.0.7-rc014 Mar 2019
(7 years ago)
4.0.6-rc130 Jan 2019
(7 years ago)
4.0.630 Jan 2019
(7 years ago)
4.0.6-rc023 Jan 2019
(7 years ago)
4.0.5-rc113 Dec 2018
(7 years ago)
4.0.513 Dec 2018
(7 years ago)
4.0.5-rc011 Dec 2018
(7 years ago)
4.0.4-rc205 Nov 2018
(7 years ago)
4.0.405 Nov 2018
(7 years ago)
4.0.4-rc101 Nov 2018
(7 years ago)
4.0.4-rc030 Oct 2018
(7 years ago)
4.0.3-rc025 Sep 2018
(8 years ago)
4.0.325 Sep 2018
(8 years ago)
4.0.2-rc024 Aug 2018
(8 years ago)
4.0.224 Aug 2018
(8 years ago)
4.0.1-rc127 Jul 2018
(8 years ago)
4.0.127 Jul 2018
(8 years ago)
4.0.1-rc023 Jul 2018
(8 years ago)
4.0.021 Jun 2018
(8 years ago)
4.0.0-rc720 Jun 2018
(8 years ago)
4.0.0-rc619 Jun 2018
(8 years ago)
4.0.0-rc512 Jun 2018
(8 years ago)
4.0.0-rc407 Jun 2018
(8 years ago)
4.0.0-rc306 Jun 2018
(8 years ago)
4.0.0-rc204 Jun 2018
(8 years ago)
4.0.0-rc130 May 2018
(8 years ago)
4.0.0-rc021 May 2018
(8 years ago)