$redact

$redact Fáze v kanálu agregace se používá k dynamickému filtrování polí dokumentů v kolekci na základě přístupových práv nebo jiných podmínek. Zpracovává každý dokument v zpracovatelském řetězci a odebírá nebo uchovává pole na základě zadané logiky.

Syntaxe

{
  $redact: <expression>
}

Parametry

Parameter Description
<expression> Platný výraz MongoDB, který se vyhodnotí jako jeden z následujících výrazů: $$DESCEND, $$PRUNEnebo $$KEEP. Tyto proměnné určují, jestli se mají zachovat, odebrat nebo procházet hlouběji do dokumentu.

Examples

Podívejte se na tento ukázkový dokument z kolekce stores.

{
    "_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
    "name": "First Up Consultants | Beverage Shop - Satterfieldmouth",
    "location": {
        "lat": -89.2384,
        "lon": -46.4012
    },
    "staff": {
        "totalStaff": {
            "fullTime": 8,
            "partTime": 20
        }
    },
    "sales": {
        "totalSales": 75670,
        "salesByCategory": [
            {
                "categoryName": "Wine Accessories",
                "totalSales": 34440
            },
            {
                "categoryName": "Bitters",
                "totalSales": 39496
            },
            {
                "categoryName": "Rum",
                "totalSales": 1734
            }
        ]
    },
    "promotionEvents": [
        {
            "eventName": "Unbeatable Bargain Bash",
            "promotionalDates": {
                "startDate": {
                    "Year": 2024,
                    "Month": 6,
                    "Day": 23
                },
                "endDate": {
                    "Year": 2024,
                    "Month": 7,
                    "Day": 2
                }
            },
            "discounts": [
                {
                    "categoryName": "Whiskey",
                    "discountPercentage": 7
                },
                {
                    "categoryName": "Bitters",
                    "discountPercentage": 15
                },
                {
                    "categoryName": "Brandy",
                    "discountPercentage": 8
                },
                {
                    "categoryName": "Sports Drinks",
                    "discountPercentage": 22
                },
                {
                    "categoryName": "Vodka",
                    "discountPercentage": 19
                }
            ]
        },
        {
            "eventName": "Steal of a Deal Days",
            "promotionalDates": {
                "startDate": {
                    "Year": 2024,
                    "Month": 9,
                    "Day": 21
                },
                "endDate": {
                    "Year": 2024,
                    "Month": 9,
                    "Day": 29
                }
            },
            "discounts": [
                {
                    "categoryName": "Organic Wine",
                    "discountPercentage": 19
                },
                {
                    "categoryName": "White Wine",
                    "discountPercentage": 20
                },
                {
                    "categoryName": "Sparkling Wine",
                    "discountPercentage": 19
                },
                {
                    "categoryName": "Whiskey",
                    "discountPercentage": 17
                },
                {
                    "categoryName": "Vodka",
                    "discountPercentage": 23
                }
            ]
        }
    ]
}

Příklad 1: Redakce citlivých informací

Tento dotaz vyfiltruje promotionEvents pole pro dokumenty, ve kterých discountPercentage povýšení přesahuje 15%.

db.stores.aggregate([
  {
    $redact: {
      $cond: {
        if: {
          $gt: ["$promotionEvents.discounts.discountPercentage", 15]
        },
        then: "$$PRUNE",
        else: "$$DESCEND"
      }
    }
  }
])

První dva výsledky vrácené tímto dotazem jsou:

[
    {
        "_id": "new-store-001",
        "name": "Adatum Corporation - Downtown Branch",
        "sales": {
            "totalSales": 5000
        },
        "createdDate": "2025-06-11T11:11:32.262Z",
        "status": "new",
        "staff": {
            "totalStaff": {
                "fullTime": 0,
                "partTime": 0
            }
        },
        "version": 1,
        "storeOpeningDate": "2025-06-11T11:11:32.262Z",
        "storeFeatures": 213
    },
    {
        "_id": "gaming-store-mall-001",
        "name": "Trey Research | Gaming Paradise - Mall Location",
        "location": {
            "lat": 35.6762,
            "lon": 139.6503
        },
        "createdDate": "2025-06-11T11:13:27.180Z",
        "status": "active",
        "staff": {
            "totalStaff": {
                "fullTime": 8,
                "partTime": 12
            },
            "manager": "Alex Johnson",
            "departments": [
                "gaming",
                "accessories",
                "repairs"
            ]
        },
        "sales": {
            "totalSales": 0,
            "salesByCategory": []
        },
        "operatingHours": {
            "weekdays": "10:00-22:00",
            "weekends": "09:00-23:00"
        },
        "metadata": {
            "version": 1,
            "source": "store-management-system"
        },
        "storeOpeningDate": "2025-06-11T11:11:32.262Z",
        "storeFeatures": 189
    }
]

Příklad 2: Omezení přístupu na základě značek

Tento dotaz odebere všechny dokumenty, které obsahují značku #MembershipDeals.

db.stores.aggregate([
  {
    $redact: {
      $cond: {
        if: {
          $in: ["#MembershipDeals", "$tag"]
        },
        then: "$$PRUNE",
        else: "$$DESCEND"
      }
    }
  }
])