You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 two 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
+
constencryptedUserSchema=newSchema({
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:'queryableEncryption' });
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.
Copy file name to clipboardExpand all lines: lib/schema.js
+100-1Lines changed: 100 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -86,6 +86,7 @@ const numberRE = /^\d+$/;
86
86
* - [pluginTags](https://mongoosejs.com/docs/guide.html#pluginTags): array of strings - defaults to `undefined`. If set and plugin called with `tags` option, will only apply that plugin to schemas with a matching tag.
87
87
* - [virtuals](https://mongoosejs.com/docs/tutorials/virtuals.html#virtuals-via-schema-options): object - virtuals to define, alias for [`.virtual`](https://mongoosejs.com/docs/api/schema.html#Schema.prototype.virtual())
88
88
* - [collectionOptions]: object with options passed to [`createCollection()`](https://www.mongodb.com/docs/manual/reference/method/db.createCollection/) when calling `Model.createCollection()` or `autoCreate` set to true.
89
+
* - [encryptionType]: the encryption type for the schema. Valid options are `csfle` or `queryableEncryption`. See https://mongoosejs.com/docs/field-level-encryption.
89
90
*
90
91
* #### Options for Nested Schemas:
91
92
*
@@ -128,6 +129,7 @@ function Schema(obj, options) {
128
129
// For internal debugging. Do not use this to try to save a schema in MDB.
129
130
this.$id=++id;
130
131
this.mapPaths=[];
132
+
this.encryptedFields={};
131
133
132
134
this.s={
133
135
hooks: newKareem()
@@ -463,6 +465,8 @@ Schema.prototype._clone = function _clone(Constructor) {
0 commit comments