Aggregate.prototype.redact()

Parameters
  • expression «Object» redact options or conditional expression

  • [thenExpr] «String|Object» true case for the condition

  • [elseExpr] «String|Object» false case for the condition

Returns:
  • «Aggregate» this

Appends a new $redact operator to this aggregate pipeline.

If 3 arguments are supplied, Mongoose will wrap them with if-then-else of $cond operator respectively If thenExpr or elseExpr is string, make sure it starts with , like DESCEND, PRUNE or KEEP.

Example:

  1. Model.aggregate(...)
  2. .redact({
  3. $cond: {
  4. if: { $eq: [ '$level', 5 ] },
  5. then: '$$PRUNE',
  6. else: '$$DESCEND'
  7. }
  8. })
  9. .exec();
  10. // $redact often comes with $cond operator, you can also use the following syntax provided by mongoose
  11. Model.aggregate(...)
  12. .redact({ $eq: [ '$level', 5 ] }, '$$PRUNE', '$$DESCEND')
  13. .exec();