Learn the "why" behind slow queries and how to fix them in our 2-Part Webinar.
Register now >
Docs 菜单
Docs 主页
/ /

ClientEncryption.createEncryptedCollection()(mongosh方法)

7.0 版本中的新增功能

ClientEncryption.createEncryptedCollection(dbName, collName, clientEncOpts)

ClientEncryption.createEncryptedCollection 在由dbName指定的数据库上创建由collName指定的加密collection。

提示

mongosh 中,您可以使用助手 db.createEncryptedCollection(),它在当前数据库的上下文中运行,并在未指定 keyId 值时自动生成数据键。

此命令可用于以下环境中托管的部署:

  • MongoDB Atlas:用于云中 MongoDB 部署的完全托管服务

ClientEncryption.createEncryptedCollection 通过以下语法实现:

clientEncryption = db.getMongo().getClientEncryption()
clientEncryption.createEncryptedCollection(
dbName,
collName,
{
provider: kmsProviderName,
createCollectionOptions: encryptedFieldsMap,
masterKey: customerMasterKeyCredentials
}
)

createEncryptedCollection 采用这些字段:

字段
类型
必要性
说明

dbName

字符串

必需

要加密的数据库名称。

collName

字符串

必需

要加密的collection的名称。

clientEncOpts

文档

必需

用于配置加密collection的选项。

clientEncOpts.provider

字符串

必需

用于存储客户主密钥的 KMS。

clientEncOpts.createCollectionOptions

文档

必需

要加密的字段。 有关如何配置encryptedFieldsMap对象的详细信息,请参阅指定加密字段

clientEncOpts.masterKey

文档

Optional

指定当KMS提供商为 AWS、 GCP或Azure时访问权限主密钥所需的凭证和密钥标识字段。当KMS提供商为 local 时,为可选。

mongosh客户端字段级别和可查询Queryable Encryption方法需要为客户端加密配置数据库连接。 如果当前数据库连接不是在启用客户端字段级加密的情况下启动的,则:

or

以下示例使用本地托管的 KMS 进行 Queryable Encryption 配置。

1
  1. 启动 mongosh

    运行:

    mongosh --nodb

    --nodb 表示不连接到数据库。

  2. 生成密钥string

    生成一个基本 64 96 字节的string :

    const TEST_LOCAL_KEY = require("crypto").randomBytes(96).toString("base64")
  3. 创建加密选项对象

    要创建客户端字段级加密选项对象,请使用上一步中的 TEST_LOCAL_KEY string :

    var autoEncryptionOpts = {
    "keyVaultNamespace" : "encryption.__dataKeys",
    "kmsProviders" : {
    "local" : {
    "key" : BinData(0, TEST_LOCAL_KEY)
    }
    }
    }
  4. 创建加密客户端对象

    要创建加密的客户端对象,请使用Mongo()构造函数。 将 mongodb://myMongo.example.net URI 替换为目标集群的连接string URI 。 示例:

    encryptedClient = Mongo(
    "mongodb://myMongo.example.net:27017/?replSetName=myMongo",
    autoEncryptionOpts
    )
2

创建encryptedFieldsMaps以指定要加密的字段:

const encryptedFieldsMap = {
encryptedFields: {
fields: [
{
path: "secretField",
bsonType: "string",
queries: { queryType: "equality" },
},
],
},
};
3

创建加密的enc.userscollection:

clientEncryption = encryptedClient.getClientEncryption();
let result = clientEncryption.createEncryptedCollection(
"enc",
"users",
{
provider: "local",
createCollectionOptions: encryptedFieldsMap,
masterKey: {} // masterKey is optional when provider is local
}
)
4

createEncryptedCollection 返回一个包含许多字段的大型结果对象。检查result.collection的值,确认已在所需位置创建collection。

enc> result.collection
enc.users

后退

SessionOptions

在此页面上