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

Compression

On this page

  • Specify Compression By Using ConnectionString
  • Specify Compression By Using MongoClientSettings
  • Dependencies

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:

  • Snappy: Snappy compression can be used when connecting to MongoDB servers running version 3.4 and later.

  • Zlib: Zlib compression can be used when connecting to MongoDB servers running version 3.6 and later.

  • Zstandard: Zstandard compression can be used when connecting to MongoDB servers running version 4.2 and later.

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

Next

Databases and Collections