For AI agents: a documentation index is available at https://www.mongodb.com/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.
Docs Menu

Compress Network Traffic

The following table lists the minimum supported driver and MongoDB Server version for each compression algorithm:

Compression Algorithm
Minimum Driver Version
Minimum Server Version

3.6

4.2

3.6

4.2

3.11

4.2

To enable network compression for your MongoDB connection, install the required dependencies and specify the compression algorithms that you want to use.

The following table lists each algorithm and the dependency it requires, if any:

Compression Algorithm
Dependency

Snappy

Download snappy-java, a Java JNI wrapper for the Snappy library. To install, see snappy-java in Maven Central.

Zlib

None. The driver natively supports Zlib compression.

Zstandard

Download zstd-jni, a Java JNI wrapper for the Zstandard library. To install, see zstd-jni in Maven Central.

You can enable compression for the connection to your MongoDB instance by specifying the algorithms in one of two ways. Select the Connection String or MongoClientSettings tab to see the corresponding syntax:

  • In the compressors parameter of your connection string

  • In the compressorList method chained to the MongoClientSettings.builder() method

The following example shows how to specify all compression algorithms:

ConnectionString connectionString = new ConnectionString(
"mongodb+srv://<db_username>:<db_password>@<cluster-url>/?compressors=snappy,zlib,zstd");
MongoClient client = MongoClients.create(connectionString);
MongoClientSettings settings = MongoClientSettings.builder()
.compressorList(Arrays.asList(MongoCompressor.createSnappyCompressor(),
MongoCompressor.createZlibCompressor(),
MongoCompressor.createZstdCompressor()))
.build();
MongoClient client = MongoClients.create(settings);

To learn more about any of the methods or types discussed in this guide, see the following API documentation: