$redact
聚合管道中的阶段用于动态根据访问权限或其他条件筛选文档集合中的字段。 它处理管道中的每个文档,并根据指定的逻辑删除或保留字段。
语法
{
$redact: <expression>
}
参数
参数 | DESCRIPTION |
---|---|
<expression> |
一个计算结果为下列值之一的有效 MongoDB 表达式: $$DESCEND , $$PRUNE 或 $$KEEP 。 这些变量确定是保留、删除还是遍历文档。 |
例子
请考虑商店集合中的这个示例文档。
{
"_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
}
]
}
]
}
示例 1:修订敏感信息
此查询筛选 promotionEvents
促销中超过 discountPercentage
15 个%的文档的字段。
db.stores.aggregate([
{
$redact: {
$cond: {
if: {
$gt: ["$promotionEvents.discounts.discountPercentage", 15]
},
then: "$$PRUNE",
else: "$$DESCEND"
}
}
}
])
此查询返回的前两个结果为:
[
{
"_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
}
]
示例 2:基于标记限制访问
此查询删除包含标记 #MembershipDeals
的所有文档。
db.stores.aggregate([
{
$redact: {
$cond: {
if: {
$in: ["#MembershipDeals", "$tag"]
},
then: "$$PRUNE",
else: "$$DESCEND"
}
}
}
])
相关内容
- 查看有关 从 MongoDB 迁移到适用于 MongoDB 的 Azure Cosmos DB (vCore) 的选项。
- 详细了解 与 MongoDB 的功能兼容性。