对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

使用显式加密的Queryable Encryption

显式加密提供了对安全性的细粒度控制,但代价是在配置集合和为 MongoDB 驱动程序编写代码时增加了复杂性。 通过显式加密,您可以为对数据库执行的每个操作指定如何加密文档中的字段,并在整个应用程序中包含此逻辑。

以下 MongoDB 产品提供显式加密:

  • MongoDB 社区服务器

  • MongoDB Enterprise Advanced

  • MongoDB Atlas

ClientEncryption is an abstraction used across drivers and mongosh that encapsulates the Key Vault collection and KMS operations involved in explicit encryption.

要创建ClientEncryption实例,请指定:

  • 经过配置,具备您客户主密钥 KMS 托管提供商访问权限的 kmsProviders 对象

  • 密钥保管库集合的命名空间

  • 如果您使用 MongoDB Community Server,请将 bypassQueryAnalysis 选项设置为 True

  • 可以访问您的密钥保管库集合的 MongoClient 实例

有关更多ClientEncryption选项,请参阅适用于 Queryable Encryption 的 MongoClient 选项。

您必须在整个应用程序中更新读写操作,使应用程序在执行读写操作前对字段进行加密。

要加密字段,请使用ClientEncryption实例的encrypt方法。 指定以下内容:

  • 要加密的值

  • 数据加密密钥的 ID

  • 算法

    • unindexed:用于不可查询的加密字段。

    • indexed:支持相等查询。

    • range:支持范围查询。

  • For read operations on queryable encrypted fields, specify the queryType. All algorithms below require an index on the server, which is created when you specify the encryptedFields option during db.createCollection().

  • 对于可查询的加密字段的读取操作,请指定任何其他选项,例如范围字段的最小和最大可查询值。

算法
queryType
其他参数

indexed

equality

range

range

注意

从MongoDB 8.0 开始,rangePreview Queryable Encryption算法已弃用并删除。请改用 Range算法。

如果您的Queryable Encryption集合使用 rangePreview ,则必须删除该集合才能升级到MongoDB 8.0 。

要自动解密字段,请按如下方式配置MongoClient实例:

  • 指定 kmsProviders 对象

  • 指定密钥保管库集合

  • 如果您使用 MongoDB Community Server,请将 bypassQueryAnalysis 选项设置为 True

注意

MongoDB Community 服务器中的自动解密

MongoDB Community 服务器提供自动解密功能。自动加密需要 MongoDB Enterprise 或 MongoDB Atlas。

对集合中的特定字段实施加密的步骤

IndexedRange字段需要在服务器上有索引。 通过在 db.createCollection() 中指定encryptedFields选项来创建索引

如果您的MongoDB实例强制执行特定字段的加密,则任何使用显式加密执行Queryable Encryption的客户端都必须按指定方式对这些字段进行加密。 要学习;了解如何设立服务器端Queryable Encryption强制实施,请参阅加密字段和启用的查询。

要了解有关密钥保管库集合、数据加密密钥和客户主密钥的更多信息,请参阅加密密钥和密钥保管库。

To learn more about KMS providers and kmsProviders objects, see KMS Providers.