w3resource

MongoDB Aggregation Stages Operator - $redact

Description

The $redact operator can change and gives a new form of each document in the stream by restricting the content for each document based on information stored in the documents themselves.

Syntax:

{ $redact: <expression> }

The expression can be the system variables $$DESCEND, $$PRUNE, or $$KEEP. Here is description below

System Variables Description
$$DESCEND $redact returns the fields at the current document level, excluding embedded documents. The embedded documents and embedded documents within arrays can also be included by applying the $cond expression to the embedded documents.
$$PRUNE $redact excludes all fields from the current document/embedded document level, without further inspection of any of the excluded fields. The excluded field contains embedded documents which may have different access levels can also be applied without further any inspection.
$$KEEP $redact keeps intact all fields in the current document/embedded document level, without further inspection of the fields at this level. The included field contains embedded documents which may have different access levels can also be applied without further any inspection.

Sample collection testdate

A collection testdate contains documents of the following form where the tags field lists the different access values for that document/embedded document level; i.e. a value of [ "MM", "KKLT" ] specifies either "MM" or "KKLT" can access the data.

{
        "_id" : ObjectId("55432b302a8be1f2a41b9464"),
        "title" : "Department Report of 456",
        "tags" : [
                "MM",
                "KKLT"
        ],
        "year" : 2013,
        "subsections" : [
                {
                        "subtitle" : "Overview : Section 1",
                        "tags" : [
                                "PJ",
                                "MM"
                        ],
                        "content" : "Section 1: This is the content of section 1."
                },
                {
                        "subtitle" : "Section 2: Placement",
                        "tags" : [
                                "KKLT"
                        ],
                        "content" : "Section 2: This is the content of section 2."
                },
                {
                        "subtitle" : "Section 3: Projection",
                        "tags" : [
                                "FS"
                        ],
                        "content" : {
                                "text" : "Section 3: This is the content of section3.",
                                "tags" : [
                                        "LTS"
                                ]
                        }
                }
        ]
}

Example: $redact

The following commands show that, a user has access to view information with either the tag "KKLT" or "MM" to run a query on all documents with the year 2013 for this user.

var userAccess = [ "MM", "KKLT" ];
db.testdate.aggregate(
   [
     { $match: { year: 2013 } },
     { $redact: {
        $cond: {
           if: { $gt: [ { $size: { $setIntersection: [ "$tags", userAccess ] } }, 0 ] },
           then: "$$DESCEND",
           else: "$$PRUNE"
         }
       }
     }
   ]
);

Output :

{
        "_id" : ObjectId("55432b302a8be1f2a41b9464"),
        "title" : "Department Report of 456",
        "tags" : [
                "MM",
                "KKLT"
        ],
        "year" : 2013,
        "subsections" : [
                {
                        "subtitle" : "Overview : Section 1",
                        "tags" : [
                                "PJ",
                                "MM"
                        ],
                        "content" : "Section 1: This is the content of section 1."
                },
                {
                        "subtitle" : "Section 2: Placement",
                        "tags" : [
                                "KKLT"
                        ],
                        "content" : "Section 2: This is the content of section 2."
                }
        ]
}

Previous: $match
Next: $unwind



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/mongodb/aggregation/mongodb-aggregatrion-redact-operator.php