繁体   English   中英

MongoDB只读视图字段Masking

[英]MongoDB Read-only view field Masking

我是MongoDB的新手,正在尝试寻找一种可以掩盖字段以保护隐私的方法。 在MongoDB 3.4中尝试了只读视图

我有以下收藏,

db.employees.find().pretty()
{
        "_id" : ObjectId("59802d45d2f4250001ead835"),
        "name" : "Nick",
        "mobile" : "927 113 4566"
},
{
        "_id" : ObjectId("59802d45d2f4250001ead835"),
        "name" : "Sam",
        "mobile" : "817 133 4566"
}

创建一个只读视图:

db.createView("employeeView", "employees", [ {$project : { "mobile": 1} } ] )

db.employeeView.find()

{ "_id" : ObjectId("59802d45d2f4250001ead835"), "mobile" : "927 113 4566"}
{ "_id" : ObjectId("59802d45d2f4250001ead835"), "mobile" : "817 133 4566"}

但我找不到在employeeView中屏蔽“移动”字段的解决方案,但提到我们可以在MongoDB GDPR白皮书中屏蔽

https://www.mongodb.com/collat​​eral/gdpr-impact-to-your-data-management-landscape

任何建议做到这一点。

有几种方法可以“屏蔽” mobile字段值,具体取决于您的用例。 视图是基于MongoDB聚合管道构建的,您可以根据需要使用它。

例如,您可以简单地使用$ project隐藏mobile字段。 即给出这些文件:

{"_id": ObjectId(".."), "name": "Nick", "mobile": "927 113 4566"}
{"_id": ObjectId(".."), "name": "Sam", "mobile": "817 133 4566"}

您可以在视图中排除mobile字段:

> db.createView("empView", "employees", [{"$project":{name:1}}])
> db.empView.find() 
{"_id": ObjectId(".."), "name": "Nick"}
{"_id": ObjectId(".."),"name": "Sam"}

或者,也许您在文档中有一个额外的字段来指示信息是否公开,即给定的文档:

{"_id": ObjectId(".."), "name": "Nick", "mobile": "927 113 4566", "show": true}
{"_id": ObjectId(".."), "name": "Sam", "mobile": "817 133 4566", "show": false}

您可以利用$ cond表达式根据field show进行遮罩。 例如:

> db.createView("empView", "employees", 
             [{$project:{ 
               name:1, 
               mobile:{$cond:{
                       if:{$eq:["$show", false]}, 
                       then: "xxx xxx xxxx", 
                       else: "$mobile"}}
}}])

> db.empView.find() 

{"_id": ObjectId(".."), "name": "Nick", "mobile": "927 113 4566"}
{"_id": ObjectId(".."), "name": "Sam", "mobile": "xxx xxx xxxx"}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM