Encryption
Encryption protects data from being read by anyone without the key — even if they steal the disk, sniff the network, or dump a backup.
Introduction
Encryption protects data from being read by anyone without the key — even if they steal the disk, sniff the network, or dump a backup. MongoDB supports three independent layers: encryption in transit (TLS), encryption at rest (storage engine), and client-side field-level encryption (CSFLE / Queryable Encryption) which keeps fields encrypted even from the DBA.
Defense in depth means turning on all three — they protect against different threats and have very different operational costs.
Understanding the topic
The three layers — what each protects against:
- TLS (in transit) — stops network sniffing and MITM. Mandatory on Atlas; one flag on self-hosted.
- Encryption at rest — stops disk/backup theft. WiredTiger + KMIP/KMS-managed key (AWS KMS, Azure KV, GCP KMS).
- CSFLE — fields encrypted in the driver before sending to the server. DBAs cannot read SSNs even with full cluster access.
- Queryable Encryption (8.0+) — equality and range queries on encrypted fields without ever decrypting server-side.
- Key hierarchy — CMK (in KMS) wraps DEKs (per field/collection) — rotating CMK is instant, no re-encryption.
Informative example
Client-side field-level encryption — Node driver:
const client = new MongoClient(uri, {autoEncryption: {keyVaultNamespace: "encryption.__keyVault",kmsProviders: { aws: { accessKeyId, secretAccessKey } },schemaMap: {"clinic.patients": {properties: {ssn: {encrypt: {keyId: [dataKeyId],bsonType: "string",algorithm: "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic" // queryable}}}}}}});// Insert — driver encrypts ssn transparently before sendingawait db.collection("patients").insertOne({ name: "Ada", ssn: "123-45-6789" });// Query by encrypted field — works because of Deterministic algorithmawait db.collection("patients").findOne({ ssn: "123-45-6789" });
Real-world use
Banks store account numbers, PHI providers store diagnoses, and SaaS multi-tenant systems isolate tenant secrets all using CSFLE — the auditor sees only opaque binary blobs, and a rogue DBA cannot exfiltrate anything meaningful.
Best practices
- TLS-only — set
tlsAllowInvalidCertificates: falsein production. - Store CMKs in a managed KMS, never on the application server.
- Use Queryable Encryption for new projects on 8.0+ — better range support than legacy CSFLE.
- Rotate DEKs after any insider incident; CMK rotation is cheap, do it yearly.
Common mistakes
- Encrypting everything — query patterns break and indexes become useless.
- Storing the KMS key alongside the app secrets — same blast radius as no encryption.
- Forgetting that backup snapshots are also encrypted — losing the key loses the data.