$$KEEP - 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).

$$KEEP

The $$KEEP system variable is used with the $redact stage in the aggregation pipeline to keep the current document or field unchanged and include it in the output.

Parameters

None

Example (MongoDB Shell)

The following example demonstrates the usage of $$KEEP in an Amazon DocumentDB aggregation pipeline. Documents are only kept if access equals "public", otherwise they are removed.

Create sample documents

db.articles.insertMany([ { title: "Article A", access: "public", content: "Visible content" }, { title: "Article B", access: "private", content: "Hidden content" } ]);

Query example

db.articles.aggregate([ { $redact: { $cond: [ { $eq: ["$access", "public"] }, "$$KEEP", "$$PRUNE" ] } } ]);

Output

[ { "_id" : ObjectId("..."), "title" : "Article A", "access" : "public", "content" : "Visible content" } ]

Code examples

To view a code example for using the $$KEEP command, choose the tab for the language that you want to use:

Node.js
const { MongoClient } = require('mongodb'); async function run() { const client = new MongoClient( 'mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0' ); try { await client.connect(); const db = client.db('test'); const articles = db.collection('articles'); const pipeline = [ { $redact: { $cond: [ { $eq: ["$access", "public"] }, "$$KEEP", "$$PRUNE" ] } } ]; const results = await articles.aggregate(pipeline).toArray(); console.log(results); } finally { await client.close(); } } run().catch(console.error);
Python
from pymongo import MongoClient client = MongoClient( "mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0" ) db = client.test articles = db.articles pipeline = [ { "$redact": { "$cond": [ {"$eq": ["$access", "public"]}, "$$KEEP", "$$PRUNE" ] } } ] results = list(articles.aggregate(pipeline)) print(results) client.close()