Overview
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.
Compression Algorithm Dependencies
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.3com.github.luben:zstd-jni:1.5.5-3
To learn more, see the snappy-java and zstd-jni GitHub pages.
Specify Compression Algorithms
You can enable compression for the connection to your MongoDB instance by specifying the algorithms in one of the following ways:
In the
compressorsparameter of your connection stringIn the
compressorListmethod chained to theMongoClientSettings.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)
API Documentation
To learn more about any of the methods or types discussed in this guide, see the following API documentation: