When you use Queryable Encryption, you define encrypted fields at the collection level using an encryption schema. Encrypting a field and enabling queries increases storage requirements and impacts query performance. You can configure an encrypted field for either equality or range queries, but not both. Configure fields for the expected query type.
For instructions on creating an encryption schema and configuring querying, see Create an Encryption Schema.
Supported Query Types and Behavior
For a list of supported query operators and behavior with encrypted fields, see Supported Query Operators.
Client and Server Schema Validation
MongoDB supports using schema validation to enforce encryption of specific fields in a collection. Clients using automatic Queryable Encryption behave differently depending on the database connection configuration:
If the connection
encryptedFieldsMapobject contains a key for the specified collection, the client uses that object to perform automatic Queryable Encryption, rather than using the remote schema. At minimum, the local rules must encrypt all fields that the remote schema does.If the connection
encryptedFieldsMapobject doesn't contain a key for the specified collection, the client downloads the server-side remote schema for the collection and uses it instead.Important
Remote Schema Behavior
When using a remote schema:
The client trusts that the server has a valid schema
The client uses the remote schema to perform automatic Queryable Encryption only. The client does not enforce any other validation rules specified in the schema.
Considerations when Enabling Querying
You can make an encrypted field queryable. To change which fields are encrypted or queryable, rebuild the collection's encryption schema and re-create the collection.
If you don't need to query an encrypted field, you may not need to enable querying on that field. You can still retrieve the document by querying other fields that are queryable or unencrypted.
For every encrypted collection, MongoDB creates two metadata collections, increasing storage space. MongoDB creates an index for each encrypted field, which increases the duration of write operations on that field. When a write operation updates an indexed field, MongoDB updates the related index.
Configure Encrypted Fields for Optimal Search and Storage
MongoDB provides the following parameters to facilitate debugging and performance tuning:
min, maxQuery Type: Range queries only.
Type: Must match the field's
bsonType.Required if
bsonTypeisdecimalordouble. Optional but highly recommended if it isint,long, ordate. Defaults to thebsonTypemin and max values.Specify minimum and maximum (inclusive) queryable values for a field when possible, as smaller bounds improve query efficiency. If querying values outside of these bounds, MongoDB returns an error.
Important
The sparsity, precision, trimFactor, and contention parameters are intended for advanced users only. The default values for these options are suitable for the majority of use cases, and should only be modified if your use case requires it.
sparsityQuery Type: Range queries only.
Type: Integer from
1-4.Optional. Defaults to
2.Affects how thoroughly MongoDB indexes range values. Low sparsity (dense indexing) improves query performance, but stores more documents in the encrypted metadata collections for each insert or update operation, causing greater storage overhead. High sparsity does the opposite.
precisionQuery Type: Range queries only.
Type: Integer.
Optional. Allowed only if
bsonTypeisdoubleordecimal. If unset, MongoDB uses the same maximum precision as thebsonType, eitherdoubleordecimal.Limits how many digits after the decimal point are taken into account when querying a
doubleordecimalfield. Additional digits are dropped, not rounded. For example, aprecisionof1treats10.18as10.1for queries. The encrypted value is still stored as10.18.Specify
precisionand limit it when possible. Every digit increases storage overhead and has a high impact on searchable range and index generation.
trimFactorQuery Type: Range queries only.
Type: Integer.
Optional. Defaults to
6.The
trimFactorcontrols the throughput of concurrent inserts and updates. A highertrimFactorincreases the throughput of concurrent insert and updates at the cost of slowing down some range read operations. A lowertrimFactordoes the opposite.
contentionQuery Type: Equality and range queries.
Type: Integer.
Optional. Defaults to
8.Concurrent write operations, such as inserting the same field/value pair into multiple documents in close succession, can cause contention: conflicts that delay operations.
With Queryable Encryption, MongoDB tracks the occurrences of each field/value pair in an encrypted collection using an internal counter. The contention factor partitions this counter, similar to an array. This minimizes issues with incrementing the counter when using
insert,update, orfindAndModifyto add or modify an encrypted field with the same field/value pair in close succession.contention = 0creates an array with one element at index 0.contention = 4creates an array with 5 elements at indexes 0-4. MongoDB increments a random array element during insert.When unset,
contentiondefaults to8, which provides high performance for most workloads. Higher contention improves the performance of insert and update operations on low cardinality fields, but decreases find performance.You can optionally include
contentionon queryable fields to change the value from its default of8.For more thorough information on contention factor and its cryptographic implications, see "Section 9: Guidelines" in MongoDB's Queryable Encryption Technical Paper.