说明
Mongo(host, autoEncryptionOpts, api)JavaScript constructor to instantiate a database connection from
mongoshor from a JavaScript file.The
Mongo()method has the following parameters:Parameter类型说明hoststring或
Mongo实例可选。 主机或连接string 。
The host can either be a connection string or in the form of
<host>or<host><:port>. The connection string can be in the form of aMongoinstance. If you specify aMongoinstance, theMongo()constructor uses the connection string of the specified Mongo instance.If omitted,
Mongo()instantiates a connection to the localhost interface on the default port27017.autoEncryptionOpts文档
可选。用于启用In-正在使用的加密的配置参数。
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.See
AutoEncryptionOptsfor usage and syntax details.api文档
兼容性
此方法可用于以下环境中托管的部署:
- MongoDB Atlas:用于云中 MongoDB 部署的完全托管服务
MongoDB Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
AutoEncryptionOpts
autoEncryptionOpts文档指定“正在使用的加密”的配置选项。如果数据库连接具有正在使用的加密配置,则 autoEncryptionOpts 将覆盖该配置。MongoDB提供两种“正在使用的加密”方法:客户端字段级加密和Queryable Encryption。
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.
autoEncryptionOpts 文档的语法如下:
{ "keyVaultClient" : <object>, "keyVaultNamespace" : "<string>", "kmsProviders" : <object>, "schemaMap" : <object>, "bypassAutoEncryption" : <boolean>, "tlsOptions": <object>, "encryptedFieldsMap": <object> }
autoEncryptionOpts 文档采用以下参数:
Parameter | 类型 | 说明 | |||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
| (可选)托管密钥保管库集合的 MongoDB cluster。 Specify a If | |||||||||||||||||||||||||||
| 字符串 | (必填)密钥保管库集合的完整命名空间 。 | |||||||||||||||||||||||||||
| 文档 | (必填)客户端字段级加密使用的密钥管理服务(KMS),用于管理客户主密钥(CMK)。客户端字段级加密使用 CMK 对数据加密密钥进行加密及解密。 客户端字段级加密支持以下 KMS 提供商: If possible, consider defining the credentials provided in
| |||||||||||||||||||||||||||
| 文档 | (可选) 使用JSON schema草案 4 标准语法和特定于加密的关键字指定的自动客户端字段级加密规则。此选项与 有关完整的文档,请参阅加密模式。 | |||||||||||||||||||||||||||
| 布尔 | (可选)指定 | |||||||||||||||||||||||||||
| 布尔 | (可选)指定 | |||||||||||||||||||||||||||
| 布尔 | (可选)指定 | |||||||||||||||||||||||||||
| 对象 | (可选) PEM 格式 ( | |||||||||||||||||||||||||||
| 文档 | (可选)集合命名空间到 要学习;了解有关Queryable Encryption中加密字段的更多信息,请参阅加密字段和启用的查询。 |
api
api 参数为 Stable API 指定配置选项。您可以使用以下选项启用或禁用可选行为:
选项 | 类型 | 说明 |
|---|---|---|
| 字符串 | 指定 API 版本。 |
| 布尔 |
如果指定 如果未指定,则默认为 |
| 布尔 | 如果为 如果未指定,则默认为 |
api 参数采用以下语法:
{ api: { version: <string>, strict: <boolean>, deprecationErrors: <boolean> } }
示例
连接到 MongoDB 集群
The following operation creates a new connection object from within a mongosh session:
cluster = Mongo("mongodb://mymongo.example.net:27017/?replicaSet=myMongoCluster")
针对cluster对象发出操作以与 mymongo.example.net:27017 集群交互:
myDB = cluster.getDB("myDB"); //returns the database object myColl = myDB.getCollection("myColl"); // returns the collection object
连接到已启用客户端加密的集群
创建加密客户端
Use the Mongo() constructor with the client-side field level encryption options configured to create a database connection. Replace the mongodb://myMongo.example.net URI with the connection string URI of the target cluster.
encryptedClient = Mongo( "mongodb://myMongo.example.net:27017/?replSetName=myMongo", autoEncryptionOpts )
对 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();
有关客户端字段级加密方法的完整列表,请参阅客户端字段级加密。
连接到已启用自动客户端加密的集群
为本地管理的密钥配置客户端字段级加密:
生成不带换行符的 base64 编码的 96 字节字符串
use
mongoshto load the key
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. The AutoEncryptionOpts option specifies the required options for enabling automatic client-side encryption on the hr.employees collection:
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 )
对 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" } )
指定的自动加密规则使用指定的数据加密密钥和算法对 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:
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 );
有关客户端字段级加密方法的完整列表,请参阅客户端字段级加密。
连接到已启用稳定 API 的集群
以下操作会在mongosh 会话中创建新的连接对象。 选项启用 Stable APIapi V1 ,并指定您不能运行已弃用的命令或 Stable API之外的命令。
cluster = Mongo( "mongodb://mymongo.example.net:27017/?replicaSet=myMongoCluster", null, { api: { version: "1", strict: true, deprecationErrors: true } } )
要与 mymongo.example.net:27017 集群进行交互,请针对 cluster 对象发出操作。有关 Stable API 命令的完整列表,请参阅 Stable API 命令。