如何加密不包含子文档中特定字段的子文档?
我正在尝试使用 mongoose-encryption plugin 在以下架构上实现加密.我的父模式即“parentSchema”被加密,但不是子模式。我需要加密“childSchema”和“childinformationSchema”。我在这里缺少什么?
var childinformationSchema = new Schema({
otherwitnes: String,
reportedemployOther: String,
status: String,
updateddate: Date,
updatedby: String
});
childinformationSchema.plugin(encrypt, {
key: encryptionKey,
exclude: ['status', 'updateddate', 'updatedby']
});
var childSchema = new Schema({
childdata: {
childinformation: [childinformationSchema]
}
});
childSchema.plugin(encrypt.encryptedChildren, {
key: encryptionKey
});
var parentSchema = new Schema({
practicename: String,
createddate: Date,
createdby: String,
updateddate: Date,
updatedby: String,
patientrecordno: String,
state: String,
child: [childSchema]
});
parentSchema.plugin(
encrypt.encryptedChildren,
{
key: encryptionKey,
exclude: ['createddate', 'createdby', 'updateddate', 'updatedby', 'state', 'patientrecordno', 'child']
}
);
最佳答案
在您的用例中,您有子文档的子文档。从一些测试来看,Mongoose 似乎并不完全支持子子文档上的中间件,因此如果不对您的架构进行一些重构,该插件将无法工作。总的来说,这可能是个好主意,因为 MongoDB 本身 doesn't have full support对于嵌套的嵌套数组。
如果您在其中一个级别引用子项而不是直接将它们作为子文档包括在内,是否可行?例如:
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 children
childSchema.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']
});
同样,您可以让 childSchema
直接嵌套在 parentSchema
中,而是通过引用包含 childinformationSchema
。
在 the docs 中使用带 mongoose 加密的子文档的更多详细信息
披露:我是插件作者
关于javascript - Mongoose 加密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28291516/