文档菜单
文档首页
/
MongoDB 手册
/

实施字段级编辑

本页面内容

  • 过程

$redact 管道操作符根据存储在文档中的信息限制文档内容。

Diagram of security architecture with middleware and redaction.

要存储访问标准数据,向文档和嵌套文档添加字段。为了允许相同数据的多重访问级别组合,请考虑将访问字段设置为多维数组。每个数组元素包含一个所需的集合,允许具有该集合的用户访问数据。

然后,在 db.collection.aggregate() 操作中包含 $redact 阶段,根据查看数据所需的访问权限限制结果集的内容。

有关 $redact 管道操作符的更多信息,包括其语法、相关系统变量以及附加示例,请参阅 $redact

例如,一个 forecasts 集合包含以下形式的文档,其中 tags 字段确定查看数据所需的访问级别

{
_id: 1,
title: "123 Department Report",
tags: [ [ "G" ], [ "FDW" ] ],
year: 2014,
subsections: [
{
subtitle: "Section 1: Overview",
tags: [ [ "SI", "G" ], [ "FDW" ] ],
content: "Section 1: This is the content of section 1."
},
{
subtitle: "Section 2: Analysis",
tags: [ [ "STLW" ] ],
content: "Section 2: This is the content of section 2."
},
{
subtitle: "Section 3: Budgeting",
tags: [ [ "TK" ], [ "FDW", "TGE" ] ],
content: {
text: "Section 3: This is the content of section3.",
tags: [ [ "HCS"], [ "FDW", "TGE", "BX" ] ]
}
}
]
}

对于每个文档,tags 字段包含查看数据所需的各个访问分组。例如,值 [ [ "G" ], [ "FDW", "TGE" ] ] 可以指定用户需要访问级别 ["G"] 或两者之一 [ "FDW", "TGE" ] 才能查看数据。

考虑一个只有访问 "FDW""TGE" 标签信息的用户。为此用户执行所有年份为 2014 的文档的查询,包括以下 $redact 阶段

var userAccess = [ "FDW", "TGE" ];
db.forecasts.aggregate(
[
{ $match: { year: 2014 } },
{ $redact:
{
$cond: {
if: { $anyElementTrue:
{
$map: {
input: "$tags" ,
as: "fieldTag",
in: { $setIsSubset: [ "$$fieldTag", userAccess ] }
}
}
},
then: "$$DESCEND",
else: "$$PRUNE"
}
}
}
]
)

聚合操作返回以下 "已编辑" 文档供用户查看

{ "_id" : 1,
"title" : "123 Department Report",
"tags" : [ [ "G" ], [ "FDW" ] ],
"year" : 2014,
"subsections" :
[
{
"subtitle" : "Section 1: Overview",
"tags" : [ [ "SI", "G" ], [ "FDW" ] ],
"content" : "Section 1: This is the content of section 1."
},
{
"subtitle" : "Section 3: Budgeting",
"tags" : [ [ "TK" ], [ "FDW", "TGE" ] ]
}
]
}

提示

另请参阅

返回

为 FIPS 配置

本页面内容