$redact阶段可以根据文档本身存储的信息,限制输出整个文档或文档中的内容。
语法
{ $redact: <expression> }
使用
参数可以是任何有效的表达式,只要能被解析为$$DESCEND、$$PRUNE或 $$KEEP系统变量即可。
$$DESCEND
$redact返回当前文档级别的字段,不包括嵌入文档。若要包含嵌入文档和数组中的嵌入文档,需要对嵌入文档应用$cond表达式,以确定这些嵌入文档的访问权限。
$$PRUNE
$redact排除当前/嵌入文档级别的所有字段,无需进一步检查任何排除字段,即使排除的字段包含可能具有不同访问级别的嵌入文档。
$$KEEP
$redact返回或保留当前/嵌入文档级别的所有字段,而无需进一步检查该级别的字段。即使包含的字段包含可能具有不同访问级别的嵌入文档。
举例
评估每个文档级别的访问权限
forecasts集合包含以下形式的文档,其中tags字段列出了该文档/嵌入式文档级别的不同访问值;例如,[ "G"、"STLW" ]表示"G"或"STLW"可以访问数据:
{
_id: 1,
title: "123 Department Report",
tags: [ "G", "STLW" ],
year: 2014,
subsections: [
{
subtitle: "Section 1: Overview",
tags: [ "SI", "G" ],
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" ],
content: {
text: "Section 3: This is the content of section 3.",
tags: [ "HCS" ]
}
}
]
}
用户有权查看"STLW"或"G"的信息,要对该用户2014年的所有文档进行查询,可以在查询时加入$redact阶段:
var userAccess = [ "STLW", "G" ];
db.forecasts.aggregate(
[
{ $match: { year: 2014 } },
{ $redact: {
$cond: {
if: { $gt: [ { $size: { $setIntersection: [ "$tags", userAccess ] } }, 0 ] },
then: "$$DESCEND",
else: "$$PRUNE"
}
}
}
]
);
聚合操作返回下面"编辑后"的文档:
{
"_id" : 1,
"title" : "123 Department Report",
"tags" : [ "G", "STLW" ],
"year" : 2014,
"subsections" : [
{
"subtitle" : "Section 1: Overview",
"tags" : [ "SI", "G" ],
"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."
}
]
}
排除指定级别的所有字段
accounts集合有下列文档:
{
_id: 1,
level: 1,
acct_id: "xyz123",
cc: {
level: 5,
type: "yy",
num: 000000000000,
exp_date: ISODate("2015-11-01T00:00:00.000Z"),
billing_addr: {
level: 5,
addr1: "123 ABC Street",
city: "Some City"
},
shipping_addr: [
{
level: 3,
addr1: "987 XYZ Ave",
city: "Some City"
},
{
level: 3,
addr1: "PO Box 0123",
city: "Some City"
}
]
},
status: "A"
}
在这个示例文档中,level字段决定了查看数据所需的访问级别。
要在状态为A的所有文档上运行查询,并排除级别为5的文档/嵌入式文档中包含的所有字段,可在查询时加入$redact阶段,在then字段中指定系统变量"$$PRUNE":
db.accounts.aggregate(
[
{ $match: { status: "A" } },
{
$redact: {
$cond: {
if: { $eq: [ "$level", 5 ] },
then: "$$PRUNE",
else: "$$DESCEND"
}
}
}
]
);
$redact阶段会评估level字段以确定访问权限,如果level字段等于5,则排除该级别的所有字段,即使排除的字段包含可能具有不同级别值的嵌入文档,如shipping_addr字段。
聚合操作会返回下面的文档:
{
"_id" : 1,
"level" : 1,
"acct_id" : "xyz123",
"status" : "A"
}
结果显示$redact阶段排除了整个cc字段,包括shipping_addr字段,该字段包含的嵌入文档的级别字段值等于3而不是5。

被折叠的 条评论
为什么被折叠?



