-
Notifications
You must be signed in to change notification settings - Fork 34
DOCSP-47057: Network compression #254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
norareidy
merged 4 commits into
mongodb:comp-cov
from
norareidy:DOCSP-47057-compression
May 23, 2025
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
source/connect/connection-options/network-compression.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
.. _php-network-compression: | ||
|
||
======================== | ||
Compress Network Traffic | ||
======================== | ||
|
||
.. contents:: On this page | ||
:local: | ||
:backlinks: none | ||
:depth: 2 | ||
:class: singlecol | ||
|
||
.. facet:: | ||
:name: genre | ||
:values: reference | ||
|
||
.. meta:: | ||
:keywords: zlib, zstandard, zstd, snappy | ||
|
||
Overview | ||
-------- | ||
|
||
In this guide, you can learn how to configure **network compression** for | ||
your connection to MongoDB. | ||
|
||
Network compression is a feature that allows you to compress and | ||
decompress messages sent between your application and MongoDB, reducing | ||
the total amount of data passed over the network. | ||
|
||
The {+library-short+} supports the following compressors: | ||
|
||
1. `Snappy <https://google.github.io/snappy/>`__ | ||
#. `Zlib <https://zlib.net/>`__ | ||
#. `Zstandard <https://github.com/facebook/zstd/>`__ | ||
|
||
.. note:: Compressor Selection | ||
|
||
If you specify multiple compressors to use on your connection, the | ||
driver selects the first one that is supported by the MongoDB | ||
instance that the {+library-short+} is connected to. | ||
|
||
.. _php-enable-compression: | ||
|
||
Enable Network Compression | ||
-------------------------- | ||
|
||
To enable compression for the connection to your MongoDB instance, use the | ||
``compressors`` connection option and specify the compression algorithms you want to use. | ||
You can do this in two ways: | ||
|
||
- Pass the algorithms as an argument to the ``MongoDB\Client`` constructor. | ||
- Specify the algorithms in your connection string. | ||
|
||
The following example shows how to specify Snappy, Zlib, and | ||
Zstandard as the compressors for a connection. Select the :guilabel:`MongoDB\\Client` | ||
or :guilabel:`Connection URI` tab to see the corresponding code: | ||
|
||
.. tabs:: | ||
|
||
.. tab:: MongoDB\\Client | ||
:tabid: Client | ||
|
||
.. literalinclude:: /includes/connect/network-compression.php | ||
:language: php | ||
:dedent: | ||
:start-after: start-enable-client | ||
:end-before: end-enable-client | ||
|
||
.. tab:: Connection URI | ||
:tabid: connectionstring | ||
|
||
.. literalinclude:: /includes/connect/network-compression.php | ||
:language: php | ||
:dedent: | ||
:start-after: start-enable-uri | ||
:end-before: end-enable-uri | ||
|
||
Specify the zlib Compression Level | ||
---------------------------------- | ||
|
||
If you specify ``zlib`` as one of your compression algorithms, you can also use the | ||
``zlibCompressionLevel`` option to specify a compression level. This option accepts | ||
an integer value between ``-1`` and ``9``: | ||
|
||
- **-1:** (Default). zlib uses its default compression level (usually ``6``). | ||
- **0:** No compression. | ||
- **1:** Fastest speed but lowest compression. | ||
- **9:** Best compression but slowest speed. | ||
|
||
The following example specifies the ``zlib`` compression algorithm and a | ||
``zlibCompressionLevel`` value of ``1``. Select the :guilabel:`MongoDB\\Client` | ||
or :guilabel:`Connection URI` tab to see the corresponding code: | ||
|
||
.. tabs:: | ||
|
||
.. tab:: MongoDB\\Client | ||
:tabid: Client | ||
|
||
.. literalinclude:: /includes/connect/network-compression.php | ||
:language: php | ||
:dedent: | ||
:start-after: start-set-level-client | ||
:end-before: end-set-level-client | ||
|
||
.. tab:: Connection URI | ||
:tabid: connectionstring | ||
|
||
.. literalinclude:: /includes/connect/network-compression.php | ||
:language: php | ||
:dedent: | ||
:start-after: start-set-level-uri | ||
:end-before: end-set-level-uri | ||
|
||
API Documentation | ||
----------------- | ||
|
||
To learn more about the ``MongoDB\Client`` class, see :phpclass:`MongoDB\Client` | ||
in the library API documentation. | ||
|
||
To view a full list of URI options that you can pass to a ``MongoDB\Client``, see the | ||
:php:`MongoDB\Driver\Manager::__construct parameters </manual/en/mongodb-driver-manager.construct.php#refsect1-mongodb-driver-manager.construct-parameters>` | ||
in the extension API documentation. | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
// start-enable-client | ||
$client = new MongoDB\Client( | ||
'mongodb://<hostname>:<port>', | ||
['compressors' => 'snappy,zstd,zlib'], | ||
); | ||
// end-enable-client | ||
|
||
// start-enable-uri | ||
$uri = 'mongodb://<hostname>:<port>/?compressors=snappy,zstd,zlib'; | ||
$client = new MongoDB\Client($uri); | ||
// end-enable-uri | ||
|
||
// start-set-level-client | ||
$uriOptions = [ | ||
'compressors' => 'zlib', | ||
'zlibCompressionLevel' => 1, | ||
]; | ||
|
||
$client = new MongoDB\Client( | ||
'mongodb://<hostname>:<port>', | ||
$uriOptions, | ||
); | ||
// end-set-level-client | ||
|
||
// start-set-level-uri | ||
$uri = 'mongodb://<hostname>:<port>/?compressors=zlib&zlibCompressionLevel=1'; | ||
$client = new MongoDB\Client($uri); | ||
// end-set-level-uri |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.