This guide is for developers and database admins who need to protect sensitive fields in MongoDB, such as ID numbers or health records. Standard encryption protects the files on disk, but anyone who can log in to the server as an admin still sees the plain values. Client-side field-level encryption (CSFLE) solves this. It encrypts chosen fields inside the MongoDB driver, the library your application uses to talk to the database, before the data leaves your application server. The database only ever stores scrambled values, so even a full server breach exposes nothing readable for those fields. Queryable Encryption, generally available since MongoDB 7.0, goes a step further and lets you search encrypted fields by exact value or by range. You will learn how the encryption keys fit together and how a JSON schema (a small document listing which fields to encrypt and how) drives automatic encryption. You will also see how to choose between deterministic, random and queryable modes.
How to verify
Confirm a field is actually encrypted on the wire by reading the BSON without the encryption key:
mongosh --host mongo.example.internal:27017
use shop
db.users.findOne()
db.users.find({}, { ssn: 1 }).limit(1)
db.runCommand({ collMod: "users" })
The ssn value should appear as BinData(6, ...) — a binary subtype 6 (encrypted). If you see plaintext, the driver is not encrypting; the schema or the key vault is misconfigured.
What’s happening
CSFLE has a three-level key hierarchy. The customer master key (CMK) lives in your KMS (AWS KMS, Azure Key Vault, GCP KMS, KMIP, or local). The CMK wraps data encryption keys (DEKs) that live in a MongoDB collection (the “key vault”). Each protected field is encrypted with a DEK, and the DEK is decrypted on demand by the driver after fetching it from the key vault and unwrapping it via the KMS.
Encryption is driven by a JSON schema that maps each protected field to its encryption algorithm and DEK. The schema can live in the driver’s local config (schemaMap) or attached server-side via encryptedFieldsMap (Queryable Encryption requires the server-side form). Automatic encryption rewrites queries: a find({ ssn: "123-45-6789" }) becomes a find({ ssn: BinData(6, "<ciphertext>") }) after the driver computes the deterministic ciphertext for the plaintext value.
Three encryption algorithms exist. Deterministic (AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic) produces the same ciphertext for the same plaintext, so equality queries work — at the cost of leaking ciphertext equality patterns. Random (AEAD_AES_256_CBC_HMAC_SHA_512-Random) randomizes each encryption, so queries against the field are not possible. Queryable Encryption (7.0+) uses a structured-encryption scheme that supports equality and range queries against encrypted values while leaking less than deterministic encryption.
The procedure
-
Provision the CMK in your KMS.
aws kms create-key --description "MongoDB CSFLE CMK prod" -
Create the key vault collection with a unique index on
keyAltNames.use encryption db.__keyVault.createIndex( { keyAltNames: 1 }, { unique: true, partialFilterExpression: { keyAltNames: { $exists: true } } } ) -
Create a DEK referencing the CMK.
const ce = Mongo("...").getClientEncryption({ keyVaultNamespace: "encryption.__keyVault", kmsProviders: { aws: { accessKeyId: "...", secretAccessKey: "..." } } }) ce.createKey("aws", { region: "us-east-1", key: "arn:aws:kms:..." }, ["ssn-key"]) -
Define the encryption schema. Pin each field to a DEK and an algorithm.
{ "bsonType": "object", "properties": { "ssn": { "encrypt": { "keyId": "/ssn-key", "bsonType": "string", "algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic" } } } } -
Configure the driver with the schema and KMS credentials. From this point, writes to
users.ssnare encrypted automatically and queries onssnwork transparently.const client = new MongoClient(uri, { autoEncryption: { keyVaultNamespace: "encryption.__keyVault", kmsProviders, schemaMap: { "shop.users": schema } } }) -
For Queryable Encryption, configure
encryptedFieldsat collection creation time.db.createCollection("users", { encryptedFields: { fields: [{ keyId: UUID("..."), path: "ssn", bsonType: "string", queries: { queryType: "equality" } }] } })
Operational notes
- DEK rotation is per-key, not per-collection.
rewrapManyDataKeyre-encrypts the DEK with a new CMK version without re-encrypting the underlying field data — fast, but means the same DEK is still in use. - Deterministic encryption leaks distribution. If 70% of users have a specific country, the ciphertext for that country appears 70% of the time. An attacker with read access to the encrypted column can infer values.
- Queryable Encryption requires Enterprise or Atlas. Community Edition does not include the server-side query support. Plan licensing accordingly.
- The key vault is now a critical asset. Losing the DEK collection is equivalent to losing the data. Replicate it, back it up, and treat it as having the same RTO/RPO requirements as the protected data.
- Performance overhead is per-field. Encrypt the fields that actually need protection (PII, secrets), not the entire document. Each encrypted field adds CPU work on every read and write.
Stack Harbor designs CSFLE and Queryable Encryption deployments for clients in healthcare, financial services, and regulated industries — KMS integration, key vault replication, schema management, and the application-side rollout that adds encryption without breaking existing queries. See /en/services/managed-operations/ for the engagement model.