Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java MongoDB只读视图字段屏蔽_Java_Mongodb - Fatal编程技术网

Java MongoDB只读视图字段屏蔽

Java MongoDB只读视图字段屏蔽,java,mongodb,Java,Mongodb,我是MongoDB的新手,正在尝试寻找一种可以屏蔽字段以保护隐私的方法。在MongoDB 3.4中尝试了只读视图 我有以下收藏 db.employees.find().pretty() { "_id" : ObjectId("59802d45d2f4250001ead835"), "name" : "Nick", "mobile" : "927 113 4566" }, { "_id" : ObjectId("59802d45d2f

我是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白皮书中屏蔽

任何建议。

根据您的使用情况,有几种方法可以“屏蔽”移动字段值。视图是基于构建的,您可以根据需要使用它

例如,您可以简单地使用隐藏
mobile
字段。 i、 e.鉴于这些文件:

{"_id": ObjectId(".."), "name": "Nick", "mobile": "927 113 4566"}
{"_id": ObjectId(".."), "name": "Sam", "mobile": "817 133 4566"}
{"_id": ObjectId(".."), "name": "Nick", "mobile": "927 113 4566", "show": true}
{"_id": ObjectId(".."), "name": "Sam", "mobile": "817 133 4566", "show": false}
您可以在视图中排除
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"}
{"_id": ObjectId(".."), "name": "Sam", "mobile": "817 133 4566"}
{"_id": ObjectId(".."), "name": "Nick", "mobile": "927 113 4566", "show": true}
{"_id": ObjectId(".."), "name": "Sam", "mobile": "817 133 4566", "show": false}
您可以根据字段
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"}