Mongo()
On this page本页内容
- Description描述
- AutoEncryptionOpts
- api
- Examples实例
- Connect to a MongoDB Cluster连接到MongoDB集群
- Connect to a Cluster with Client-Side Encryption Enabled连接到启用客户端加密的群集
- Connect to a Cluster with Automatic Client-Side Encryption Enabled连接到启用了自动客户端加密的群集
- Connect to a Cluster with the Stable API Enabled连接到已启用稳定API的群集
Description描述
Changed in version 4.2.4.2版更改。
- Mongo(host, autoEncryptionOpts, api)
- 
JavaScript constructor to instantiate a database connection fromJavaScript构造函数,用于从mongoshor from a JavaScript file.mongosh或JavaScript文件实例化数据库连接。TheMongo()method has the following parameters:Mongo()方法具有以下参数:Parameter参数Type类型Description描述hoststring Optional.可选的。The host, either in the form of主机,形式为<host>or<host><:port>.<host>或<host><:port>。
 If omitted,如果省略,Mongo()instantiates a connection to the localhost interface on the default port27017.Mongo()将实例化到默认端口27017上的localhost接口的连接。autoEncryptionOptsdocument New in version 4.2.4.2版新增。
 Optional.可选的。Configuration parameters for enabling Client-Side Field Level Encryption.用于启用客户端字段级加密的配置参数。
 autoEncryptionOptsoverrides the existing client-side field level encryption configuration of the database connection.覆盖数据库连接的现有客户端字段级加密配置。If omitted,如果省略,Mongo()inherits the client-side field level encryption configuration of the current database connection.Mongo()将继承当前数据库连接的客户端字段级加密配置。
 See有关用法和语法的详细信息,请参阅AutoEncryptionOptsfor usage and syntax details.AutoEncryptionOpts。apidocument Optional.可选的。Configuration options for enabling the Stable API.用于启用稳定API的配置选项。
 See有关用法和语法的详细信息,请参阅apifor usage and syntax details.api。
See also: 另请参阅:
AutoEncryptionOpts
New in version 4.2. 4.2版新增。
The autoEncryptionOpts document specifies configuration options for Client-Side Field Level Encryption. autoEncryptionOpts文档指定客户端字段级加密的配置选项。If the database connection has an existing client-side field level encryption configuration, specifying 如果数据库连接具有现有的客户端字段级加密配置,则指定autoEncryptionOpts overrides that configuration.autoEncryptionOpts将覆盖该配置。
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 unless Mongo() includes autoEncryptionOpts.Mongo()创建的新数据库连接将继承加密设置,除非Mongo()包含autoEncryptionOpts。
The autoEncryptionOpts document has the following syntax:autoEncryptionOpts文档具有以下语法:
{
  "keyVaultClient" : <object>,
  "keyVaultNamespace" : "<string>",
  "kmsProviders" : <object>,
  "schemaMap" : <object>,
  "bypassAutoEncryption" : <boolean>
}
The autoEncryptionOpts document takes the following parameters:autoEncryptionOpts文档采用以下参数:
| keyVaultClient | Mongo() | Mongo()connection object pointing to the cluster:Mongo()连接对象:var keyVaultClient = Mongo(<MongoDB URI>); keyVaultClientis omitted, thehostspecified to theMongo()object containing theautoEncryptionOptsdocument is used as the key vault host.keyVaultClient,则为包含autoEncryptionOpts文档的Mongo()对象指定的host将用作键保管库主机。 | 
| keyVaultNamespace | string | |
| kmsProviders | document | kmsProvidersas environment variables, and then passing them tomongoshusing the--evaloption.kmsProviders中提供的凭据定义为环境变量,然后使用--eval选项将它们传递给mongosh。
 | 
| schemaMap | document | |
| bypassAutoEncryption | boolean | trueto bypass automatic client-side field level encryption rules and perform explicit (manual) per-field encryption.true可绕过自动客户端字段级加密规则并执行显式(手动)每个字段加密。 | 
| bypassQueryAnalysis | boolean | trueto use explicit encryption on indexed fields without thecrypt_sharedlibrary.true可对没有crypt_shared库的索引字段使用显式加密。 | 
api
The api parameter specifies configuration options for the Stable API. api参数指定Stable api的配置选项。You can enable or disable optional behavior using the following options:您可以使用以下选项启用或禁用可选行为:
| version | string | "1" | 
| strict | boolean | true, using a command that is not part of the declared API version returns an APIStrictError error.true,则使用不是声明的API版本的一部分的命令将返回APIStrictError错误。strict, you must also specifyversion.strict,则还必须指定version。false.false。 | 
| deprecationErrors | boolean | true, using a command or behavior that is deprecated in the specified API version returns an APIDeprecationError.true,则使用指定API版本中不推荐使用的命令或行为将返回APIDeprecationError。deprecationErrors, you must also specifyversion.depositionErrors,则还必须指定version。false.false。 | 
The api parameter has the following syntax:api参数具有以下语法:
{ api: { version: <string>, strict: <boolean>, deprecationErrors: <boolean> } }
Examples实例
Connect to a MongoDB Cluster连接到MongoDB集群
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返回集合对象
Connect to a Cluster with Client-Side Encryption Enabled连接到启用客户端加密的群集
Create the Client-Side Field Level Encryption Options创建客户端字段级加密选项
Create the client-side field level encryption options using the generated local key string:使用生成的本地键字符串创建客户端字段级加密选项:
var autoEncryptionOpts = {
"keyVaultNamespace" : "encryption.__dataKeys",
"kmsProviders" : {
"local" : {
"key" : BinData(0, TEST_LOCAL_KEY)
}
}
}
Create Your Encrypted Client创建加密客户端
Use the 使用Mongo() constructor with the client-side field level encryption options configured to create a database connection. Mongo()构造函数和配置用于创建数据库连接的客户端字段级加密选项。Replace the 更换mongodb://myMongo.example.net URI with the connection string URI of the target cluster.mongodb://myMongo.example.netURI与目标群集的连接字符串URI。
encryptedClient = Mongo(
"mongodb://myMongo.example.net:27017/?replSetName=myMongo",
autoEncryptionOpts
)
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.有关客户端字段级别加密方法的完整列表,请参阅客户端字段级别的加密方法。
Connect to a Cluster with Automatic Client-Side Encryption Enabled连接到启用了自动客户端加密的群集
To configure client-side field level encryption for a locally managed key:要为本地管理的键配置客户端字段级加密,请执行以下操作:
- generate a base64-encoded 96-byte string with no line breaks生成不带换行符的base64编码的96字节字符串
- use使用- mongoshto load the key- mongosh加载键
export TEST_LOCAL_KEY=$(echo "$(head -c 96 /dev/urandom | base64 | tr -d '\n')")
mongosh --nodb
The following operation creates a new connection object from within a 以下操作从mongosh session. mongosh会话中创建一个新的连接对象。The AutoEncryptionOpts option specifies the required options for enabling automatic client-side encryption on the hr.employees collection:AutoEncryptionOpts选项指定在hr.employees集合上启用客户端自动加密所需的选项:
var autoEncryptionOpts = {
"keyVaultNamespace" : "encryption.__dataKeys",
"kmsProviders" : {
"local" : {
"key" : BinData(0, process.env["TEST_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",
autoEncryptionOpts
)
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. Only clients configured for the correct KMS and access to the specified data encryption key can decrypt the field.taxid和taxid-short字段进行加密。只有为正确的KMS和对指定数据加密键的访问权限配置的客户端才能解密该字段。
See Client-Side Field Level Encryption Methods for a complete list of client-side field level encryption methods.有关客户端字段级别加密方法的完整列表,请参阅客户端字段级别的加密方法。
Connect to a Cluster with the Stable API Enabled连接到已启用稳定API的群集
The following operation creates a new connection object from within a 以下操作从mongosh session. mongosh会话中创建一个新的连接对象。The api选项启用Stable api V1,并指定您不能在Stable api之外运行不推荐使用的命令。api option enables Stable API V1 and specifies that you cannot run deprecated command or commands outside of the Stable API.
cluster = Mongo(
"mongodb://mymongo.example.net:27017/?replicaSet=myMongoCluster",
null,
{ api: { version: "1", strict: true, deprecationErrors: true } }
)
To interact with the 要与mymongo.example.net:27017 cluster, issue operations against the cluster object. For a full list of Stable API commands, see Stable API Commands.mymongo.example.net:27017集群交互,请针对cluster对象发出操作。有关Stable API命令的完整列表,请参阅Stable API命令。