Skip to content

Commit cafa204

Browse files
CSHARP-2763: Create reference documentation for compression.
1 parent 67d73ca commit cafa204

File tree

1 file changed

+123
-0
lines changed

1 file changed

+123
-0
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
+++
2+
date = "2020-01-29T22:05:03-04:00"
3+
title = "Compression"
4+
[menu.main]
5+
parent = "Reference Reading and Writing"
6+
identifier = "Compression"
7+
weight = 25
8+
pre = "<i class='fa'></i>"
9+
+++
10+
11+
## Compression
12+
13+
The C# driver supports compression of messages to and from MongoDB servers. The driver implements the three algorithms that are supported by MongoDB servers:
14+
15+
* [Snappy](https://google.github.io/snappy/): Snappy compression can be used when connecting to MongoDB servers starting with the 3.4 release.
16+
* [Zlib](https://zlib.net/): Zlib compression can be used when connecting to MongoDB servers starting with the 3.6 release.
17+
* [Zstandard](https://facebook.github.io/zstd/): Zstandard compression can be used when connecting to MongoDB servers starting with the 4.2 release.
18+
19+
The driver will negotiate which, if any, compression algorithm is used based on capabilities advertised by the server in the [ismaster]({{<docsref "reference/command/isMaster/">}}) command response.
20+
21+
### Specify compression via `Connection String`
22+
23+
```c#
24+
using MongoDB.Driver;
25+
```
26+
27+
To specify compression with [`ConnectionString`]({{<apiref "T_MongoDB_Driver_Core_Configuration_ConnectionString">}}), just add `compressors` into the connection string, as in:
28+
29+
```c#
30+
var connectionString = "mongodb://localhost/?compressors=snappy";
31+
var client = new MongoClient(connectionString);
32+
```
33+
for Snappy compression, or
34+
35+
```c#
36+
var connectionString = "mongodb://localhost/?compressors=zlib";
37+
var client = new MongoClient(connectionString);
38+
```
39+
for zlib compression, or
40+
41+
```c#
42+
var connectionString = "mongodb://localhost/?compressors=zstd";
43+
var client = new MongoClient(connectionString);
44+
```
45+
for Zstandard compression, or
46+
47+
```c#
48+
var connectionString = "mongodb://localhost/?compressors=snappy,zlib,zstd";
49+
var client = new MongoClient(connectionString);
50+
```
51+
to configure multiple compressors.
52+
53+
In all cases the driver will use the first compressor in the list for which the server advertises support.
54+
55+
Additionally, zlib compression allows specifying a compression level with supported values between -1 and 9:
56+
57+
```c#
58+
var connectionString = "mongodb://localhost/?compressors=zlib;zlibcompressionlevel=6";
59+
var client = new MongoClient(connectionString);
60+
```
61+
62+
### Specify compression via `MongoClientSettings`
63+
64+
```c#
65+
using MongoDB.Driver;
66+
using MongoDB.Driver.Core.Compression;
67+
using MongoDB.Driver.Core.Configuration;
68+
using System.Collections.Generic;
69+
```
70+
71+
To specify compression with [`MongoClientSettings`]({{<apiref "T_MongoDB_Driver_MongoClientSettings">}}), set the `Compressors` property to a list of [`CompressorConfiguration`]({{<apiref "T_MongoDB_Driver_Core_Configuration_CompressorConfiguration">}}) instances:
72+
73+
```c#
74+
var mongoClientSettings = new MongoClientSettings();
75+
mongoClientSettings.Compressors = new List<CompressorConfiguration>()
76+
{
77+
new CompressorConfiguration(CompressorType.Snappy)
78+
};
79+
var client = new MongoClient(mongoClientSettings);
80+
```
81+
for Snappy compression, or
82+
83+
```c#
84+
var mongoClientSettings = new MongoClientSettings();
85+
mongoClientSettings.Compressors = new List<CompressorConfiguration>()
86+
{
87+
new CompressorConfiguration(CompressorType.Zlib)
88+
};
89+
var client = new MongoClient(mongoClientSettings);
90+
```
91+
for zlib compression, or
92+
93+
```c#
94+
var mongoClientSettings = new MongoClientSettings();
95+
mongoClientSettings.Compressors = new List<CompressorConfiguration>()
96+
{
97+
new CompressorConfiguration(CompressorType.Zstd)
98+
};
99+
var client = new MongoClient(mongoClientSettings);
100+
```
101+
for Zstandard compression, or
102+
103+
```c#
104+
var mongoClientSettings = new MongoClientSettings();
105+
mongoClientSettings.Compressors = new List<CompressorConfiguration>()
106+
{
107+
new CompressorConfiguration(CompressorType.Snappy),
108+
new CompressorConfiguration(CompressorType.Zlib),
109+
new CompressorConfiguration(CompressorType.Zstd)
110+
};
111+
var client = new MongoClient(mongoClientSettings);
112+
```
113+
to configure multiple compressors.
114+
115+
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:
116+
117+
```c#
118+
var mongoClientSettings = new MongoClientSettings();
119+
var compressorConfiguration = new CompressorConfiguration(CompressorType.Zlib);
120+
compressorConfiguration.Properties.Add("Level", 6);
121+
mongoClientSettings.Compressors = new List<CompressorConfiguration>() { compressorConfiguration };
122+
var client = new MongoClient(mongoClientSettings);
123+
```

0 commit comments

Comments
 (0)