For AI agents: a documentation index is available at https://www.mongodb.com/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.
See how MongoDB 9.0 delivers up to 2x higher throughput.
MongoDB Branding Shape
Register now >
Docs Menu

Compress Network Traffic

All versions of the Laravel Integration support each network compression algorithm.

The following table lists the minimum supported MongoDB Server version for each algorithm:

Compression Algorithm
Minimum Server Version

4.2

4.2

4.2

To enable network compression for your MongoDB connection, perform the following steps:

  • Build the MongoDB PHP Extension with the required dependencies. The extension handles network compression, not the Laravel Integration.

  • Specify the compression algorithms you want to use.

Because the Laravel Integration uses the MongoDB PHP Extension and the MongoDB C Driver to implement network compression, you must install the C driver with the required dependencies. To view these dependencies, see Install Dependencies in the C driver documentation.

To enable compression for the connection to your MongoDB instance, update the connections array in your application's config/database.php configuration file. You can specify the compression algorithms in one of the following ways:

  • Include the compressors option in the options array

  • Use the compressors parameter in the dsn connection string

Select the Options Array or Connection String tab to view an example that specifies all available compressors:

'connections' => [
'mongodb' => [
'dsn' => 'mongodb+srv://mongodb0.example.com/',
'driver' => 'mongodb',
'database' => 'sample_mflix',
'options' => [
'compressors' => 'snappy,zstd,zlib',
],
],
],
'connections' => [
'mongodb' => [
'dsn' => 'mongodb+srv://mongodb0.example.com/?compressors=snappy,zstd,zlib',
'driver' => 'mongodb',
'database' => 'sample_mflix',
],
],

If you specify Zlib as one of your compression algorithms, you can use the zlibCompressionLevel option to specify a compression level. This option accepts an integer value between -1 and 9, and values have the following effects:

  • -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.

Select the Options Array or Connection String tab to view an example that sets the zlibCompressionLevel option to 1:

'connections' => [
'mongodb' => [
'dsn' => 'mongodb+srv://mongodb0.example.com/',
'driver' => 'mongodb',
'database' => 'sample_mflix',
'options' => [
'compressors' => 'zlib',
'zlibCompressionLevel' => 1,
],
],
],
'connections' => [
'mongodb' => [
'dsn' => 'mongodb+srv://mongodb0.example.com/?compressors=zlib&zlibCompressionLevel=1',
'driver' => 'mongodb',
'database' => 'sample_mflix',
],
],