Overview
MongoDB驱动程序可以压缩发送到 MongoDB Server 和从MongoDB Server接收的消息。这减少了每次操作传输的数据量。压缩可以提高受限网络上的性能,并降低与运行MongoDB Atlas相关的数据传输成本。
压缩在传输协议级别运行,因此它适用于应用程序执行的每个操作。如果指定多个算法,驾驶员将使用列表中MongoDB Server也支持的第一个算法。
压缩算法
MongoDB驱动程序支持以下压缩算法:
网络压缩的好处
MongoDB Atlas charges for data transferred across cloud regions, cloud providers, and between your infrastructure and MongoDB Atlas. Network compression reduces the amount of data that your application sends and receives, which can lower these data transfer costs. To learn more about pricing, see Data Transfer Costs.
网络压缩还有助于提高工作效率。压缩后的消息通过网络传输的时间更短,这可以缩短应用程序的响应时间。压缩还可以减少MongoDB流量使用的带宽,从而为同一网络上的其他应用程序留出更多带宽。
选择压缩算法时,请考虑以下准则:
使用 Zstandard 在压缩效率和资源使用之间取得平衡。对于大多数应用程序来说,Zstandard 是一个不错的默认选择。
将 Snappy 用于需要最小计算开销的延迟敏感型应用程序。
使用 Zlib 实现跨MongoDB驱动程序和环境的广泛兼容性。
提示
监控 CPU 使用率
Compression trades network bandwidth for CPU usage. After you enable network compression, monitor your application's CPU utilization to confirm that the trade-off benefits your workload. To learn more about these trade-offs, see the CPU Utilization and Network Compression in MongoDB blog post.
版本兼容性
下表列出了每种压缩算法支持的最低驾驶员和MongoDB Server版本:
启用网络压缩
要为MongoDB连接启用网络压缩,请安装所需的依赖项并指定要使用的压缩算法。
安装依赖项
下表列出了每种算法及其所需的依赖项(如果有):
Compression Algorithm | 依赖 |
|---|---|
snappy | Add the Snappy compression algorithm to your application by running the |
zlib | The driver uses the built-in |
Zstandard | Add the Zstandard compression algorithm to your application by running the |
指定压缩算法
您可以通过以下方式之一指定算法,为MongoDB 部署的连接启用压缩:
在连接字符串中设置压缩算法
Set the compression algorithm in a
ClientOptionsinstance
使用以下字符串指定压缩算法:
更多信息
有关本指南中概念的更多信息,请参阅以下文档:
创建 MongoClient指南的“连接 URI”部分。
手册中的 string连接 压缩选项MongoDB Server
zlib包 Go文档