7.0版本新增。
ClientEncryption.createEncryptedCollection(dbName, collName, clientEncOpts)ClientEncryption.createEncryptedCollection在由dbName指定的数据库上创建由collName指定的加密collection。
提示
In mongosh, you can use the helper db.createEncryptedCollection(), which runs in the context of the current database and automatically generates data keys when keyId values are not specified.
兼容性
此命令可用于以下环境中托管的部署:
- MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud
MongoDB Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
语法
ClientEncryption.createEncryptedCollection 通过以下语法实现:
clientEncryption = db.getMongo().getClientEncryption() clientEncryption.createEncryptedCollection( dbName, collName, { provider: kmsProviderName, createCollectionOptions: encryptedFieldsMap, masterKey: customerMasterKeyCredentials } )
命令字段
createEncryptedCollection 采用这些字段:
字段 | 类型 | 必要性 | 说明 |
|---|---|---|---|
| 字符串 | 必需 | 要加密的数据库名称。 |
| 字符串 | 必需 | 要加密的collection的名称。 |
| 文档 | 必需 | 用于配置加密collection的选项。 |
| 字符串 | 必需 | 用于存储客户主密钥的 KMS。 |
| 文档 | 必需 | 要加密的字段。 有关如何配置 |
| 文档 | Optional | 指定当KMS提供商为 AWS、 GCP或Azure时访问权限主密钥所需的凭证和密钥标识字段。当KMS提供商为 |
行为
The mongosh client-side field level and queryable encryption methods require a database connection configured for client-side encryption. If the current database connection was not initiated with client-side field level encryption enabled, either:
使用
mongosh中的Mongo()构造函数与所需的客户端字段级加密选项建立连接。Mongo()方法支持以下用于“客户主密钥”(KMS) 管理的“密钥管理服务”(CMK) 提供商:
or
- Use the
mongoshcommand line options to establish a connection with the required options. The command line options only support the Amazon Web Services KMS provider for CMK management.
例子
以下示例使用本地托管的 KMS 进行 Queryable Encryption 配置。
创建加密连接
启动 mongosh
运行:
mongosh --nodb --nodb表示不连接到数据库。生成密钥string
生成一个基本 64 96 字节的string :
const TEST_LOCAL_KEY = require("crypto").randomBytes(96).toString("base64") 创建加密选项对象
要创建客户端字段级加密选项对象,请使用上一步中的
TEST_LOCAL_KEYstring :var autoEncryptionOpts = { "keyVaultNamespace" : "encryption.__dataKeys", "kmsProviders" : { "local" : { "key" : BinData(0, TEST_LOCAL_KEY) } } } 创建加密客户端对象
要创建加密的客户端对象,请使用
Mongo()构造函数。 将mongodb://myMongo.example.netURI 替换为目标集群的连接string URI 。 示例:encryptedClient = Mongo( "mongodb://myMongo.example.net:27017/?replSetName=myMongo", autoEncryptionOpts )
了解详情
有关在启用客户端字段级加密的情况下启动 MongoDB 连接的完整文档,请参阅
Mongo()。有关如何创建和查询加密集合的完整示例,请参阅Queryable Encryption快速入门。