<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mansour mobarakian</title>
    <description>The latest articles on DEV Community by Mansour mobarakian (@mansourmobarakian).</description>
    <link>https://dev.to/mansourmobarakian</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1324343%2F1be01080-bba8-4b80-8baa-cb03cad2b8d5.jpeg</url>
      <title>DEV Community: Mansour mobarakian</title>
      <link>https://dev.to/mansourmobarakian</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mansourmobarakian"/>
    <language>en</language>
    <item>
      <title>Using $redact in Mongoose Aggregation(Simple example)</title>
      <dc:creator>Mansour mobarakian</dc:creator>
      <pubDate>Mon, 11 Mar 2024 08:13:20 +0000</pubDate>
      <link>https://dev.to/mansourmobarakian/using-redact-in-mongoose-aggregationsimple-example-2klh</link>
      <guid>https://dev.to/mansourmobarakian/using-redact-in-mongoose-aggregationsimple-example-2klh</guid>
      <description>&lt;p&gt;$REDACT EXCLUDES ALL FIELDS AT THIS CURRENT DOCUMENT/EMBEDDED DOCUMENT LEVEL, WITHOUT FURTHER INSPECTION OF ANY OF THE EXCLUDED FIELDS.&lt;br&gt;
RESOURCE: &lt;a href="https://www.mongodb.com/docs/manual/reference/operator/aggregation/redact/"&gt;$redact (aggregation) — MongoDB Manual&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The $redact stage in MongoDB aggregation allows you to restrict entire documents or content within documents from being outputted based on information stored in the documents themselves. It’s a powerful tool for controlling the visibility of data based on specific conditions.&lt;/p&gt;
&lt;h2&gt;
  
  
  Scenario
&lt;/h2&gt;

&lt;p&gt;Suppose we have a collection of users, and we want to retrieve specific information about users named “sohrab.” We’ll demonstrate how to use $redact to achieve this.&lt;/p&gt;
&lt;h2&gt;
  
  
  User Data
&lt;/h2&gt;

&lt;p&gt;Let’s consider the following sample user data:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Users = [
  {
    "id": 1,
    "name": "john",
    "age": 30,
    "phone": "+12324252525"
  },
  {
    "id": 2,
    "name": "tom",
    "age": 25,
    "phone": "+12323525225"
  },
  {
    "id": 3,
    "name": "sohrab",
    "age": 23,
    "phone": "+12232325225"
  },
  {
    "id": 4,
    "name": "sohrab",
    "age": 25,
    "phone": "+12232325225"
  }
];

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 1: Using $match
&lt;/h2&gt;

&lt;p&gt;We start by using the $match stage to filter users with the name “sohrab”:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Users.aggregate([
  {
    $match: {
      name: "sohrab"
    }
  }
]);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The results will include the following documents:&lt;/p&gt;

&lt;p&gt;User with id = 3:&lt;br&gt;
Age: 23&lt;br&gt;
Name: “sohrab”&lt;br&gt;
Phone: “+12232325225”&lt;br&gt;
User with id = 4:&lt;br&gt;
Age: 25&lt;br&gt;
Name: “sohrab”&lt;br&gt;
Phone: “+12232325225”&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 2: Using $redact and $cond
&lt;/h2&gt;

&lt;p&gt;Now let’s further limit the output. We want to keep only users with an age of 25. We’ll use $redact and $cond for this purpose:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Users.aggregate([
  {
    $match: {
      name: "sohrab"
    }
  },
  {
    $redact: {
      $cond: {
        if: {
          $eq: ["$age", 25]
        },
        then: "$$DESCEND", // Keep fields at this level
        else: "$$PRUNE" // Exclude fields at this level
      }
    }
  },
  {
    $project: {
      _id: false,
      age: "$age",
      phone: "$phone"
    }
  }
]);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The resulting output will be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[
  {
    "age": 25,
    "phone": "+12232325225"
  }
]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this final output, we’ve kept only the age and phone fields for users named “sohrab” with an age of 25.&lt;/p&gt;

&lt;p&gt;Remember that $redact allows you to control the visibility of fields based on conditions, making it a valuable tool for fine-tuning your aggregation results.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>mongodb</category>
      <category>mongoose</category>
      <category>node</category>
    </item>
  </channel>
</rss>
