$redact (aggregation)

在本页面

Definition

根据文档本身中存储的信息限制文档的内容。

$redact阶段具有以下原型形式:

{ $redact: <expression> }

该参数可以是任何有效的expression,只要它可以解析为$$DESCEND$$PRUNE$$KEEP系统变量。有关表达式的更多信息,请参见Expressions

System Variable Description
$$DESCEND $redact返回当前文档级别的字段,不包括嵌入式文档。要将嵌入式文档和嵌入式文档包含在数组中,请对嵌入式文档应用$cond表达式以确定对这些嵌入式文档的访问。
$$PRUNE $redact排除此当前文档/嵌入文档级别的所有字段, 无需 进一步检查任何排除的字段。即使排除字段包含具有不同访问级别的嵌入式文档,这也适用。
$$KEEP $redact返回或保留所有字段为当前文档/嵌入文档级别,而无需进一步检查此级别的字段。即使所包含的字段包含具有不同访问级别的嵌入式文档,这也适用。

Examples

本节中的示例使用mongo shell 2.6 版本中提供的db.collection.aggregate()帮助器。

评估每个文档级别的访问权限

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 section3.",
        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的文档/嵌入文档中包含的* all *字段,请在then字段中包含一个$redact阶段,该阶段指定系统变量"$$PRUNE"

db.accounts.aggregate(
  [
    { $match: { status: "A" } },
    {
      $redact: {
        $cond: {
          if: { $eq: [ "$level", 5 ] },
          then: "$$PRUNE",
          else: "$$DESCEND"
        }
      }
    }
  ]
);

$redact阶段评估level字段以确定访问权限。如果level字段等于5,则排除该级别的所有字段,即使排除的字段包含可能具有不同的level值的嵌入式文档,例如shipping_addr字段。

聚合操作返回以下“已编辑”文档:

{
  "_id" : 1,
  "level" : 1,
  "acct_id" : "xyz123",
  "status" : "A"
}

结果集显示$redact阶段从整体上排除了字段cc,包括shipping_addr字段,其中包含level字段值等于3而不是5的嵌入式文档。

See also

实施字段级别修订提供针对同一数据设置访问权限的多个组合的步骤。

首页