Description描述
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 or字符串或MongoinstanceMongo实例Optional. Host or connection string.可选。主机或连接字符串。The host can either be a connection string or in the form of主机可以是连接字符串,也可以是<host>or<host><:port>.<host>或<host><:port>的形式。The connection string can be in the form of a连接字符串可以是Mongoinstance.Mongo实例的形式。If you specify a如果指定了Mongo实例,Mongoinstance, theMongo()constructor uses the connection string of the specified Mongo instance.Mongo()构造函数将使用指定Mongo实例的连接字符串。If omitted,如果省略,Mongo()instantiates a connection to the localhost interface on the default port27017.Mongo()将在默认端口27017上实例化到localhost接口的连接。autoEncryptionOptsdocument文档Optional. Configuration parameters for enabling In-Use Encryption.可选。启用使用中加密的配置参数。autoEncryptionOptsoverrides the existing in-use encryption configuration of the database connection.覆盖数据库连接的现有正在使用的加密配置。If omitted,如果省略,Mongo()inherits the in-use 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的配置选项。
Tip
Compatibility兼容性
This method 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的源代码可用、免费使用和自我管理版本
AutoEncryptionOpts
The autoEncryptionOpts document specifies configuration options for In-Use Encryption. autoEncryptionOpts文档指定了正在使用的加密的配置选项。If your database connection has an existing in-use encryption configuration, 如果数据库连接有一个现有的正在使用的加密配置,autoEncryptionOpts overrides that configuration. MongoDB provides two approaches to In-Use Encryption: Client-Side Field Level Encryption and Queryable Encryption.autoEncryptionOpts会覆盖该配置。MongoDB提供了两种使用中加密的方法:客户端字段级加密和可查询加密。
For example, starting 例如,使用客户端字段级加密命令行选项启动mongosh with client-side field level encryption command-line options enables client-side encryption for that connection. New database connections created using Mongo() inherit the encryption settings unless
Mongo() includes autoEncryptionOpts.mongosh可以为该连接启用客户端加密。使用Mongo()创建的新数据库连接继承加密设置,除非Mongo()包含autoEncryptionOpts。
The autoEncryptionOpts document has the following syntax:autoEncryptionOpts文档具有以下语法:
{
"keyVaultClient" : <object>,
"keyVaultNamespace" : "<string>",
"kmsProviders" : <object>,
"schemaMap" : <object>,
"bypassAutoEncryption" : <boolean>,
"tlsOptions": <object>,
"encryptedFieldsMap": <object>
}
The autoEncryptionOpts document takes the following parameters:autoEncryptionOpts文档采用以下参数:
keyVaultClient | Mongo() |
|
keyVaultNamespace | ||
kmsProviders |
| |
schemaMap |
| |
bypassAutoEncryption | true to bypass automatic client-side field level encryption rules and perform explicit (manual) per-field encryption.true可绕过自动客户端字段级加密规则,并执行显式(手动)每个字段加密。 | |
bypassQueryAnalysis | true to use explicit encryption on indexed fields without the crypt_shared library. true可在没有crypt_shared库的情况下对索引字段使用显式加密。 | |
explicitEncryptionOnly | true to use neither automatic encryption nor automatic decryption. true既不使用自动加密也不使用自动解密。getKeyVault() and getClientEncryption() to perform explicit encryption. This option is mutually exclusive with schemaMap. If omitted, defaults to false.getKeyVault()和getClientEncryption()来执行显式加密。此选项与schemaMap互斥。如果省略,则默认为false。 | |
tlsOptions | object | tlsCertificateKeyFile), TLS client certificate and private key file password (tlsCertificateKeyFilePassword), or TLS certificate authority file (tlsCAFile) to use to connect to the KMS in PEM format. tlsCertificateKeyFile)、TLS客户端证书和私钥文件密码(tlsCCertificateKeyFilePassword)或TLS证书授权文件(tlsCAFile),用于连接PEM格式的KMS。 |
encryptedFieldsMap |
|
api
The api parameter specifies configuration options for the Stable API. You can enable or disable optional behavior using the following options:api参数指定稳定api的配置选项。您可以使用以下选项启用或禁用可选行为:
version | "1" is currently the only supported version. | |
strict |
| |
deprecationErrors |
|
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 objectConnect to a Cluster with Client-Side Encryption Enabled连接到启用了客户端加密的群集
Generate Your Key生成键
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字节字符串。
const TEST_LOCAL_KEY = require("crypto").randomBytes(96).toString("base64")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 In-Use 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 keymongosh加载键
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配置并访问指定数据加密键的客户端才能解密该字段。
The following operation creates a new connection object from within a 以下操作从mongosh session. The mongo.tlsOptions option enables a connection using KMIP as the KMS provider:mongosh会话中创建新的连接对象。mongo.tlsOptions选项启用使用KMIP作为KMS提供程序的连接:
var csfleConnection = {
keyVaultNamespace: "encryption.__keyVault",
kmsProviders: { kmip: { endpoint: "kmip.example.com:123" } },
tlsOptions: { kmip: { tlsCertificateKeyFile: "/path/to/client/cert-and-key-bundle.pem" } }
}
cluster = Mongo(
"mongodb://mymongo.example.net:27017/?replicaSet=myMongoCluster",
csfleConnection
);
See In-Use 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. The api option enables Stable API V1 and specifies that you cannot run deprecated command or commands outside of the Stable API.mongosh会话中创建新的连接对象。api选项启用Stable api V1,并指定您不能在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命令。