$$PRUNE - Amazon DocumentDB
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

$$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:

Node.js
const { MongoClient } = require('mongodb'); const client = new MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false'); async function main() { await client.connect(); const db = client.db('test'); const users = db.collection('users'); const result = await users.aggregate([ { $redact: { $cond: { if: { $gt: ["$age", 30] }, then: "$$PRUNE", else: "$$DESCEND" } } } ]).toArray(); console.log(result); await client.close(); } main();
Python
from pymongo import MongoClient client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false') db = client['test'] users = db['users'] result = list(users.aggregate([ { '$redact': { '$cond': { 'if': { '$gt': ['$age', 30] }, 'then': '$$PRUNE', 'else': '$$DESCEND' } } } ])) print(result) client.close()