New in version 7.0.在版本7.0中新增。
ClientEncryption.createEncryptedCollection(dbName, collName, clientEncOpts)ClientEncryption.createEncryptedCollectioncreates an encrypted collection specified by在collNameon the database specified bydbName.dbName指定的数据库上创建由collName指定的加密集合。
Compatibility兼容性
This command is available in deployments hosted in the following environments:此命令在以下环境中托管的部署中可用:
- MongoDB Atlas
: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务
- MongoDB Enterprise
: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本 - MongoDB Community
: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本
Syntax语法
ClientEncryption.createEncryptedCollection has the following syntax:具有以下语法:
clientEncryption = db.getMongo().getClientEncryption()
clientEncryption.createEncryptedCollection(
dbName,
collName,
{
provider: kmsProviderName,
createCollectionOptions: encryptedFieldsMap,
masterKey: customerMasterKeyCredentials
}
)Command Fields命令字段
createEncryptedCollection takes these fields:接受这些字段:
dbName | |||
collName | |||
clientEncOpts | |||
clientEncOpts.provider | |||
clientEncOpts.createCollectionOptions | encryptedFieldsMap object.encryptedFieldsMap对象的详细信息,请参阅步骤。 | ||
clientEncOpts.masterKey |
Behavior行为
The mongosh client-side field level and queryable encryption methods require a database connection configured for client-side encryption. mongosh客户端字段级和可查询加密方法需要为客户端加密配置数据库连接。If the current database connection was not initiated with client-side field level encryption enabled, either:如果当前数据库连接不是在启用客户端字段级加密的情况下启动的,则:
Use the使用Mongo()constructor from themongoshto establish a connection with the required client-side field level encryption options. TheMongo()method supports the following Key Management Service (KMS) providers for Customer Master Key (CMK) management:mongosh中的Mongo()构造函数与所需的客户端字段级加密选项建立连接。Mongo()方法支持以下用于客户主键(CMK)管理的键管理服务(KMS)提供程序:
or
Use the使用mongoshcommand line options to establish a connection with the required options.mongosh命令行选项建立与所需选项的连接。The command line options only support the Amazon Web Services KMS provider for CMK management.命令行选项仅支持用于CMK管理的Amazon Web Services KMS提供程序。
Example示例
The following example uses a locally managed KMS for the Queryable Encryption configuration.以下示例使用本地管理的KMS进行可查询加密配置。
Create Your Encrypted Connection创建加密连接
Start mongosh开始mongoshRun:运行:mongosh --nodb--nodbmeans don't connect to a database.意味着不要连接到数据库。Generate a Key String生成键字符串Generate a base 64 96-byte string:生成一个基本64 96字节的字符串:const TEST_LOCAL_KEY = require("crypto").randomBytes(96).toString("base64")Create an Encryption Options Object创建加密选项对象To create a client-side field level encryption options object, use the要创建客户端字段级加密选项对象,请使用上一步中的TEST_LOCAL_KEYstring from the previous step:TEST_LOCAL_KEY字符串:var autoEncryptionOpts = {
"keyVaultNamespace" : "encryption.__dataKeys",
"kmsProviders" : {
"local" : {
"key" : BinData(0, TEST_LOCAL_KEY)
}
}
}Create an Encrypted Client Object创建加密客户端对象To create an encrypted client object, use the要创建加密的客户端对象,请使用Mongo()constructor.Mongo()构造函数。Replace the更换mongodb://myMongo.example.netURI with the connection string URI for the target cluster. For example:mongodb://myMongo.example.netURI,其中包含目标群集的连接字符串URI。例如:encryptedClient = Mongo(
"mongodb://myMongo.example.net:27017/?replSetName=myMongo",
autoEncryptionOpts
)
Specify which Fields to Encrypt指定要加密的字段
Create an 创建encryptedFieldsMaps to specify which fields to encrypt:encryptedFieldsMaps以指定要加密的字段:
const encryptedFieldsMap = {
encryptedFields: {
fields: [
{
path: "secretField",
bsonType: "string",
queries: { queryType: "equality" },
},
],
},
};Create Your Encrypted Collection创建加密集合
Create an encrypted 创建加密的enc.users collection:enc.users集合:
clientEncryption = encryptedClient.getClientEncryption();
var result = clientEncryption.createEncryptedCollection(
"enc",
"users",
{
provider: "local",
createCollectionOptions: encryptedFieldsMap,
masterKey: {} // masterKey is optional when provider is local
}
)Learn More了解更多
For complete documentation on initiating MongoDB connections with client-side field level encryption enabled, see有关启用客户端字段级加密启动MongoDB连接的完整文档,请参阅Mongo().Mongo()。For a complete example of how to create and query an encrypted collection, see Queryable Encryption Quick Start.有关如何创建和查询加密集合的完整示例,请参阅可查询加密快速入门。