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

4.10

4.2

4.10

4.2

4.10

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 on your connection by specifying the algorithms in the following ways:

  • Adding the compressors parameter to your ConnectionString instance

  • Calling the compressorList() method from the MongoClientSettings builder

To enable compression on your connection in a ConnectionString instance, specify the compressors parameter. You can specify one or more of the following values for the compressors parameter:

The following example shows how to specify Snappy, Zlib, and Zstandard as the compression algorithms for a connection:

// Replace the placeholders with values from your MongoDB deployment's connection string
val connectionString = ConnectionString("mongodb+srv://<db_username>:<db_password>@<cluster-url>/?compressors=snappy,zlib,zstd")
// Create a new client with your settings
val mongoClient = MongoClient.create(connectionString)

To enable compression using within your MongoClientSettings, call the compressorList() builder method and pass one or more MongoCompressor instances as a parameter.

You can specify compression algorithms by calling the following methods from MongoCompressor:

  • createSnappyCompressor() for Snappy compression

  • createZlibCompressor() for Zlib compression

  • createZstdCompressor() for Zstandard compression

The following example shows how to specify Snappy, Zlib, and Zstandard as the compression algorithms for a connection:

// Replace the placeholder with your MongoDB deployment's connection string
val uri = "<connection string>"
val settings = MongoClientSettings.builder()
.applyConnectionString(ConnectionString(uri))
.compressorList(
listOf(
MongoCompressor.createSnappyCompressor(),
MongoCompressor.createZlibCompressor(),
MongoCompressor.createZstdCompressor())
)
.build()
// Create a new client with your settings
val mongoClient = MongoClient.create(settings)