|
| 1 | +============================================== |
| 2 | +MongoDB\\Database::createEncryptedCollection() |
| 3 | +============================================== |
| 4 | + |
| 5 | +.. versionadded:: 1.16 |
| 6 | + |
| 7 | +.. default-domain:: mongodb |
| 8 | + |
| 9 | +.. contents:: On this page |
| 10 | + :local: |
| 11 | + :backlinks: none |
| 12 | + :depth: 1 |
| 13 | + :class: singlecol |
| 14 | + |
| 15 | +Definition |
| 16 | +---------- |
| 17 | + |
| 18 | +.. phpmethod:: MongoDB\\Database::createEncryptedCollection() |
| 19 | + |
| 20 | + Explicitly creates an encrypted collection. |
| 21 | + |
| 22 | + .. code-block:: php |
| 23 | + |
| 24 | + function createEncryptedCollection(string $collectionName, ClientEncryption $clientEncryption, string $kmsProvider, ?array $masterKey, array $options = []): array |
| 25 | + |
| 26 | + This method wraps :phpmethod:`MongoDB\\Database::createCollection()` with |
| 27 | + additional functionality. Unlike ``createCollection()`, this method requires |
| 28 | + that the ``encryptedFields`` option be specified. |
| 29 | + |
| 30 | + This function will automatically create data keys for any encrypted fields |
| 31 | + where the ``keyId`` option is ``null``. Data keys will be created using |
| 32 | + :php:`MongoDB\\Driver\\ClientEncryption::createDataKey() <mongodb-driver-clientencryption.createdatakey>` |
| 33 | + and the provided ``$kmsProvider`` and ``$masterKey`` parameters. |
| 34 | + |
| 35 | + This method does not affect any auto encryption settings on existing |
| 36 | + :phpclass:`MongoDB\\Client` objects. Users must configure auto encryption |
| 37 | + after creating the encrypted collection with ``createEncryptedCollection()``. |
| 38 | + |
| 39 | + This method has the following parameters: |
| 40 | + |
| 41 | + .. include:: /includes/apiargs/MongoDBDatabase-method-createEncryptedCollection-param.rst |
| 42 | + |
| 43 | + The ``$options`` parameter supports the following options: |
| 44 | + |
| 45 | + .. include:: /includes/apiargs/MongoDBDatabase-method-createEncryptedCollection-option.rst |
| 46 | + |
| 47 | + Note that not all options are available on all versions of MongoDB. Refer to |
| 48 | + the :manual:`create </reference/command/create>` command reference in the |
| 49 | + MongoDB manual for compatibility considerations. |
| 50 | + |
| 51 | +Return Values |
| 52 | +------------- |
| 53 | + |
| 54 | +A tuple consisting of the result from |
| 55 | +:phpmethod:`MongoDB\\Database::createCollection()` and the modified |
| 56 | +``encryptedFields`` option. |
| 57 | + |
| 58 | +Errors/Exceptions |
| 59 | +----------------- |
| 60 | + |
| 61 | +:phpclass:`MongoDB\\Driver\\Exception\\CreateEncryptedCollectionException` if |
| 62 | +any error is encountered while creating data keys or invoking |
| 63 | +``createCollection()``. The original exception and modified ``encryptedFields`` |
| 64 | +option can be accessed via ``getPrevious()`` and ``getEncryptedFields()``, |
| 65 | +respectively. |
| 66 | + |
| 67 | +.. include:: /includes/extracts/error-invalidargumentexception.rst |
| 68 | + |
| 69 | +Example |
| 70 | +------- |
| 71 | + |
| 72 | +The following example creates an encrypted ``users`` collection in the ``test`` |
| 73 | +database. The ``ssn`` field within the ``users`` collection will be defined as |
| 74 | +an encrypted string field. |
| 75 | + |
| 76 | +.. code-block:: php |
| 77 | + |
| 78 | + <?php |
| 79 | + |
| 80 | + // 96-byte master key used to encrypt/decrypt data keys |
| 81 | + define('LOCAL_MASTERKEY', '...'); |
| 82 | + |
| 83 | + $client = new MongoDB\Client; |
| 84 | + |
| 85 | + $clientEncryption = $client->createClientEncryption([ |
| 86 | + 'keyVaultNamespace' => 'keyvault.datakeys', |
| 87 | + 'kmsProviders' => [ |
| 88 | + 'local' => ['key' => new MongoDB\BSON\Binary(base64_decode(LOCAL_MASTERKEY), 0)], |
| 89 | + ], |
| 90 | + ); |
| 91 | + |
| 92 | + [$result, $encryptedFields] = $client->test->createEncryptedCollection( |
| 93 | + 'users', |
| 94 | + $clientEncryption, |
| 95 | + 'local', |
| 96 | + null, |
| 97 | + [ |
| 98 | + 'encryptedFields' => [ |
| 99 | + 'fields' => [ |
| 100 | + ['path' => 'ssn', 'bsonType' => 'string', 'keyId' => null], |
| 101 | + ], |
| 102 | + ], |
| 103 | + ] |
| 104 | + ); |
| 105 | + |
| 106 | +If the encrypted collection was successfully created, ``$result`` will contain |
| 107 | +the return value from :phpmethod:`MongoDB\\Database::createCollection()` (i.e. |
| 108 | +:manual:`create </reference/command/create>` command response) and |
| 109 | +``$encryptedFields['fields'][0]['keyId']`` will contain the return value from |
| 110 | +:php:`MongoDB\\Driver\\ClientEncryption::createDataKey() <mongodb-driver-clientencryption.createdatakey>` |
| 111 | +(i.e. a :php:`MongoDB\\BSON\\Binary <class.mongodb-bson-binary>` with subtype 4, |
| 112 | +UUID). |
| 113 | + |
| 114 | +See Also |
| 115 | +-------- |
| 116 | + |
| 117 | +- :phpmethod:`MongoDB\\Database::createCollection()` |
| 118 | +- :phpmethod:`MongoDB\\Client::createClientEncryption()` |
| 119 | +- :php:`MongoDB\\Driver\\ClientEncryption::createDataKey() <mongodb-driver-clientencryption.createdatakey>` |
| 120 | +- :manual:`create </reference/command/create>` command reference in the MongoDB |
| 121 | + manual |
0 commit comments