バージョン7.0の新機能。
db.createEncryptedCollection(collName, options)db.createEncryptedCollection()は、現在のデータベースにcollNameで指定された暗号化されたコレクションを作成します。このメソッドは、
encryptedFields.fields[*].keyIdがnullまたはoptions.createCollectionOptions.encryptedFields定義で省略されている場合にデータキーを自動的に作成するClientEncryption.createEncryptedCollection()を囲むラッパーです。
互換性
このコマンドは、次の環境でホストされている配置で使用できます。
- MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud
MongoDB Enterprise: サブスクリプションベースの自己管理型 MongoDB バージョン
MongoDB Community: ソースが利用可能で、無料で使用できる自己管理型の MongoDB のバージョン
構文
db.createEncryptedCollection() の構文は次のとおりです。
db.createEncryptedCollection( collName, { provider: kmsProviderName, createCollectionOptions: encryptedFieldsMap, masterKey: customerMasterKeyCredentials } )
コマンドフィールド
db.createEncryptedCollection() 次のフィールドを取ります。
フィールド | タイプ | 必要性 | 説明 |
|---|---|---|---|
| string | 必須 | 現在のデータベースで暗号化されたコレクションの名前。 |
| ドキュメント | 必須 | 暗号化されたコレクションを構成するオプション。 |
| string | 必須 | CMK の保存に使用している KMS。 |
| ドキュメント | 必須 | 暗号化されたフィールド。 |
| ドキュメント | 任意 | KMS プロバイダーがAWS、 GCP、またはAzureである場合に、マスター キーにアクセスするために必要な認証情報とキー識別フィールドを指定します。KMS プロバイダーが |
動作
The mongosh client-side field level and queryable encryption methods require a database connection configured for client-side encryption. If the current database connection was not initiated with client-side field level encryption enabled, either:
必要なクライアント側フィールドレベル暗号化オプションとの接続を確立するには、
mongoshからMongo()コンストラクターを使用します。Mongo()メソッドは、CMK(Customer Master Key)管理用に次のKMS ( KMS )プロバイダーをサポートしています。
or
- Use the
mongoshcommand line options to establish a connection with the required options. The command line options only support the Amazon Web Services KMS provider for CMK management.
ラッパーの動作
The db.createEncryptedCollection() method runs in the context of the current db and uses the same Queryable Encryption-enabled connection as the current mongosh session. Internally, it calls ClientEncryption.createEncryptedCollection() with the current database name, the specified collection name, and the provided options document.
options.createCollectionOptions.encryptedFields.fields[*].keyId が null または省略されている場合、メソッドはキーヴォールトに必要なデータキーを自動的に作成し、作成されたコレクションの encryptedFields 定義に対応する keyId 値を入力します。
例
次の例では、Queryable Encryption 構成にローカルで管理されている KMS を使用しています。
暗号化された接続の作成
mongosh を起動する
実行:
mongosh --nodb --nodbは、データベースに接続しないことを意味します。キーstringの生成
基本64 96バイトの string を生成する:
const TEST_LOCAL_KEY = require("crypto").randomBytes(96).toString("base64") 暗号化オプション オブジェクトの作成
クライアント側のフィールドレベル暗号化オプションオブジェクトを作成するには、前のステップの
TEST_LOCAL_KEYstring を使用します。var autoEncryptionOpts = { "keyVaultNamespace" : "encryption.__dataKeys", "kmsProviders" : { "local" : { "key" : BinData(0, TEST_LOCAL_KEY) } } } 暗号化されたクライアント オブジェクトの作成
暗号化されたクライアントオブジェクトを作成するには、
Mongo()コンストラクターを使用します。mongodb://myMongo.example.netURI を、ターゲットクラスターの接続string URIに置き換えます。 (例: )。encryptedClient = Mongo( "mongodb://myMongo.example.net:27017/?replSetName=myMongo", autoEncryptionOpts )
結果オブジェクトを確認する
db.createEncryptedCollection() は、作成されたコレクション名とコレクションの encryptedFields 定義を含む結果オブジェクトを返します。
result.collection の値をチェックして、コレクションが目的のロケーションに作成されていることを確認し、オプションで入力された encryptedFields を検査します。
enc> result.collection enc.users enc> result.encryptedFields { fields: [ { path: "secretField", bsonType: "string", queries: { queryType: "equality", /* ... */ }, keyId: UUID("...") } ] }
詳細
クライアント側のフィールドレベル暗号化を有効にして MongoDB 接続を開始する方法に関する詳細なドキュメントについては、
Mongo()を参照してください。暗号化されたコレクションの作成およびクエリ方法の完全な例については、 Queryable Encryptionクイック スタート を参照してください。