从加密中排除嵌套字段 Mongoose 加密 [英] Exclude nested field from encryption Mongoose encryption

查看:54
本文介绍了从加密中排除嵌套字段 Mongoose 加密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我使用了一个名为 mongoose-encryption 的 npm 包,除了一件重要的事情.我有一个名为报告的数组,其中包含很多对象.每个对象都有一个名为 report_id 的唯一字段,事情是我需要根据该 ID 执行删除操作,但是如果我对其进行加密,猫鼬显然找不到它.为此,我排除了文档所说的某些字段,但显然我无法排除嵌套字段,我试过这个:

So i am using an npm package called mongoose-encryption all is great except one important thing. I have an array called reports and it has alot of objects in it. Each object has one unique field called report_id the thing is i need to perform a delete operation based on that ID but if i encrypt it mongoose apparently cant find it. For that i excluded some fields like the docs said but i cant apparently exclude a nested field i tried this:

usersSchema.plugin(encrypt,{secret:sigKey,excludeFromEncryption: ['username','reports.report_id']});

所以用户名被排除在加密之外,而不是 reports.report_id有什么想法吗?

So the username is excluded from encryption but not the reports.report_id any ideas?

推荐答案

[文档](https://www.npmjs.com/package/mongoose-encryption] 说:

为了加密,相关字段从文档中移除,转换为 JSON,以 Buffer 格式加密,并在前面加上 IV 和插件版本,然后插入文档的 _ct 字段.Mongoose 在发送到 mongo 时将 _ct 字段转换为 Binary.

To encrypt, the relevant fields are removed from the document, converted to JSON, enciphered in Buffer format with the IV and plugin version prepended, and inserted into the _ct field of the document. Mongoose converts the _ct field to Binary when sending to mongo.

如果 reports 字段是通过 mongoose-encryption 加密的,那么它根本不会出现在存储在 MongoDB 中的文档中.

If the reports field is encrypted by mongoose-encryption, it will not be present at all in the document stored in MongoDB.

如果您需要在加密 reports 字段的同时在查询中访问 reports.report_id 字段的值,则需要将它们复制到未加密的数组.

If you need the value of the reports.report_id field to be accessible in a query while encrypting the reports field, you'll need to copy them out to an array that is not being encrypted.

这篇关于从加密中排除嵌套字段 Mongoose 加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆