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

3.0

4.2

3.0

4.2

4.7

4.2

To enable network compression for your MongoDB connection, install the required dependencies and specify the compression algorithms you want to use.

The following table lists each algorithm and the required dependencies, if any:

Compression Algorithm
Dependencies

Snappy

Add the snappy package to your application. To install, run the npm install --save snappy command.

Zlib

None

Zstandard

Add the @mongodb-js/zstd package to your application. To install, run the npm install --save @mongodb-js/zstd command.

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

  1. Adding the parameter to your connection string.

  2. Specifying the compressors option in your MongoClientOptions.

To enable compression using the connection string, add the compressors parameter in the connection string. You can specify one or more compression algorithms, separating them with commas:

const uri =
"mongodb+srv://<user>:<password>@<cluster-url>/?compressors=snappy,zlib";
const client = new MongoClient(uri);

To enable compression using the MongoClientOptions, pass the compressors option and the compression algorithm you want to use. You can specify one or more compression algorithms, separating them with commas:

const uri =
"mongodb+srv://<user>:<password>@<cluster-url>";
const client = new MongoClient(uri,
{
compressors: ["snappy"]
});

Specify compression algorithms using the following strings: