本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
$$PRUNE
$$PRUNE系统变量与聚合管道中的$redact阶段一起使用,从结果中排除文档或嵌入式文档级别。当条件计算为时$$PRUNE,当前文档或子文档将从输出中删除。它通常与$$DESCEND(用于保留和遍历文档)或$$KEEP(将文档保持在所有级别)一起使用。
参数
无。$$PRUNE系统变量不带任何参数,必须与一起使用$redact。
示例(MongoDB 外壳)
以下示例演示如何$$PRUNE使用和将 30 岁$redact以上的用户排除在结果之外。
创建示例文档
db.users.insert([ { _id:1, name: "Carlos Salazar", age: 35, address: { street: "123 Main St", city: "Anytown", state: "CA" } }, { _id:2, name: "Saanvi Sarkar", age: 28, address: { street: "456 Oak Rd", city: "Someplace", state: "NY" } }, { _id:3, name: "Li Juan", age: 42, address: { street: "789 Pine Ave", city: "Springfield", state: "TX" } } ])
查询示例
db.users.aggregate([ { $redact: { $cond: { if: { $gt: ["$age", 30] }, then: "$$PRUNE", else: "$$DESCEND" } } } ])
输出
[
{
"_id": 2,
"name": "Saanvi Sarkar",
"age": 28,
"address": {
"street": "456 Oak Rd",
"city": "Someplace",
"state": "NY"
}
}
]
代码示例
要查看使用该$$PRUNE命令的代码示例,请选择要使用的语言的选项卡: