---
name: "mongoc_client_encryption_create_encrypted_collection"
section: "3"
platform: "linux"
package: "libmongoc-doc"
version: "1.30.4-1+deb13u3"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/mongoc_client_encryption_create_encrypted_collection"
---

# mongoc\_client\_encryption\_create\_encrypted\_collection(3)

## SYNOPSIS

```
mongoc_collection_t*
mongoc_client_encryption_create_encrypted_collection (
      mongoc_client_encryption_t *enc,
      mongoc_database_t *database,
      const char *name,
      const bson_t *in_options,
      bson_t *out_options,
      const char *kms_provider,
      const bson_t *opt_masterKey,
      bson_error_t *error);
```

Create a new collection with *Queryable Encryption* enabled. Requires a valid *mongoc\_client\_encryption\_t* object to operate.

Added in version 1.24.0.

**SEE ALSO:**

This function is a convenience API wrapping *mongoc\_database\_create\_collection()*.

## PARAMETERS

- •

  **enc**: The *mongoc\_client\_encryption\_t* to be used to configure encryption for the new collection.

- •

  **database**: The *mongoc\_database\_t* in which the new collection will be created.

- •

  **name**: The name of the new collection.

- •

  **in\_options**: The options for the new collection. (See below).

- •

  **out\_options**: An optional output option for the final create-collection options. Should point to storage for a *bson\_t*. The pointed-to object must be destroyed by the caller. If **NULL**, has no effect.

- •

  **kms\_provider**: The name of the KMS provider to use for generating new data encryption keys for encrypted fields within the collection.

- •

  **opt\_masterKey**: If provided, used as the masterkey option when data encryption keys need to be created. (See: *mongoc\_client\_encryption\_datakey\_opts\_set\_masterkey()*)

- •

  **error**: Optional output parameter pointing to storage for a *bson\_error\_t*. If an error occurs, will be initialized with error information.

## RETURNS

If successful, this function returns a new *mongoc\_collection\_t* object. Upon failure, returns **NULL** and initializes **\*error** with an error indicating the reason for failure. The returned collection object must be freed by the caller.

## CREATION OPTIONS

The **in\_options** parameter behaves similarly to the **opts** parameter for *mongoc\_database\_create\_collection()*, which accepts the options for the **create** MongoDB command (*Documented here*). The **in\_options** document accepted here is different in one important way:

The **$.encryptedFields.fields** array is *required* by this function, and, unlike the schema documented for the **create** command, accepts a value of **null** for the **keyId** parameter on each array element.

This function has the following as-if effect:

- 1\.

  A new set of options O will be created based on **in\_options**.

- 2\.

  For each element F in the **$.encryptedFields.fields** array of O:

  - 1\.

    If F contains a **"keyId": null** element, a new data encryption key K\_f will be created as-if by calling the *mongoc\_client\_encryption\_create\_datakey()*, using the relevant arguments that were given to **mongoc\_client\_encryption\_create\_encrypted\_collection**.

  - 2\.

    The ID of K\_f will be used to replace the **"keyId": null** element within F.

- 3\.

  A collection will be created using the options O.

- 4\.

  If **out\_options** is not **NULL**, O will be written to **out\_options**.

## AUTHOR

MongoDB, Inc

## COPYRIGHT

2009-present, MongoDB, Inc.
