Version Compatibility
The following table lists the minimum supported driver and MongoDB Server version for each compression algorithm:
Enable Network Compression
To enable network compression for your MongoDB connection, install the required dependencies and specify the compression algorithms that you want to use.
Compression Algorithm Dependencies
The following table lists each algorithm and the dependency it requires, if any:
Compression Algorithm | Dependency |
|---|---|
Snappy | Download |
Zlib | None. The driver natively supports Zlib compression. |
Zstandard | Download |
Specify Compression Algorithms
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
compressorsparameter of your connection stringIn the
compressorListmethod chained to theMongoClientSettings.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);
API Documentation
To learn more about any of the methods or types discussed in this guide, see the following API documentation: