Docs Menu
Docs Home
/ / /
Rust Driver
/ /

Network Compression

On this page

  • Overview
  • Add Compression Feature Flags
  • Enable Network Compression
  • Connection String
  • ClientOptions
  • Additional Information
  • API Documentation

In this guide, you can learn how to configure network compression for your connection to MongoDB by using the Rust driver.

Network compression is a feature that allows you to compress and decompress messages sent between your application and MongoDB, reducing the total amount of data passed over the network.

The driver supports the following compressors:

  1. Snappy: available in MongoDB 3.4 and later

  2. Zlib: available in MongoDB 3.6 and later

  3. Zstandard: available in MongoDB 4.2 and later


Compressor Selection

If you specify multiple compressors to use on your connection, the driver selects the first one that is supported by the MongoDB instance that the driver is connected to.

This guide includes the following sections:

  • Add Compression Feature Flags describes how to add feature flags to your application for different compressors

  • Enable Network Compression describes how to enable network compression on your Client instance

  • Additional Information provides links to resources and API documentation for types and methods mentioned in this guide

To use a compressor, add the relevant feature flag to your mongodb dependency's feature list in your project's Cargo.toml file.

Select the tab for your preferred compressor to see how to add the necessary feature flag to your mongodb dependency:


To specify multiple compressors, you must add the feature flag for each compressor to your mongodb dependency.

You can enable compression on your Client instance by specifying compressors in the following ways:

  • Adding the compressors parameter to your connection string. To see an example that enables compression this way, see the Connection String section.

  • Setting the compressors field of a ClientOptions instance. You can then pass the options to the with_options() method when instantiating a Client. To see an example that enables compression this way, see the ClientOptions section.

To enable compression by using a connection string, specify the compressors parameter. You can specify one or more of the following values for the compressors parameter:

  • "snappy" for Snappy compression

  • "zlib" for Zlib compression

  • "zstd" for Zstandard compression

The following example shows how to specify Snappy, Zlib, and Zstandard as the compressors for a connection:

let uri = "mongodb+srv://<user>:<password>@<cluster-url>/?compressors=snappy,zlib,zstd";
let client = Client::with_uri_str(uri).await?;

To learn more about setting client options, see the guide on Connection Options.

To enable compression within your ClientOptions instance, set the compressors field, and then pass the options when creating a client.

The compressors field takes a value of type Vec<Compressor>. The Compressor type has the following possible values:

  • Compressor::Snappy

  • Compressor::Zstd { level: <integer> }

  • Compressor::Zlib { level: <integer> }

For the compressors that have a level field, set the value to None to indicate the default level. The following table describes the default and accepted compression levels for Zlib and Zstandard:

Default Level
Accepted Levels
Integers from 0 to 9 or None
Integers from 1 to 22 or None

A higher level value results in more compression, which is slower.

The following example shows how to specify Snappy, Zlib, and Zstandard as the compressors for a connection:

let uri = "<connection string>";
let mut client_options = ClientOptions::parse_async(uri).await?;
let compressors = vec![
Compressor::Zstd { level: Some(1) },
Compressor::Zlib { level: None }
client_options.compressors = Some(compressors);
let client = Client::with_options(client_options)?;

For more information about the concepts in this guide, see the following documentation:

To learn more about any of the methods or types mentioned in this guide, see the following API documentation:

← Connection Options