如何加密子文档中不包含特定字段的子文档?
我正在尝试使用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']
}
);
答案 0 :(得分:1)
在您的用例中,您有子文档的子文档。从某些测试来看,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-encryption的子文档的更多详细信息披露:我是插件作者