On this page本页内容
Changed in version 4.2.在版本4.2中更改。
Mongo(host, ClientSideFieldLevelEncryptionOptions)
JavaScript constructor to instantiate a database connection from JavaScript构造函数,用于从mongosh
or from a JavaScript file.mongosh
或JavaScript文件实例化数据库连接。
The Mongo()
method has the following parameters:Mongo()
方法具有以下参数:
host | string |
|
ClientSideFieldLevelEncryptionOptions | Document | Optional
|
ClientSideFieldLevelEncryptionOptions
New in version 4.2.在版本4.2中新增。
The ClientSideFieldLevelEncryptionOptions
document specifies configuration options for Client-Side Field Level Encryption. ClientSideFieldLevelEncryptionOptions
文档指定客户端字段级加密的配置选项。If the database connection has an existing client-side field level encryption configuration, specifying 如果数据库连接具有现有的客户端字段级加密配置,则指定ClientSideFieldLevelEncryptionOptions
overrides that configuration.ClientSideFieldLevelEncryptionOptions
将覆盖该配置。
For example, starting 例如,使用客户端字段级加密命令行选项启动mongosh
with client-side field level encryption command-line options enables client-side encryption for that connection. mongosh
可以为该连接启用客户端加密。New database connections created using 使用Mongo()
inherit the encryption settings unlessMongo()
includes ClientSideFieldLevelEncryptionOptions
.Mongo()
创建的新数据库连接继承加密设置除非Mongo()
包括ClientSideFieldLevelEncryptionOptions
。
The ClientSideFieldLevelEncryptionOptions
document has the following syntax:ClientSideFieldLevelEncryptionOptions
文档具有以下语法:
{ "keyVaultClient" : <object>, "keyVaultNamespace" : "<string>", "kmsProviders" : <object>, "schemaMap" : <object>, "bypassAutoEncryption" : <boolean> }
The ClientSideFieldLevelEncryptionOptions
document takes the following parameters:ClientSideFieldLevelEncryptionOptions
文档采用以下参数:
keyVaultClient | Mongo() |
var keyVaultClient = Mongo(<MongoDB URI>);
var ClientSideFieldLevelEncryptionOptions = {
"keyVaultClient" : keyVaultClient,
"keyVaultNamespace" : "<database>.<collection>",
"kmsProviders" : { ... }
}
|
keyVaultNamespace | string | |
kmsProviders | document |
|
schemaMap | document |
|
bypassAutoEncryption | boolean | true to bypass automatic client-side field level encryption rules and perform explicit (manual) per-field encryption.true 以绕过自动客户端字段级加密规则,并执行显式(手动)每个字段加密。
|
The following operation creates a new connection object from within a 以下操作在mongosh
session:mongosh
会话中创建一个新的连接对象:
cluster = Mongo("mongodb://mymongo.example.net:27017/?replicaSet=myMongoCluster")
Issue operations against the 对cluster
object to interact with the mymongo.example.net:27017
cluster:cluster
对象发出操作以与mymongo.example.net:27017
群集交互:
myDB = cluster.getDB("myDB"); //returns the database object myColl = myDB.getCollection("myColl"); // returns the collection object
Configuring client-side field level encryption for a locally managed key requires specifying a base64-encoded 96-byte string with no line breaks. 为本地托管密钥配置客户端字段级加密需要指定不带换行符的base64编码96字节字符串。The following operation generates a key that meets the stated requirements and loads it into 以下操作生成满足所述要求的密钥,并将其加载到mongosh
:mongosh
中:
TEST_LOCAL_KEY=$(echo "$(head -c 96 /dev/urandom | base64 | tr -d '\n')") mongosh --nodb --shell --eval "var TEST_LOCAL_KEY='$TEST_LOCAL_KEY'"
The following operation creates a new connection object from within a 以下操作将从mongosh
session. mongosh
会话中创建一个新的连接对象。The ClientSideFieldLevelEncryptionOptions
option specifies the required options for enabling client-side field level encryption using a locally managed key:ClientSideFieldLevelEncryptionOptions
选项指定使用本地管理密钥启用客户端字段级加密所需的选项:
var ClientSideFieldLevelEncryptionOptions = { "keyVaultNamespace" : "encryption.dataKeys", "kmsProviders" : { "local" : { "key" : BinData(0, TEST_LOCAL_KEY) } } } cluster = Mongo( "mongodb://mymongo.example.net:27017/?replicaSet=myMongoCluster", ClientSideFieldLevelEncryptionOptions )
Issue operations against the 对cluster
object to interact with the mymongo.example.net:27017
cluster and perform explicit encryption:cluster
对象发出操作以与mymongo.example.net:27017
群集交互并执行显式加密:
// returns the database object myDB = cluster.getDB("myDB"); // returns the collection object myColl = myDB.getCollection("myColl"); // returns object for managing data encryption keys keyVault = cluster.getKeyVault(); // returns object for explicit encryption/decryption clientEncryption = cluster.getClientEncryption();
See Client-Side Field Level Encryption Methods for a complete list of client-side field level encryption methods.有关客户端字段级别加密方法的完整列表,请参阅客户端字段级别的加密方法。
Configuring client-side field level encryption for a locally managed key requires specifying a base64-encoded 96-byte string with no line breaks. 为本地托管密钥配置客户端字段级加密需要指定不带换行符的base64编码96字节字符串。The following operation generates a key that meets the stated requirements and loads it into 以下操作生成满足所述要求的密钥,并将其加载到mongosh
:mongosh
中:
TEST_LOCAL_KEY=$(echo "$(head -c 96 /dev/urandom | base64 | tr -d '\n')") mongosh --nodb --shell --eval "var TEST_LOCAL_KEY='$TEST_LOCAL_KEY'"
The following operation creates a new connection object from within a 以下操作将从mongosh
session. mongosh
会话中创建一个新的连接对象。The ClientSideFieldLevelEncryptionOptions
option specifies the required options for enabling automatic client-side encryption on the hr.employees
collection:ClientSideFieldLevelEncryptionOptions
选项指定在hr.employees
集合上启用自动客户端加密所需的选项:
var ClientSideFieldLevelEncryptionOptions = { "keyVaultNamespace" : "encryption.dataKeys", "kmsProviders" : { "local" : { "key" : BinData(0,"BASE64-ENCODED-96-BYTE-LOCAL-KEY") } }, schemaMap : { "hr.employees" : { "bsonType": "object", "properties" : { "taxid" : { "encrypt" : { "keyId" : [UUID("bffb361b-30d3-42c0-b7a4-d24a272b72e3")], "bsonType" : "string", "algorithm" : "AEAD_AES_256_CBC_HMAC_SHA_512-Random" } }, "taxid-short": { "encrypt": { "keyId": [UUID("33408ee9-e499-43f9-89fe-5f8533870617")], "algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic", "bsonType": "string" } } } } } } cluster = Mongo( "mongodb://mymongo.example.net:27017/?replicaSet=myMongoCluster", ClientSideFieldLevelEncryptionOptions )
Issue operations against the 对cluster
object to interact with the mymongo.example.net:27017
cluster and utilize automatic encryption:cluster
对象发出操作以与mymongo.example.net:27017
群集交互,并使用自动加密:
// returns the database object myDB = cluster.getDB("myDB"); // returns the collection object myColl = myDB.getCollection("myColl"); myColl.insertOne( { "name" : "J Doe", "taxid" : "123-45-6789", "taxid-short" : "6789" } )
The specified automatic encryption rules encrypt the 指定的自动加密规则使用指定的数据加密密钥和算法对taxid
and taxid-short
fields using the specified data encryption key and algorithm. taxid
和taxid-short
字段进行加密。Only clients configured for the correct KMS and access to the specified data encryption key can decrypt the field.只有为正确的KMS配置并访问指定数据加密密钥的客户端才能解密该字段。
See Client-Side Field Level Encryption Methods for a complete list of client-side field level encryption methods.有关客户端字段级别加密方法的完整列表,请参阅客户端字段级别的加密方法。