javascript - Mongoose 加密

标签 javascript node.js mongodb encryption mongoose

如何加密不包含子文档中特定字段的子文档?

我正在尝试使用 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/

相关文章:

mongodb - mongo 和 mongodump 与 auth 一起工作,但 restore 不

javascript - GA 使用数组作为关联吗?

javascript - 从 jQuery .val() 添加文本时如何更新 MarkdownDeep 的实时预览?

linux - 如何在 Node.js 中使用 EJS 而不是 JADE

node.js - 为什么会出现错误 JwtStrategy requires a secret or key?

java - 通过Java加载任意javascript到Mongodb中,稍后调用

javascript - 在胜利图表方面需要一些帮助

javascript - 在 backbone.js 中处理重新渲染 View 的最佳方法?

javascript - 从事件更新 react 变量

MongoDB 从文档中提取子数组