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