Skip to content

Commit 18d4315

Browse files
add docs
1 parent 6c469ec commit 18d4315

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

docs/field-level-encryption.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,41 @@ With the above connection, if you create a model named 'Test' that uses the 'tes
112112
const Model = mongoose.model('Test', mongoose.Schema({ name: String }));
113113
await Model.create({ name: 'super secret' });
114114
```
115+
116+
## Automatic FLE in Mongoose
117+
118+
Mongoose supports the declaration of encrypted schemas - schemas that, when connected to a model, utilize MongoDB's Client Side
119+
Field Level Encryption or Queryable Encryption under the hood. Mongoose automatically generates either an `encryptedFieldsMap` or a
120+
`schemaMap` when instantiating a MongoClient and encrypts fields on write and decrypts fields on reads.
121+
122+
### Encryption types
123+
124+
MongoDB has to different automatic encryption implementations: client side field level encryption (CSFLE) and queryable encryption (QE).
125+
See [choosing an in-use encryption approach](https://www.mongodb.com/docs/v7.3/core/queryable-encryption/about-qe-csfle/#choosing-an-in-use-encryption-approach).
126+
127+
### Declaring Encrypted Schemas
128+
129+
The following schema declares two properties, `name` and `ssn`. `ssn` is encrypted using queryable encryption, and
130+
is configured for equality queries:
131+
132+
```javascript
133+
const encryptedUserSchema = new Schema({
134+
name: String,
135+
ssn: {
136+
type: String,
137+
// 1
138+
encrypt: {
139+
keyId: '<uuid string of key id>',
140+
queries: 'equality'
141+
}
142+
}
143+
// 2
144+
}, { encryptionType: 'queryable encryption' });
145+
```
146+
147+
To declare a field as encrypted, you must:
148+
149+
1. Annotate the field with encryption metadata in the schema definition
150+
2. Choose an encryption type for the schema and configure the schema for the encryption type
151+
152+
Not all schematypes are supported for CSFLE and QE. For an overview of valid schema types, refer to MongoDB's documentation.

0 commit comments

Comments
 (0)