Docs Menu
Docs Home
/ / /
C#/.NET
/ /

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 .NET/C# Driver supports the following compression algorithms:

  1. Snappy: available in MongoDB 3.6 and later.

  2. Zlib: available in MongoDB 3.6 and later.

  3. 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.

To enable compression for the connection to your MongoDB instance, specify the algorithms you want to use in one of the following ways:

  1. Add the algorithms to your connection string as a parameter

  2. Specify the algorithms in the Compressors property of your MongoClientSettings object

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

const string connectionUri =
"mongodb+srv://<db_username>:<db_password>@<cluster-url>/?compressors=snappy,zlib,zstd";
var client = new MongoClient(connectionUri);

To enable compression by using MongoClientSettings, set the Compressors property of your MongoClientSettings object to a List of one or more CompressorConfiguration objects. Each CompressorConfiguration object in the List represents an algorithm you want to use:

var settings = new MongoClientSettings()
{
Scheme = ConnectionStringScheme.MongoDB,
Server = new MongoServerAddress("<cluster-url>"),
Compressors = new List<CompressorConfiguration>()
{
new CompressorConfiguration(CompressorType.Snappy),
new CompressorConfiguration(CompressorType.Zlib),
new CompressorConfiguration(CompressorType.Zstandard)
}
};
var client = new MongoClient(settings);

Back

Configure TLS