- Reference
- Driver
- Reading and Writing
- Compression
Compression
The C# driver supports compression of messages to and from MongoDB servers. The driver implements the three algorithms that are supported by MongoDB servers:
- Snappy: Snappy compression can be used when connecting to MongoDB servers starting with the 3.4 release. Supported platforms: Windows: x86 and x64, Linux/macOS: x64.
- Zlib: Zlib compression can be used when connecting to MongoDB servers starting with the 3.6 release. Supported on all platforms and architectures.
- Zstandard: Zstandard compression can be used when connecting to MongoDB servers starting with the 4.2 release. Supported platforms: Windows/Linux/macOS: x64 only.
The driver will negotiate which, if any, compression algorithm is used based on capabilities advertised by the server in the hello or legacy hello command response.
Specify compression via Connection String
using MongoDB.Driver;
To specify compression with ConnectionString
, just add compressors
into the connection string, as in:
var connectionString = "mongodb://localhost/?compressors=snappy";
var client = new MongoClient(connectionString);
for Snappy compression, or
var connectionString = "mongodb://localhost/?compressors=zlib";
var client = new MongoClient(connectionString);
for zlib compression, or
var connectionString = "mongodb://localhost/?compressors=zstd";
var client = new MongoClient(connectionString);
for Zstandard compression, or
var connectionString = "mongodb://localhost/?compressors=snappy,zlib,zstd";
var client = new MongoClient(connectionString);
to configure multiple compressors.
In all cases the driver will use the first compressor in the list for which the server advertises support.
Additionally, zlib compression allows specifying a compression level with supported values between -1 and 9:
var connectionString = "mongodb://localhost/?compressors=zlib;zlibcompressionlevel=6";
var client = new MongoClient(connectionString);
Specify compression via MongoClientSettings
using MongoDB.Driver;
using MongoDB.Driver.Core.Compression;
using MongoDB.Driver.Core.Configuration;
using System.Collections.Generic;
To specify compression with MongoClientSettings
, set the Compressors
property to a list of CompressorConfiguration
instances:
var mongoClientSettings = new MongoClientSettings();
mongoClientSettings.Compressors = new List<CompressorConfiguration>()
{
new CompressorConfiguration(CompressorType.Snappy)
};
var client = new MongoClient(mongoClientSettings);
for Snappy compression, or
var mongoClientSettings = new MongoClientSettings();
mongoClientSettings.Compressors = new List<CompressorConfiguration>()
{
new CompressorConfiguration(CompressorType.Zlib)
};
var client = new MongoClient(mongoClientSettings);
for zlib compression, or
var mongoClientSettings = new MongoClientSettings();
mongoClientSettings.Compressors = new List<CompressorConfiguration>()
{
new CompressorConfiguration(CompressorType.Zstd)
};
var client = new MongoClient(mongoClientSettings);
for Zstandard compression, or
var mongoClientSettings = new MongoClientSettings();
mongoClientSettings.Compressors = new List<CompressorConfiguration>()
{
new CompressorConfiguration(CompressorType.Snappy),
new CompressorConfiguration(CompressorType.Zlib),
new CompressorConfiguration(CompressorType.Zstd)
};
var client = new MongoClient(mongoClientSettings);
to configure multiple compressors.
As with configuration via connection string, the driver will use the first compressor in the list for which the server advertises support. Also, the driver allows specifying a compression level option for zlib compression:
var mongoClientSettings = new MongoClientSettings();
var compressorConfiguration = new CompressorConfiguration(CompressorType.Zlib);
compressorConfiguration.Properties.Add("Level", 6);
mongoClientSettings.Compressors = new List<CompressorConfiguration>() { compressorConfiguration };
var client = new MongoClient(mongoClientSettings);