|
| 1 | +.. _csharp-network-compression: |
| 2 | + |
| 3 | +=================== |
| 4 | +Network Compression |
| 5 | +=================== |
| 6 | + |
| 7 | +You can enable a driver option to compress messages, which reduces the amount |
| 8 | +of data passed over the network between MongoDB and your application. |
| 9 | + |
| 10 | +The {+driver-short+} supports the following compression algorithms: |
| 11 | + |
| 12 | +1. `Snappy <https://google.github.io/snappy/>`__: available in MongoDB 3.6 and later. |
| 13 | + |
| 14 | +2. `Zlib <https://zlib.net/>`__: available in MongoDB 3.6 and later. |
| 15 | + |
| 16 | +3. `Zstandard <https://github.com/facebook/zstd/>`__: available in MongoDB 4.2 and later. |
| 17 | + |
| 18 | +If you specify multiple compression algorithms, the driver selects the |
| 19 | +first one in the list supported by your MongoDB instance. |
| 20 | + |
| 21 | +.. _enable-compression: |
| 22 | + |
| 23 | +Specify Compression Algorithms |
| 24 | +------------------------------ |
| 25 | + |
| 26 | +To enable compression for the connection to your MongoDB instance, |
| 27 | +specify the algorithms you want to use in one of the following ways: |
| 28 | + |
| 29 | +1. Add the algorithms to your connection string as a parameter |
| 30 | +2. Specify the algorithms in the ``Compressors`` property of your ``MongoClientSettings`` |
| 31 | + object |
| 32 | + |
| 33 | +.. tabs:: |
| 34 | + |
| 35 | + .. tab:: Connection String |
| 36 | + :tabid: connection-string |
| 37 | + |
| 38 | + To enable compression by using the connection string, add the |
| 39 | + ``compressors`` parameter to the connection string. You can |
| 40 | + specify one or more compression algorithms, separating them with |
| 41 | + commas: |
| 42 | + |
| 43 | + .. code-block:: csharp |
| 44 | + :emphasize-lines: 2 |
| 45 | + |
| 46 | + const string connectionUri = |
| 47 | + "mongodb+srv://<user>:<password>@<cluster-url>/?compressors=snappy,zlib,zstd"; |
| 48 | + |
| 49 | + var client = new MongoClient(connectionUri); |
| 50 | + |
| 51 | + .. tab:: MongoClientSettings |
| 52 | + :tabid: mongoclientsettings |
| 53 | + |
| 54 | + To enable compression by using |
| 55 | + `MongoClientSettings <{+api-root+}/T_MongoDB_Driver_MongoClientSettings.htm>`__, |
| 56 | + set the ``Compressors`` property of your ``MongoClientSettings`` object to a |
| 57 | + ``List`` of one or more ``CompressorConfiguration`` objects. Each |
| 58 | + ``CompressorConfiguration`` object in the ``List`` represents an algorithm you |
| 59 | + want to use: |
| 60 | + |
| 61 | + .. code-block:: csharp |
| 62 | + :emphasize-lines: 5-10 |
| 63 | + |
| 64 | + var settings = new MongoClientSettings() |
| 65 | + { |
| 66 | + Scheme = ConnectionStringScheme.MongoDB, |
| 67 | + Server = new MongoServerAddress("<cluster-url>"), |
| 68 | + Compressors = new List<CompressorConfiguration>() |
| 69 | + { |
| 70 | + new CompressorConfiguration(CompressorType.Snappy), |
| 71 | + new CompressorConfiguration(CompressorType.Zlib), |
| 72 | + new CompressorConfiguration(CompressorType.Zstandard) |
| 73 | + } |
| 74 | + }; |
| 75 | + |
| 76 | + var client = new MongoClient(settings); |
0 commit comments