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 Scala driver provides a connection option to compress messages, which reduces the amount of data passed over the network between MongoDB and your application.

The driver supports the following algorithms:

If you specify multiple compression algorithms, the driver selects the first one in the list that your MongoDB instance supports.

The Java Development Kit (JDK) supports Zlib compression. However, Snappy and Zstandard depend on open source Java implementations, and you must add explicit dependencies for those algorithms. The driver tests against the following versions:

  • org.xerial.snappy:snappy-java:1.1.10.3

  • com.github.luben:zstd-jni:1.5.5-3

To learn more, see the snappy-java and zstd-jni GitHub pages.

You can enable compression for the connection to your MongoDB instance by specifying the algorithms in one of the following ways:

  • 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. Select the Connection String or MongoClientSettings tab to see the corresponding syntax:

val mongoClient = MongoClient(
"mongodb://<hostname>:<port>/?compressors=snappy,zlib,zstd"
)
val settings = MongoClientSettings.builder()
.applyConnectionString(ConnectionString("<connection string>"))
.compressorList(List(
MongoCompressor.createSnappyCompressor(),
MongoCompressor.createZlibCompressor(),
MongoCompressor.createZstdCompressor()
).asJava)
.build()
val mongoClient = MongoClient(settings)

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