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:
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.
Specify Compression Algorithms
To enable compression for the connection to your MongoDB instance, specify the algorithms you want to use in one of the following ways:
Add the algorithms to your connection string as a parameter
Specify the algorithms in the
Compressors
property of yourMongoClientSettings
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);