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

getKeyVault()(mongosh方法)

getKeyVault()

返回当前数据库连接的KeyVault对象。 KeyVault对象支持客户端字段级加密的数据加密密钥管理。

返回:

当前数据库连接的KeyVault对象。

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

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

getKeyVault() 通过以下语法实现:

keyVault = db.getMongo().getKeyVault();

使用KeyVault对象访问以下数据加密密钥管理方法:

以下示例使用本地托管的密钥进行客户端字段级加密配置。

The mongosh client-side field level encryption methods require a database connection with client-side field level encryption enabled. If the current database connection was not initiated with client-side field level encryption enabled, either:

The getKeyVault() method automatically creates a unique index on the keyAltNames field with a partial index filter for only documents where keyAltNames exists. getKeyVault() creates this index in the key vault collection. This prevents any two data encryption keys in the same key vault from having the same key alternative name and therefore avoids ambiguity around which data encryption key is appropriate for encryption/decryption.

警告

Do not drop the unique index created by getKeyVault(). Client-Side Field Level Encryption operations depend on server-enforced uniqueness of keyAltNames. Removing the index may lead to unexpected or unpredictable behavior.

以下示例使用本地托管的密钥进行客户端字段级加密配置。

1

启动mongosh客户端。

mongosh --nodb
2

要为本地托管的密钥配置客户端字段级加密,请生成一个不带换行符的 base64 编码的 96 字节字符串。

const TEST_LOCAL_KEY = require("crypto").randomBytes(96).toString("base64")
3

使用生成的本地密钥字符串创建客户端字段级加密选项:

let autoEncryptionOpts = {
"keyVaultNamespace" : "encryption.__dataKeys",
"kmsProviders" : {
"local" : {
"key" : BinData(0, TEST_LOCAL_KEY)
}
}
}
4

使用配置了客户端字段级加密选项的Mongo()构造函数来创建数据库连接。 将mongodb://myMongo.example.net URI 替换为目标集群的连接字符串 URI

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

Use the getKeyVault() method to retrieve the key vault object:

keyVault = encryptedClient.getKeyVault()

有关在启用客户端字段级加密的情况下启动 MongoDB 连接的完整文档,请参阅Mongo()

给本页内容打分