Docs Menu
Docs Home
/ / /
Java Reactive Streams Driver
/ /

Compression

The Java Reactive Streams driver supports compression of messages to and from MongoDB servers. The driver implements the three algorithms that are supported by MongoDB servers:

The driver will negotiate which, if any, compression algorithm is used based on capabilities advertised by the server in the hello command response.

Include the following import statements:

import com.mongodb.ConnectionString;
import com.mongodb.reactivestreams.client.MongoClients;
import com.mongodb.reactivestreams.client.MongoClient;

To specify compression within a ConnectionString, specify compressors as part of the connection string.

The following code specifies the Snappy compression algorithm:

ConnectionString connectionString =
new ConnectionString("mongodb://localhost/?compressors=snappy");
MongoClient mongoClient = MongoClients.create(connectionString);

The following code specifies the Zlib compression algorithm:

ConnectionString connectionString =
new ConnectionString("mongodb://localhost/?compressors=zlib");
MongoClient mongoClient = MongoClients.create(connectionString);

The following code specifies the Zstandard compression algorithm:

ConnectionString connectionString =
new ConnectionString("mongodb://localhost/?compressors=zstd");
MongoClient mongoClient = MongoClients.create(connectionString);

The following code specifies multiple compression algorithms:

ConnectionString connectionString =
new ConnectionString("mongodb://localhost/?compressors=snappy,zlib,zstd");
MongoClient mongoClient = MongoClients.create(connectionString);

In all cases, the driver uses the first compressor in the list for which the server has support.

Include the following import statements:

import com.mongodb.MongoClientSettings;
import com.mongodb.MongoCompressor;
import java.util.Arrays;

To specify compression within a MongoClientSettings instance, set the compressors property to a list of MongoCompressor instances.

The following code specifies the Snappy compression algorithm:

MongoClientSettings settings = MongoClientSettings.builder()
.compressorList(Arrays.asList(MongoCompressor.createSnappyCompressor()))
.build();
MongoClient client = MongoClients.create(settings);

The following code specifies the Zlib compression algorithm:

MongoClientSettings settings = MongoClientSettings.builder()
.compressorList(Arrays.asList(MongoCompressor.createZlibCompressor()))
.build();
MongoClient client = MongoClients.create(settings);

The following code specifies the Zstandard compression algorithm:

MongoClientSettings settings = MongoClientSettings.builder()
.compressorList(Arrays.asList(MongoCompressor.createZstdCompressor()))
.build();
MongoClient client = MongoClients.create(settings);

The following code specifies multiple compression algorithms:

MongoClientSettings settings = MongoClientSettings.builder()
.compressorList(Arrays.asList(MongoCompressor.createSnappyCompressor(),
MongoCompressor.createZlibCompressor(),
MongoCompressor.createZstdCompressor()))
.build();
MongoClient client = MongoClients.create(settings);

As with configuration that uses a URI, the driver uses the first compressor in the list for which the server has support.

As the JDK has no built-in support for Snappy or Zstandard, the driver takes a dependency on existing open-source Snappy and Zstandard implementations. See the snappy-java GitHub repository and the zstd-java GitHub repository for details.

Back

Authentication

On this page