On this page本页内容
$redact
Restricts the contents of the documents based on information stored in the documents themselves.基于存储在文档本身中的信息限制文档的内容。
The $redact
stage has the following prototype form:$redact
阶段具有以下原型形式:
{ $redact: <expression> }
The argument can be any valid expression as long as it resolves to the 参数可以是任何有效的表达式,只要它解析为$$DESCEND
, $$PRUNE
, or $$KEEP
system variables. $$DESCEND
、$$PRUNE
或$$KEEP
系统变量即可。For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式。
$$DESCEND | $redact $cond expression to the embedded documents to determine access for these embedded documents.$cond 表达式应用于嵌入文档,以确定对这些嵌入文档的访问。
|
$$PRUNE
| $redact |
$$KEEP
| $redact |
The examples in this section use the 本节中的示例使用db.collection.aggregate()
helper.db.collection.aggregate()
助手。
A 预测集合包含以下格式的文档,其中forecasts
collection contains documents of the following form where the tags
field lists the different access values for that document/embedded document level; i.e. a value of [ "G", "STLW" ]
specifies either "G"
or "STLW"
can access the data: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 section3.", tags: [ "HCS" ] } } ] }
A user has access to view information with either the tag 用户可以使用标签"STLW"
or "G"
. "STLW"
"G"
查看信息。To run a query on all documents with year 要为该用户运行2014
for this user, include a $redact
stage as in the following: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" } } } ] );
The aggregation operation returns the following "redacted" document:聚合操作返回以下“修订”文档:
{ "_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." } ] }
A collection 集合accounts
contains the following document: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" }
In this example document, the 在本示例文档中,level
field determines the access level required to view the data.level
字段确定查看数据所需的访问级别。
To run a query on all documents with status 要对状态为A
and exclude allfields contained in a document/embedded document at level 5
, include a $redact
stage that specifies the system variable "$$PRUNE"
in the then
field:A
的所有文档运行查询,并排除级别5
的文档/嵌入文档中包含的所有字段,请包括一个$redact
阶段,该阶段在then
字段中指定系统变量"$$PRUNE"
:
db.accounts.aggregate( [ { $match: { status: "A" } }, { $redact: { $cond: { if: { $eq: [ "$level", 5 ] }, then: "$$PRUNE", else: "$$DESCEND" } } } ] );
The $redact
stage evaluates the level
field to determine access. $redact
阶段评估level
字段以确定访问权限。If the 如果level
field equals 5
, then exclude all fields at that level, even if the excluded field contains embedded documents that may have different level
values, such as the shipping_addr
field.level
字段等于5
,则排除该级别的所有字段,即使排除的字段包含可能具有不同level
值的嵌入文档,例如shipping_addr
字段。
The aggregation operation returns the following "redacted" document:聚合操作返回以下“修订”文档:
{ "_id" : 1, "level" : 1, "acct_id" : "xyz123", "status" : "A" }
The result set shows that the 结果集显示,$redact
stage excluded the field cc
as a whole, including the shipping_addr
field which contained embedded documents that had level
field values equal to 3
and not 5
.$redact
阶段整体上排除了字段cc
,包括shipping_addr
字段,该字段包含级别字段值等于3
而不是5
的嵌入文档。
Implement Field Level Redaction for steps to set up multiple combinations of access for the same data.有关为相同数据设置多个访问组合的步骤,请参阅实现字段级编校。