AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。
Docs Menu

ClientEncryption.createEncryptedCollection()(mongoshメソッド)

バージョン7.0の新機能。

ClientEncryption.createEncryptedCollection(dbName, collName, clientEncOpts)

ClientEncryption.createEncryptedCollection は、 dbNameで指定されたデータベースにcollNameで指定された暗号化コレクションを作成します。

Tip

In mongosh, you can use the helper db.createEncryptedCollection(), which runs in the context of the current database and automatically generates data keys when keyId values are not specified.

このコマンドは、次の環境でホストされている配置で使用できます。

  • MongoDB Atlas はクラウドでの MongoDB 配置のための完全管理サービスです
  • MongoDB Enterprise: サブスクリプションベースの自己管理型 MongoDB バージョン

  • MongoDB Community: ソースが利用可能で、無料で使用できる自己管理型の MongoDB のバージョン

ClientEncryption.createEncryptedCollection の構文は次のとおりです。

clientEncryption = db.getMongo().getClientEncryption()
clientEncryption.createEncryptedCollection(
dbName,
collName,
{
provider: kmsProviderName,
createCollectionOptions: encryptedFieldsMap,
masterKey: customerMasterKeyCredentials
}
)

createEncryptedCollection 次のフィールドを取ります。

フィールド
タイプ
必要性
説明

dbName

string

必須

暗号化するデータベースの名前。

collName

string

必須

暗号化するコレクションの名前。

clientEncOpts

ドキュメント

必須

暗号化されたコレクションを構成するオプション。

clientEncOpts.provider

string

必須

CMK の保存に使用している KMS。

clientEncOpts.createCollectionOptions

ドキュメント

必須

暗号化するフィールド。 encryptedFieldsMapオブジェクトの構成方法の詳細については、「暗号化のフィールドの指定 」を参照してください。

clientEncOpts.masterKey

ドキュメント

任意

KMS プロバイダーがAWS、 GCP、またはAzureである場合に、マスター キーにアクセスするために必要な認証情報とキー識別フィールドを指定します。KMS プロバイダーが local の場合は任意。

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:

or

次の例では、Queryable Encryption 構成にローカルで管理されている KMS を使用しています。

1
  1. mongosh を起動する

    実行:

    mongosh --nodb

    --nodb は、データベースに接続しないことを意味します。

  2. キーstringの生成

    基本64 96バイトの string を生成する:

    const TEST_LOCAL_KEY = require("crypto").randomBytes(96).toString("base64")
  3. 暗号化オプション オブジェクトの作成

    クライアント側のフィールドレベル暗号化オプションオブジェクトを作成するには、前のステップのTEST_LOCAL_KEY string を使用します。

    var autoEncryptionOpts = {
    "keyVaultNamespace" : "encryption.__dataKeys",
    "kmsProviders" : {
    "local" : {
    "key" : BinData(0, TEST_LOCAL_KEY)
    }
    }
    }
  4. 暗号化されたクライアント オブジェクトの作成

    暗号化されたクライアントオブジェクトを作成するには、 Mongo()コンストラクターを使用します。 mongodb://myMongo.example.net URI を、ターゲットクラスターの接続string URIに置き換えます。 (例: )。

    encryptedClient = Mongo(
    "mongodb://myMongo.example.net:27017/?replSetName=myMongo",
    autoEncryptionOpts
    )
2

暗号化するフィールドを指定するには、次のようにencryptedFieldsMapsを作成します。

const encryptedFieldsMap = {
encryptedFields: {
fields: [
{
path: "secretField",
bsonType: "string",
queries: { queryType: "equality" },
},
],
},
};
3

暗号化されたenc.usersコレクションを作成します。

clientEncryption = encryptedClient.getClientEncryption();
let result = clientEncryption.createEncryptedCollection(
"enc",
"users",
{
provider: "local",
createCollectionOptions: encryptedFieldsMap,
masterKey: {} // masterKey is optional when provider is local
}
)
4

createEncryptedCollection は、多数のフィールドを持つ大きな結果オブジェクトを返します。 result.collectionの値を確認して、コレクションが目的の場所で作成されたことを確認します。

enc> result.collection
enc.users