Docs Menu

Docs HomeMongoDB Manual


On this page

  • Behavior
  • Example

New in version 4.2.

ClientEncryption.encrypt(encryptionKeyId, value, encryptionAlgorithm)

ClientEncryption.encrypt() encrypts the value using the specified encryptionKeyId and encryptionAlgorithm. encrypt() supports explicit (manual) encryption of field values.

encrypt() has the following syntax:

clientEncryption = db.getMongo().getClientEncryption()

The data encryption key to use for encrypting the value.

The UUID is a BSON binary data object with subtype 4 that identifies a specific data encryption key. If the data encryption key does not exist in the key vault configured for the database connection, encrypt() returns an error. See Key Vault Collections for more information on key vaults and data encryption keys.

The value to encrypt.
encryptionAlgorithm or encryptionOptions
string or document
  • When specifying encryptionAlgorithm:

    The encryption algorithm to use for encrypting the value.

    • AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic

    • AEAD_AES_256_CBC_HMAC_SHA_512-Random

    • Indexed

    • Unindexed

    For complete documentation on the supported encryption algorithms, see Fields and Encryption Types.

  • When specifying encryptionOptions:

    • algorithm: The encryption algorithm to use for encrypting the value.

    • keyId: ID of the data decryption key.

    • contentionFactor: Required when algorithm is set to Indexed. Related to the frequency of the values for this field.

    • queryType: The only query type currently supported is "equality". queryType must be set when algorithm is not Indexed.

Returns:A binary data object with subtype 6.

The mongo client-side field level encryption methods require a database connection with client-side field level encryption enabled. If the current database connection was not initiated with client-side field level encryption enabled, either:

encrypt() does not supports encrypting values with the following BSON types:

  • minKey

  • maxKey

  • null

  • undefined

If encrypting a field using AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic, encrypt() does not support the following BSON types:

  • double

  • decimal128

  • bool

  • object

  • array

  • javascriptWithScope (Deprecated)

The following example uses a locally managed KMS for the client-side field level encryption configuration.

Configuring client-side field level encryption for a locally managed key requires specifying a base64-encoded 96-byte string with no line breaks. The following operation generates a key that meets the stated requirements and loads it into the mongo shell:

TEST_LOCAL_KEY=$(echo "$(head -c 96 /dev/urandom | base64 | tr -d '\n')")
mongosh --nodb --shell --eval "var TEST_LOCAL_KEY='$TEST_LOCAL_KEY'"

Create the client-side field level encryption object using the generated local key string:

var autoEncryptionOpts = {
"keyVaultNamespace" : "encryption.__dataKeys",
"kmsProviders" : {
"local" : {
"key" : BinData(0, TEST_LOCAL_KEY)

Use the Mongo() constructor to create a database connection with the client-side field level encryption options. Replace the mongodb:// URI with the connection string URI of the target cluster.

encryptedClient = Mongo(

Retrieve the ClientEncryption object and use the ClientEncryption.encrypt() method to encrypt a value using a specific data encryption key UUID and encryption algorithm:

clientEncryption = encryptedClient.getClientEncryption();

If sucessful, encrypt() returns the encrypted value:


For complete documentation on initiating MongoDB connections with client-side field level encryption enabled, see Mongo().

←  getClientEncryption()ClientEncryption.decrypt() →

On this page

Share Feedback
© 2023 MongoDB, Inc.


  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2023 MongoDB, Inc.