$$PRUNE
The $$PRUNE system variable is used with the $redact stage in the aggregation pipeline to exclude documents or embedded document levels from the results. When a condition evaluates to $$PRUNE, the current document or subdocument is removed from the output. It is typically used with $$DESCEND (to keep and traverse the document) or $$KEEP (to keep the document at all levels).
Parameters
None. The $$PRUNE system variable is used without any parameters and must be used with $redact.
Example (MongoDB Shell)
The following example demonstrates how to use $$PRUNE with $redact to exclude users over 30 years old from the results.
Create sample documents
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" } } ])
Query example
db.users.aggregate([ { $redact: { $cond: { if: { $gt: ["$age", 30] }, then: "$$PRUNE", else: "$$DESCEND" } } } ])
Output
[
{
"_id": 2,
"name": "Saanvi Sarkar",
"age": 28,
"address": {
"street": "456 Oak Rd",
"city": "Someplace",
"state": "NY"
}
}
]
Code examples
To view a code example for using the $$PRUNE command, choose the tab for the language that you want to use: