Network Compression
You can enable a driver option to compress messages, which reduces the amount of data passed over the network between MongoDB and your application.
The driver supports the following compression algorithms:
Snappy: available in MongoDB 3.6 and later.
Zlib: available in MongoDB 3.6 and later.
Zstandard: available in MongoDB 4.2 and later.
If you specify multiple compression algorithms, the driver selects the first one in the list supported by your MongoDB instance.
Note
When using the Snappy or Zstandard compression algorithm, you must add explicit dependencies.
Specify Compression Algorithms
You can enable compression for the connection to your MongoDB instance by specifying the algorithms in one of two ways:
Adding the parameter to your connection string.
Specifying the
compressors
option in yourMongoClientOptions
.
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 along with 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:
Compression Algorithm Dependencies
To add the Snappy compression algorithm to your application, run the following code:
npm install --save snappy
To add the Zstandard compression algorithm to your application, run the following code:
npm install --save @mongodb-js/zstd