[英]Mongoose encryption
如何加密子文檔除了子文檔中的特定字段?
我正在嘗試使用mongoose-encryption插件在以下架構上實現加密。 我的父模式即“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本身並不完全支持嵌套的嵌套數組。
如果您在其中一個級別引用子級而不是直接將它們包含在子級中,它會工作嗎? 例如:
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
。
使用子文檔與貓鼬加密的更多細節的文檔
披露:我是插件作者
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.