Mongoose encryption Mongoose encryption mongoose mongoose

Mongoose encryption


In your use case you have subdocuments of subdocuments. From some testing, Mongoose doesn't appear to fully support middleware on sub-subdocuments and so this plugin won't work without restructuring your schema some. This might be a good idea in general because MongoDB itself doesn't have full support for nested nested arrays.

Would it work if you referenced the children at one of the levels instead of including them directly as a subdoc? For example:

childinformationSchema.plugin(encrypt, {    encryptionKey: encryptionKey,    authenticationKey: authenticationKey, // latest version adds authentication    excludeFromEncryption: ['status', 'updateddate', 'updatedby']});var childSchema = new Schema({    childinformation: [childinformationSchema]});// because childSchema itself has encrypted childrenchildSchema.plugin(encrypt.encryptedChildren);var parentSchema = new Schema({    ...    child: [type: mongoose.Schema.Types.ObjectId, ref: 'Child']});parentSchema.plugin(encrypt, {     key: encryptionKey,    excludeFromEncryption: ['createddate', 'createdby', 'updateddate', 'updatedby', 'state', 'patientrecordno', 'child']});

Similarly, you could leave childSchema nested directly inside parentSchema and include childinformationSchema by reference instead.

More details on using subdocuments with mongoose-encryption in the docs

Disclosure: I am the plugin author