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 following table lists the minimum supported driver and MongoDB Server version for each compression algorithm:

Compression Algorithm
Minimum Driver Version
Minimum Server Version

1.7

4.2

1.7

4.2

1.15

4.2

To enable network compression for your MongoDB connection, build the C driver with the required dependencies and specify the compression algorithms that you want to use.

The following table lists each algorithm and the dependency it requires, if any:

Compression Algorithm
Dependency

Snappy

Install the Snappy library. To view installation instructions, see the snappy GitHub repository.

Zlib

None. The driver bundles Zlib by default.

Zstandard

Install the Zstandard library. To view installation instructions, see the zstd GitHub repository.

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

  • Add the compressors parameter to your connection string

  • Set the compressors on a mongoc_uri_t struct

Select the Connection String or URI Option tab to view an example that specifies all available compressors:

client = mongoc_client_new("mongodb://<db_username>:<db_password>@<hostname>/?compressors=snappy,zlib,zstd");
uri = mongoc_uri_new("mongodb://localhost:27017");
mongoc_uri_set_compressors(uri, "snappy,zlib,zstd");
client = mongoc_client_new_from_uri(uri);

When you use the Zlib algorithm, you can specify a compression level from -1 to 9. Lower levels generally provide faster compression with a lower compression ratio, and higher levels provide slower compression with a higher compression ratio.

Select the Connection String or URI Option tab to view an example that specifies a compression level for the zlib compressor:

client = mongoc_client_new("mongodb://<db_username>:<db_password>@<hostname>/?compressors=zlib&zlibCompressionLevel=<zlib_compression_level");
uri = mongoc_uri_new("mongodb://localhost:27017");
mongoc_uri_set_option_as_int32(uri, MONGOC_URI_ZLIBCOMPRESSIONLEVEL, <zlib-compression-level>);
client = mongoc_client_new_from_uri(uri);

For more information about the types and functions used on this page, see the following API documentation: