$redact(Aggregation) in MongoDB with example


Using $redact we can restrict the content of the document on the basis of information stored in documents themselves.
For example, a collection named Inventory which has the following structure.

Run the following script.


use Test



db.Inventory.insert(
{
"Title":"MongoDB Tutorial by codefari.com",
"ISBN":"1990987666765",
"AccessCountry":["India","China"],
})
db.Inventory.insert(
{
"Title":"SQL Server tutorial by codefari.com",
"ISBN":"9909876765476",
"AccessCountry":["USA"],
})
db.Inventory.insert(
{
"Title":"C# tutorial by codefari.com",
"ISBN":"8898877676544",
"AccessCountry":["UK"],
})
db.Inventory.find()


Output


/* 0 */
{
    "_id" : ObjectId("5655b294583c040b3341b96e"),
    "Title" : "MongoDB Tutorial by codefari.com",
    "ISBN" : "1990987666765",
    "AccessCountry" : [
        "India",
        "China"
    ]
}

/* 1 */
{
    "_id" : ObjectId("5655b294583c040b3341b96f"),
    "Title" : "SQL Server tutorial by codefari.com",
    "ISBN" : "9909876765476",
    "AccessCountry" : [
        "USA"
    ]
}

/* 2 */
{
    "_id" : ObjectId("5655b294583c040b3341b970"),
    "Title" : "C# tutorial by codefari.com",
    "ISBN" : "8898877676544",
    "AccessCountry" : [
        "UK"
    ]
}





See content "AccessCountry" we want to filter content on the behalf of the country means a user belong to country USA can't see the other content except "AccessCountry", "USA" at this level we can use $redact.
See below query.


var userAccess=["India","USA"];
db.Inventory.aggregate(
[{
    $redact:{
         $cond: {
           if: { $gt: [ { $size: { $setIntersection: [ "$AccessCountry", userAccess ] } }, 0 ] },
           then: "$$DESCEND",
           else: "$$PRUNE"
         }
        }
}])


Output

                                                                                                                   
/* 0 */
{
    "result" : [
        {
            "_id" : ObjectId("5655b294583c040b3341b96e"),
            "Title" : "MongoDB Tutorial by codefari.com",
            "ISBN" : "1990987666765",
            "AccessCountry" : [
                "India",
                "China"
            ]
        },
        {
            "_id" : ObjectId("5655b294583c040b3341b96f"),
            "Title" : "SQL Server tutorial by codefari.com",
            "ISBN" : "9909876765476",
            "AccessCountry" : [
                "USA"
            ]
        }
    ],
    "ok" : 1
}



Related Posts

What is the Use of isNaN Function in JavaScript? A Comprehensive Explanation for Effective Input Validation

In the world of JavaScript, input validation is a critical aspect of ensuring that user-provided data is processed correctly. One indispensa...