<?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: Ganesh Yadav</title>
    <description>The latest articles on DEV Community by Ganesh Yadav (@ganeshyadav3142).</description>
    <link>https://dev.to/ganeshyadav3142</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%2F1158258%2F1477456a-168a-4c62-a5a6-cd4066c8ab2d.jpg</url>
      <title>DEV Community: Ganesh Yadav</title>
      <link>https://dev.to/ganeshyadav3142</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ganeshyadav3142"/>
    <language>en</language>
    <item>
      <title>Launch Your SaaS Startup with Ease | Shipzo.pro Next.js Boilerplate Coming Soon</title>
      <dc:creator>Ganesh Yadav</dc:creator>
      <pubDate>Fri, 29 Nov 2024 17:53:08 +0000</pubDate>
      <link>https://dev.to/ganeshyadav3142/launch-your-saas-startup-with-ease-shipzopro-nextjs-boilerplate-coming-soon-5a1c</link>
      <guid>https://dev.to/ganeshyadav3142/launch-your-saas-startup-with-ease-shipzopro-nextjs-boilerplate-coming-soon-5a1c</guid>
      <description>&lt;p&gt;Attention developers and SaaS enthusiasts! Imagine having the tools to kickstart your SaaS business in record time. We’re working on something groundbreaking—a Next.js boilerplate designed to redefine how you build, scale, and launch SaaS products.&lt;br&gt;
Though the full release isn’t here yet, &lt;strong&gt;you can join us early and be the first to know!&lt;/strong&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Why Sign Up Now?
&lt;/h2&gt;

&lt;p&gt;We’re building something that will:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Simplify SaaS development:&lt;/strong&gt; Eliminate the hurdles of starting from scratch.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Accelerate your launch:&lt;/strong&gt; Spend less time setting up and more time growing your business.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Empower your creativity:&lt;/strong&gt; Lay a robust foundation and focus on innovating.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Empower Your Ideas:&lt;/strong&gt; Build your SaaS product effortlessly and focus on innovation.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Transform Your Startup Journey:&lt;/strong&gt; Skip the complexities and launch with confidence.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;While we’re keeping details under wraps for now, signing up ensures:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Exclusive Early Updates:&lt;/strong&gt; Be the first to know when we’re live.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Priority Access:&lt;/strong&gt; Join an insider community shaping the future of SaaS development.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Special Launch Offers:&lt;/strong&gt; Get perks only available to early adopters and early signed-up users only.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  Be Part of the Journey—Sign Up Today!
&lt;/h2&gt;

&lt;p&gt;Don’t miss out on this chance to transform the way you build SaaS applications. The journey is just beginning, and we want you on board.&lt;br&gt;
👉&lt;a href="https://www.shipzo.pro/" rel="noopener noreferrer"&gt;Sign up&lt;/a&gt; now to reserve your spot and stay updated!&lt;/p&gt;
&lt;h2&gt;
  
  
  Keep an Eye Out for What’s Next
&lt;/h2&gt;

&lt;p&gt;We’re excited to bring this to life and share the full potential of our boilerplate with you soon. By signing up, you’ll ensure you’re part of the revolution from the start.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Be the first to know. Be the first to build. Be the first to succeed.&lt;/strong&gt;&lt;/p&gt;


&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
      &lt;div class="c-embed__cover"&gt;
        &lt;a href="https://www.shipzo.pro/" class="c-link s:max-w-50 align-middle" rel="noopener noreferrer"&gt;
          &lt;img alt="" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fxkgbptqwypdydhcrebdl.supabase.co%2Fstorage%2Fv1%2Fobject%2Fsign%2Fshipezy%2FHappy%2520man%2520watching%2520launch%2520of%2520rocket.jpg%3Ftoken%3DeyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1cmwiOiJzaGlwZXp5L0hhcHB5IG1hbiB3YXRjaGluZyBsYXVuY2ggb2Ygcm9ja2V0LmpwZyIsImlhdCI6MTczMjQ2ODAyOSwiZXhwIjoxNzY0MDA0MDI5fQ.9FaNiDUvd6UGI8QDtIqrLYmdpOHZhC7qxijjBDxMOos%26t%3D2024-11-24T17%253A07%253A09.890Z" height="800" class="m-0" width="800"&gt;
        &lt;/a&gt;
      &lt;/div&gt;
    &lt;div class="c-embed__body"&gt;
      &lt;h2 class="fs-xl lh-tight"&gt;
        &lt;a href="https://www.shipzo.pro/" rel="noopener noreferrer" class="c-link"&gt;
          Shipzo: Launch Your SaaS Startup in Hours with Best Saas Template
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;p class="truncate-at-3"&gt;
          Our NextJS boilerplate help's you to ship your Saas in just hours which come with build in template and other stuff that help you to make $ online
        &lt;/p&gt;
      &lt;div class="color-secondary fs-s flex items-center"&gt;
          &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.shipzo.pro%2Ffavicon.ico" width="800" height="400"&gt;
        shipzo.pro
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;


</description>
      <category>saas</category>
      <category>webdev</category>
      <category>nextjs</category>
      <category>startup</category>
    </item>
    <item>
      <title>Decoding System Design of Rate Limiter: 1( Your Goto Protector for backend APIs)</title>
      <dc:creator>Ganesh Yadav</dc:creator>
      <pubDate>Sun, 04 Aug 2024 16:30:13 +0000</pubDate>
      <link>https://dev.to/ganeshyadav3142/decoding-system-design-of-rate-limiter-1-your-goto-protector-for-backend-apis-292e</link>
      <guid>https://dev.to/ganeshyadav3142/decoding-system-design-of-rate-limiter-1-your-goto-protector-for-backend-apis-292e</guid>
      <description>&lt;p&gt;Hello readers and welcome back to another article series in which we will decode the system design of a conventional rate limiter and further in the later part we will build our own rate limiter using node.js, so let's get started.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a Rate Limiter and what is the Purpose of using it?
&lt;/h2&gt;

&lt;p&gt;The Rate limiter is a limiting technique used to limit network traffic. A kind of filter it puts a cap on how often someone can repeat an action in a given timeframe. e.g. Posting on social media from your account in a given time frame.&lt;/p&gt;

&lt;p&gt;In HTTP world a rate limiter would simply limit the number of client requests allowed to be sent over a specified period of time. If the API request count exceeds the threshold defined by the rate limiter, all the excess calls are blocked&lt;/p&gt;

&lt;p&gt;The rate limiter helps us to maintain your system performance, by limiting traffic which is directed at an API or on our system, further also rate limiter help us to prevent our system from various malicious activities such as Bot attacks, DDos Attacks and Web scrapping.&lt;/p&gt;

&lt;p&gt;Rate limiting also protects against API overuse, which is not necessarily malicious or due to bot activity, but is important to prevent nonetheless.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where to put the Rate Limiter?
&lt;/h2&gt;

&lt;p&gt;Let us think, about a basic simple client and server application, the rate limiter can be implemented on both sides either by the client or on the server side.&lt;br&gt;
&lt;strong&gt;Client-side Implementation:&lt;/strong&gt; The client-side implementation of the rate limiter is unreliable due to its prone nature of getting forged by malicious actors and uncontrolled behaviour.&lt;br&gt;
&lt;strong&gt;Server-side Implementation :&lt;/strong&gt;Server side implementation is basically added to our backend server as shown below in the diagram.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh4qd3t1bzdd09y388p1z.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh4qd3t1bzdd09y388p1z.jpg" alt="Imagelist1"&gt;&lt;/a&gt;&lt;br&gt;
However, there is another method by which we can implement the rate limiter other than client and server-side implementation. Instead of implementing a limiter at server API, we can implement a rate limiter as a middleware which is also the technology provided by express in node.js. In Express also we can use their in-built rate limiter as a middleware, below is the representation of it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4cnw5pg7lgmtije9sp81.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4cnw5pg7lgmtije9sp81.jpg" alt="ImageList2"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;let us learn how this type of implementation of rate limiter works, suppose your backend APIs support a maximum of 3 requests per second but the client 4 requests per second, in this situation the first 3 requests get passed through the middleware towards the server and the last request will throttle by the rate limiter middleware and will return an HTTP status 429 which means that client has sent to many requests over a period of time. Below is the illustration for the same.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fym86zswd3ocz98isu164.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fym86zswd3ocz98isu164.jpg" alt="Imagelist3"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  API Gateway:
&lt;/h2&gt;

&lt;p&gt;Nowadays, Cloud microservices a gained significant popularity due to their fully managed services which include services like rate limiting, SSL termination, IP whitelisting, authentication and services static content and these services are included in a simple component called an API gateway.&lt;/p&gt;

&lt;p&gt;Although designing a rate limiter can be a tedious task if one does not have enough resources, then you can simply take these fully managed cloud microservices they provide this API gateway which includes an inbuilt rate limiter, the popular example is &lt;strong&gt;Amazon API gateway&lt;/strong&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  Algorithms for Rate Limiter:
&lt;/h2&gt;

&lt;p&gt;There are some great algorithms for building the rate limiter of our choices but each of these different algorithms has its pros and cons, algorithms are listed below&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Token Bucket&lt;/li&gt;
&lt;li&gt;Leaking Bucket&lt;/li&gt;
&lt;li&gt;Fixed window counter&lt;/li&gt;
&lt;li&gt;Sliding window log&lt;/li&gt;
&lt;li&gt;Sliding window counter&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  Token Bucket:
&lt;/h2&gt;

&lt;p&gt;Token Bucket algorithm is a simple algorithm which is used to build a rate limiter it is simple and well-understood and it is used by most organisations such as Amazon and Stripe to throttle their API requests.&lt;/p&gt;

&lt;p&gt;The algorithm approach is simple, each token Bucket has a fixed capacity in which tokens are filled at a preset interval the bucket gets filled with capacity the token will overflow&lt;/p&gt;

&lt;p&gt;when the request arrives we will check for the token if it is available inside the bucket and the request will be allocated this token and passed towards the API server and token count will be decremented from the bucket, but if there is no token available inside the bucket the request will be throttled back with status code 429, below is the basic implementation of the same.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F54h96v3na7fkl0bohb9j.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F54h96v3na7fkl0bohb9j.jpg" alt="Imagelist4"&gt;&lt;/a&gt;&lt;br&gt;
The token bucket algorithm takes two parameters the bucket size and refill rate, the bucket size corresponds to bucket capacity and the refill rate is the rate at which the token gets filled in the bucket per second.&lt;/p&gt;

&lt;p&gt;while working with bucket tokens, we need to consider the number of tokens should we use, this depends on the system requirement that you are trying to build suppose you want to throttle the request based on IP address, then you need to add each bucket for each API.&lt;/p&gt;

&lt;p&gt;But if you want to configure 10000 requests for your system then the global bucket is the best option for the system, below is the high-level implementation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6ilvqw4drtm4hxcmeq47.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6ilvqw4drtm4hxcmeq47.jpg" alt="Imagelist5"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Leaking Bucket:
&lt;/h2&gt;

&lt;p&gt;The leaking bucket algorithm is similar to a token bucket but except for the token, it redirects the request to Queue(FIFO), if the queue is empty the requests are processed if the queue is full the request is throttled back or dropped with status code 429, the successful request is then pulled from the queue and processed at a regular interval.&lt;/p&gt;

&lt;p&gt;The leaking bucket algorithm takes two parameters, the bucket size which is the same as that of the queue and the outflow rate it defines how many requests should be processed at a regular interval.&lt;/p&gt;

&lt;p&gt;Shopify an e-commerce building platform, uses this type of rate limiter on their system, below is the representation of the leaking bucket algorithm.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2vovqhhefwgwf4jv8sns.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2vovqhhefwgwf4jv8sns.jpg" alt="Imagelist6"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Summary:
&lt;/h3&gt;

&lt;p&gt;So in this part of the decoding rate limiter, we get to know insight about the rate limiter that is used in modern industries and how they are implemented in such a way that it can reduce the negative impact and the complication that our API faces in case of DDoS attacks and heavy network traffic, we also learn about how to implement this rate limiter our system, we also learn about the two types of algorithms that are widely used in implementation rate limiter.&lt;/p&gt;

&lt;p&gt;Although covering all the algorithms is out of scope for this single article i.e., I have covered two algorithms for now and in the later part we will cover the remaining algorithm and also implement our own rate limiter using node.js.&lt;/p&gt;

&lt;p&gt;So, stay tuned for the next article.&lt;/p&gt;

&lt;p&gt;I have learnt this on the internet and some of the references, I have mentioned below do checkout the link for in-depth understanding.&lt;br&gt;
&lt;/p&gt;
&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
    &lt;a href="https://www.cloudflare.com/" rel="noopener noreferrer"&gt;
      cloudflare.com
    &lt;/a&gt;
&lt;/div&gt;


</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>systemdesign</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Aggregation Pipeline in MongoDB and the use of $match and $group operator (Part 2)</title>
      <dc:creator>Ganesh Yadav</dc:creator>
      <pubDate>Thu, 25 Jan 2024 17:54:54 +0000</pubDate>
      <link>https://dev.to/ganeshyadav3142/aggregation-pipeline-in-mongodb-and-the-use-of-match-and-group-operator-part-2-18gg</link>
      <guid>https://dev.to/ganeshyadav3142/aggregation-pipeline-in-mongodb-and-the-use-of-match-and-group-operator-part-2-18gg</guid>
      <description>&lt;p&gt;Hello and welcome back readers, sorry for the delay in this second part of the MongoDB Aggregation Pipeline series, where we are going to explore the power of the Aggregation Pipeline provided by MongoDB to make a developer's life easy.&lt;/p&gt;

&lt;p&gt;If you are new to this article, I would like you to check the first part of the series by clicking &lt;a href="https://dev.to/ganeshyadav3142/introduction-to-aggregation-pipeline-in-mongodb-part-1-2cfo"&gt;here&lt;/a&gt;, and for the readers who are reading it on a continuous track let's first revise what we have learned till now.&lt;/p&gt;

&lt;p&gt;Till now we have a clear understanding of what is aggregation in MongoDB and what are the different types of aggregation provided by MongoDB such as&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Map Reduce Function&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Single Purpose Aggregation&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Aggregation Pipeline&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In this article, we will deeply explore the power of the aggregation pipeline provided with the use of &lt;strong&gt;$match&lt;/strong&gt; and &lt;strong&gt;$group&lt;/strong&gt; operators.&lt;/p&gt;

&lt;h2&gt;
  
  
  Aggregation Pipeline using $match operator:
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;$match&lt;/strong&gt; operator filters the documents to pass only the documents that match the specified conditions to the next pipeline stage.&lt;/p&gt;

&lt;p&gt;In General, if you have applied a &lt;strong&gt;$match&lt;/strong&gt; operator to a set of data with mentioned expressions/fields then it will filter out only the documents that will match the field on each document and return the documents.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//Syntax&lt;/span&gt;

&lt;span class="nx"&gt;Syntax&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;$match&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;query&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;


&lt;p&gt;let us understand the &lt;strong&gt;$match&lt;/strong&gt; operator with the basic example where we have the collection of student data&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//Collection Students&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;_id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ObjectId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;512bc95fe835e68f199c8686&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;student&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Dave Smith&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;score&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;80&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;_id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ObjectId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;512bc962e835e68f199c8687&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;student&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Dave Smith&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;score&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;85&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;_id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ObjectId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;55f5a192d4bede9ac365b257&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;student&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Ahn ben&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;score&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;_id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ObjectId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;55f5a192d4bede9ac365b258&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;student&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;li xin&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;score&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;55&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;_id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ObjectId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;55f5a1d3d4bede9ac365b259&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;student&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Ben hue&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;score&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;_id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ObjectId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;55f5a1d3d4bede9ac365b25a&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;student&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;li xin&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;score&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;94&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;_id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ObjectId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;55f5a1d3d4bede9ac365b25b&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;student&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Peter parker&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;score&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;95&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;//Now if we apply the $match operator to the above collection with the student's name &lt;/span&gt;
&lt;span class="c1"&gt;//Dave Smith We get only the documents in which the student is Dave Smith&lt;/span&gt;

&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;students&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;aggregate&lt;/span&gt;&lt;span class="p"&gt;([{&lt;/span&gt;&lt;span class="na"&gt;$match&lt;/span&gt;&lt;span class="p"&gt;:{&lt;/span&gt;&lt;span class="na"&gt;student&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Dave smith&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}}])&lt;/span&gt;
&lt;span class="c1"&gt;//we will get the results as shown below&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;_id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ObjectId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;512bc95fe835e68f199c8686&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;student&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Dave Smith&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;score&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;80&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;_id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ObjectId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;512bc962e835e68f199c8687&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;student&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Dave Smith&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;score&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;85&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;

&lt;h2&gt;
  
  
  Aggregation Pipeline using $group operator:
&lt;/h2&gt;

&lt;p&gt;Now we can move to the next operator which is the &lt;strong&gt;$group&lt;/strong&gt; operator, as we know in the aggregation pipeline there is a series of stages which we can introduce, to extract a certain kind of data as per our requirements.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;$group&lt;/strong&gt; stage separates documents into groups according to a "group key". The output is one document for each unique group key.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;syntax&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;     &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;$group&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// Group key : &lt;/span&gt;
              &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;field1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;accumulator&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;expression&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; 
                  &lt;span class="p"&gt;...&lt;/span&gt; 
                      &lt;span class="p"&gt;}&lt;/span&gt; 
             &lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;


&lt;p&gt;The above definition of group operator generally refers to, when it is applied on a set of documents it will return a set of documents with each document containing the field &lt;strong&gt;_id&lt;/strong&gt; as the first field followed by the second field in which the group is done.&lt;/p&gt;

&lt;p&gt;for example, as shown below the group operator is applied in the student collection.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="c1"&gt;//Our student's collection with the name of the student as field "student"&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;_id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ObjectId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;512bc95fe835e68f199c8686&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;student&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Dave Smith&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;score&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;80&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;_id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ObjectId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;512bc962e835e68f199c8687&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;student&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Dave Smith&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;score&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;85&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;_id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ObjectId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;55f5a192d4bede9ac365b257&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;student&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Ahn ben&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;score&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;_id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ObjectId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;55f5a192d4bede9ac365b258&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;student&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;li xin&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;score&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;55&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;_id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ObjectId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;55f5a1d3d4bede9ac365b259&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;student&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Ben hue&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;score&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;_id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ObjectId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;55f5a1d3d4bede9ac365b25a&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;student&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;li xin&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;score&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;94&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;_id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ObjectId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;55f5a1d3d4bede9ac365b25b&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;student&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Peter parker&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;score&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;95&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;//Now if we group the collections based on the student as _id &lt;/span&gt;
&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;students&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;aggregate&lt;/span&gt;&lt;span class="p"&gt;([{&lt;/span&gt;&lt;span class="na"&gt;$group&lt;/span&gt;&lt;span class="p"&gt;:{&lt;/span&gt;&lt;span class="na"&gt;_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;$student&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}])&lt;/span&gt;

&lt;span class="c1"&gt;//_id is mandatory field for applying $group which always takes the filed in&lt;/span&gt;
&lt;span class="c1"&gt;//which you want your collections to be grouped&lt;/span&gt;
&lt;span class="c1"&gt;//will always return the total distinct names present inside your collections&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;_id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Dave Smith&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;_id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Ahn ben&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;_id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;li xin&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;_id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Peter Parker&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;


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

&lt;/div&gt;


&lt;p&gt;Now from the above two operators, it is clear that &lt;strong&gt;$match&lt;/strong&gt; can be used when you want to filter out the document based on a certain field and &lt;strong&gt;$group&lt;/strong&gt; can used to group the particular collection based on the "Group key".&lt;/p&gt;

&lt;p&gt;Similarly, as we know there can be multiple stages in an aggregation pipeline and we can introduce any number of stages as much as we want, Now we will try to add these two stages in our next example, which utilises both &lt;strong&gt;$match&lt;/strong&gt; and &lt;strong&gt;$group&lt;/strong&gt; operator.&lt;/p&gt;
&lt;h2&gt;
  
  
  Problem Statement for Two-stage Pipeline:
&lt;/h2&gt;

&lt;p&gt;We want to find the name of the Student who has scored greater than or equal to 80.&lt;/p&gt;

&lt;p&gt;From the above problem statement, it is clear that we can use the $match operator to find the student with a score greater than or equal to 80 because in our collection we have the document with a duplicate student name as the student can be present twice, so we have to also use $group operator to also find distinct value.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="c1"&gt;//Our student's collection with the name of the student as field "student"&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;_id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ObjectId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;512bc95fe835e68f199c8686&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;student&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Dave Smith&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;score&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;80&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;_id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ObjectId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;512bc962e835e68f199c8687&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;student&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Dave Smith&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;score&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;85&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;_id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ObjectId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;55f5a192d4bede9ac365b257&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;student&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Ahn ben&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;score&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;_id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ObjectId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;55f5a192d4bede9ac365b258&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;student&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;li xin&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;score&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;55&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;_id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ObjectId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;55f5a1d3d4bede9ac365b259&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;student&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Ben hue&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;score&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;_id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ObjectId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;55f5a1d3d4bede9ac365b25a&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;student&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;li xin&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;score&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;94&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;_id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ObjectId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;55f5a1d3d4bede9ac365b25b&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;student&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Peter Parker&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;score&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;95&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;//we will be using two stages here for extracting the data&lt;/span&gt;
&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;students&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;aggregate&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt; 
                     &lt;span class="p"&gt;{&lt;/span&gt;  &lt;span class="c1"&gt;//first stage will find the document with the score&lt;/span&gt;
                        &lt;span class="c1"&gt;//greater than or equal to 80&lt;/span&gt;
                       &lt;span class="na"&gt;$match&lt;/span&gt;&lt;span class="p"&gt;:{&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;score&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:{&lt;/span&gt; &lt;span class="na"&gt;$gte&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;80&lt;/span&gt; &lt;span class="p"&gt;}}&lt;/span&gt;
                     &lt;span class="p"&gt;},&lt;/span&gt;
                     &lt;span class="p"&gt;{&lt;/span&gt;  &lt;span class="c1"&gt;//second Stage will group that distinct name from each document got from the first stage&lt;/span&gt;
                       &lt;span class="na"&gt;$group&lt;/span&gt;&lt;span class="p"&gt;:{&lt;/span&gt; &lt;span class="na"&gt;_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;$student&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
                     &lt;span class="p"&gt;}&lt;/span&gt;
                     &lt;span class="p"&gt;])&lt;/span&gt;

&lt;span class="c1"&gt;//The result we get from this two-stage pipeline is &lt;/span&gt;
&lt;span class="c1"&gt;//In the first stage we get the four documents with similar names  Dave Smith two times&lt;/span&gt;
&lt;span class="c1"&gt;//from the second stage we will group those documents based on distinct student name&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;_id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Dave Smith&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;_id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;li xin&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;_id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Peter Parker&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;


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

&lt;/div&gt;

&lt;h3&gt;
  
  
  Conclusion:
&lt;/h3&gt;

&lt;p&gt;In this Article, we now get a basic understanding of what is the &lt;strong&gt;$match&lt;/strong&gt; and &lt;strong&gt;$group&lt;/strong&gt; operators, why it is used and how they minimize the filter techniques which involve a tedious process at the front end if it is not filtered from the backend.&lt;/p&gt;

&lt;p&gt;But MongoDB's aggregation pipeline can do this thing in merely two lines of command to extract the particular required data, and that is what we call the essence of MongoDB Aggregation Power which can solve a bigger problem.&lt;/p&gt;

&lt;p&gt;I hope you like this article a lot and would appreciate my work, I have learned this thing from the internet as well do check the below link, and also stay tuned for further part of this series&lt;/p&gt;

&lt;p&gt;Do Like, Comment, Share and Subscribe to my Newsletter for getting my work directly in your inbox. &lt;/p&gt;


&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
    &lt;a href="https://www.mongodb.com/docs/manual/reference/operator/aggregation/group/" rel="noopener noreferrer"&gt;
      mongodb.com
    &lt;/a&gt;
&lt;/div&gt;



</description>
      <category>webdev</category>
      <category>mongodb</category>
      <category>beginners</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Introduction to Aggregation Pipeline in MongoDB (Part 1)</title>
      <dc:creator>Ganesh Yadav</dc:creator>
      <pubDate>Sun, 14 Jan 2024 15:46:49 +0000</pubDate>
      <link>https://dev.to/ganeshyadav3142/introduction-to-aggregation-pipeline-in-mongodb-part-1-2cfo</link>
      <guid>https://dev.to/ganeshyadav3142/introduction-to-aggregation-pipeline-in-mongodb-part-1-2cfo</guid>
      <description>&lt;p&gt;Hello and Welcome back readers to this amazing series in which we are going to explore deeply the MongoDB aggregation pipeline and how it reduces the number of steps and simplifies the process of data extraction from your database.&lt;/p&gt;

&lt;p&gt;But Before moving forward with Aggregation Pipeline, let us learn what are the diiferent types of aggregation that are available or provided by MongoDB.&lt;/p&gt;

&lt;h2&gt;
  
  
  Types of Aggregation in MongoDB:
&lt;/h2&gt;

&lt;p&gt;So MongoDB provides three types of Aggregation&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Map Reduce Function&lt;/li&gt;
&lt;li&gt;Single Purpose Aggregation&lt;/li&gt;
&lt;li&gt;Aggregation Pipeline&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Map Reduce Function:
&lt;/h2&gt;

&lt;p&gt;Map Reduce is used for aggregating results for a large volume of data. Map reduce has two main functions one is a map that groups all the documents and the second one is the reduce which operates on the grouped data.&lt;/p&gt;

&lt;p&gt;Although the Map Reduce type of Aggregation is not generally used, because MongoDB provide an Advance Aggregation Pipeline, we will see this Map Reduce for learning purposes and how it is used, the basic syntax for map-reduce aggregation is as follows,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;collectionName&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mapReduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;mappingFunction&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reduceFunction&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Result&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;You can see from the above expression, that for the map-reduce type of aggregation, we have to provide three arguments, which are mappingFunction, reduceFunction and out Results format.&lt;/p&gt;

&lt;p&gt;The mappingFunction generally selects a key or the fields from the document that is generally mentioned inside the mappingFunction.&lt;/p&gt;

&lt;p&gt;These selected types of Documents are then fed as the input to the reduceFunction which performs operations to those selected documents and provides the out Results in the form new Collection.&lt;/p&gt;

&lt;p&gt;Now Let us See an Example using Map Reduce,** Where we will group the documents based on age and find total marks in each age group.**&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//let the students collections has documents as&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nc"&gt;ObjectId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1384y125y1857405745c6d&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="nx"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;zack&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;19&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="nx"&gt;totalMarks&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;30&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nc"&gt;ObjectId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1384y125y1857405745c6d&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="na"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ray&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;27&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="na"&gt;totalMarks&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;15&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nc"&gt;ObjectId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1384y125y1857405745c6d&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="na"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;hiro&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;27&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="na"&gt;totalMarks&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;30&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;.........&lt;/span&gt;
&lt;span class="c1"&gt;//we can apply map reduce, method of aggregation to &lt;/span&gt;
&lt;span class="c1"&gt;//calculate the total marks with a particular age group&lt;/span&gt;
&lt;span class="c1"&gt;//here we are mapping the selected key such as age and marks&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;mapfunction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;&lt;span class="nf"&gt;emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;totalMarks&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;reducefunction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;values&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;values&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;
&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;studentsMarks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mapReduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;mapfunction&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reducefunction&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;out&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Result&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="c1"&gt;//So, we will create two variables first mapfunction &lt;/span&gt;
&lt;span class="c1"&gt;//which will emit age as a key (expressed as “_id” in the output) and &lt;/span&gt;
&lt;span class="c1"&gt;//marks as value this emitted data is passed to our reducefunction, &lt;/span&gt;
&lt;span class="c1"&gt;//which takes key and value as grouped data, and then it performs &lt;/span&gt;
&lt;span class="c1"&gt;//operations over it. After performing reduction the results are stored &lt;/span&gt;
&lt;span class="c1"&gt;//in a collection here in this case the collection is Results.&lt;/span&gt;
&lt;span class="c1"&gt;//the results of the operation is stored inside new collection created as&lt;/span&gt;
&lt;span class="c1"&gt;//Results and when we find the document inside these Results collection as&lt;/span&gt;
&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Results&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;19&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;27&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;45&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;//here we calculated the total marks for each age group&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Single Purpose Aggregation:
&lt;/h2&gt;

&lt;p&gt;This type of aggregation is generally used for simple operations such as counting the number of documents present inside the collections and such as fetching the distinct student's name inside our student collection.&lt;/p&gt;

&lt;p&gt;The Basic syntax for single-purpose aggregation is shown&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//Single Purpose Aggregation&lt;/span&gt;
&lt;span class="c1"&gt;//for finding all distinct student name&lt;/span&gt;
&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;students&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;distinct&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;//will return array of distinct name present inside the collection&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;zack&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ray&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;hiro&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="c1"&gt;//for finding total count of document&lt;/span&gt;
&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;students&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;count&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="c1"&gt;//return a number of total count of document inside the particular collection&lt;/span&gt;
&lt;span class="mi"&gt;3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Aggregation Pipeline:
&lt;/h2&gt;

&lt;p&gt;Now, we move forward to the next type of aggregation, which is most widely used and is recommended to use for simple as well as advanced aggregation.&lt;/p&gt;

&lt;p&gt;As the name suggests, a pipeline is generally a series of stages where the output of one stage is fed as the input of another stage, depending upon your requirements you can put as many stages as you want until you get your desired results.&lt;/p&gt;

&lt;p&gt;The basic definition of MongoDB Aggregation Pipeline is mentioned in their official documentation as follows:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;"An aggregation pipeline consists of one or more stages that process documents"&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Each stage operates on the input documents. For example, a stage can filter documents, group documents, and calculate values.&lt;/li&gt;
&lt;li&gt;The documents that are output from a stage are passed to the next stage.&lt;/li&gt;
&lt;li&gt;An aggregation pipeline can return results for groups of documents. For example, return the total, average, maximum, and minimum values.
The general syntax for the aggregation pipeline is shown
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;collectionName&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;aggregate&lt;/span&gt;&lt;span class="p"&gt;([])&lt;/span&gt;
&lt;span class="c1"&gt;//this array inside aggregate function is called the pipeline array which&lt;/span&gt;
&lt;span class="c1"&gt;//generally carries your stages of pipeline and expression insde it.&lt;/span&gt;

&lt;span class="c1"&gt;//Advnaced syntax breakdown&lt;/span&gt;
&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;collectionName&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;aggregate&lt;/span&gt;&lt;span class="p"&gt;([{&lt;/span&gt;&lt;span class="na"&gt;$group&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;  &lt;span class="c1"&gt;//stage&lt;/span&gt;
                             &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;$id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;//Expression&lt;/span&gt;
                              &lt;span class="na"&gt;total&lt;/span&gt;&lt;span class="p"&gt;:{&lt;/span&gt;&lt;span class="na"&gt;$sum&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;$fare&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}}}])&lt;/span&gt;
                            &lt;span class="c1"&gt;//Accumulator&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;From the Above Expression, it is clear for using the aggregation pipeline in MongoDB we have to use the .aggregate method as well as we have to provide the pipeline array inside it where all the stages and filtering happens.&lt;/p&gt;

&lt;p&gt;The advanced Aggregation pipeline generally uses multiple stages but they have some common things such as there should be the compulsory a Stage operator and there should be also the Expression present which generally refers to the input field inside the actual document in which the aggregation is going to occur such as age and totalMarks field and the third one is called accumulator which generally refers to the thing you want after you filter or grouped the document, such as sometimes we have to $sort the output or we have to $count of all the document present after the pipeline happens.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion:
&lt;/h2&gt;

&lt;p&gt;So, till now we only got to see what is the aggregation and what are the different aggregation methods that are provided by MongoDB and the most widely used technique of aggregation,&lt;/p&gt;

&lt;p&gt;In the further part of this series, we will be going to explore in depth the different stages and grouping operators provided by MongoDB to carry out aggregation pipelines such as $match,$group,$unwind, and $lookup, and how to optimize these aggregation stages to get the required results in the optimized format.&lt;/p&gt;

&lt;p&gt;So stay tuned for the further part of the series, as we will explore this beautiful concept of aggregation pipeline in MongoDB.&lt;/p&gt;

&lt;p&gt;Do Like, Comment, Share and Subscribe to my Newsletter for getting my work directly in your inbox. &lt;/p&gt;

</description>
      <category>mongodb</category>
      <category>beginners</category>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Authentication Using Context in Graphql (Part 4)</title>
      <dc:creator>Ganesh Yadav</dc:creator>
      <pubDate>Fri, 12 Jan 2024 17:44:26 +0000</pubDate>
      <link>https://dev.to/ganeshyadav3142/authentication-using-context-in-graphql-part-4-4emn</link>
      <guid>https://dev.to/ganeshyadav3142/authentication-using-context-in-graphql-part-4-4emn</guid>
      <description>&lt;p&gt;Hello and welcome back once again, to this beautiful series where we are going to explore deeply, what is graphql and how it is been used in today's web development you can read all the previous articles related to this series by clicking &lt;a href="https://dev.to/ganeshyadav3142/mutation-in-graphql-and-how-to-perform-createupdate-and-deletepart-3-17ae"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Till now, up to the 3 parts of this series which we have discussed previously, we have learned a lot of things in graphql such as&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Benefits of using Graphql over REST API.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;What is typeDefs, resolvers, and input.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;What are the queries and mutations in graphql?&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;What are Aliases, arguments and fragments in graphql?&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We have also seen, what are the parameters accepted by resolvers.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;How to Set up our graphQL server.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now, In this article, we will learn more about the Context in graphql and how to use it in resolvers to provide authentication and authorization.&lt;/p&gt;

&lt;p&gt;During a GraphQL operation, you can share data throughout your server's &lt;strong&gt;resolvers&lt;/strong&gt; and &lt;strong&gt;plugins&lt;/strong&gt; by creating an object named contextValue.&lt;/p&gt;

&lt;p&gt;Your GraphQL API probably needs to control which users can see and interact with the various data it provides.&lt;/p&gt;

&lt;p&gt;In software development, you can control this access of data to the user by using the concept of &lt;strong&gt;authentication&lt;/strong&gt; and &lt;strong&gt;authorization&lt;/strong&gt;, and we can also use this technique in graphql by using &lt;strong&gt;contextValue&lt;/strong&gt; provided by graphql.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Authentication&lt;/strong&gt; is determining whether a given user is logged in, and subsequently determining which user someone is known.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Authorization&lt;/strong&gt; is then determining what a given user has permission to do or see.&lt;/p&gt;

&lt;h2&gt;
  
  
  Putting authenticated user info in your ContextValue:
&lt;/h2&gt;

&lt;p&gt;Whenever we want to restrict the user from accessing certain data, we can authenticate the user in the contextValue, this process of **authenticating **the user in grapql can be done with multiple techniques but one of the very widely used methods is to provide the including HTTP headers and JSON web tokens.&lt;/p&gt;

&lt;p&gt;The example below extracts a user token from the HTTP Authorization header included in each operation request. It then fetches the corresponding user object for that token and adds it to the contextValue Object that's passed to every executing resolver. Each resolver can then use this object to determine what data the user has access to.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;ApolloServer&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@apollo/server&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;startStandaloneServer&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@apollo/server/standalone&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;server&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ApolloServer&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="nx"&gt;typeDefs&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;resolvers&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;url&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;startStandaloneServer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;server&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="c1"&gt;// Note: This example uses the `req` argument to access headers,&lt;/span&gt;
  &lt;span class="c1"&gt;// but the arguments received by `context` vary by integration.&lt;/span&gt;
  &lt;span class="c1"&gt;// This means they vary for Express, Fastify, Lambda, etc.&lt;/span&gt;

  &lt;span class="c1"&gt;// For `startStandaloneServer`, the `req` and `res` objects are&lt;/span&gt;
  &lt;span class="c1"&gt;// `http.IncomingMessage` and `http.ServerResponse` types.&lt;/span&gt;
  &lt;span class="na"&gt;context&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Get the user token from the headers.&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;token&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;authorization&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// Try to retrieve a user with the token&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;getUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;token&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// Add the user to the context&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;

&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`🚀 Server listening at: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; In Graphql, Because your contextValue is generated for every request, you don't have to worry about cleaning the user Data at the end of each operation execution.&lt;/p&gt;

&lt;p&gt;From the above code, it is clearly understood that a contextValue is a asynchronous function which returns an object, this object is then accessible to your server's resolver.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to Utilise this ContextValue in Resolvers:
&lt;/h2&gt;

&lt;p&gt;Since we have created a contextValue function in our server, we can now use this contextValue in the resolver to authenticate the user, Let us see how it is done.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;GraphQLError&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;graphql&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;resolvers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;Query&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Example resolver&lt;/span&gt;
    &lt;span class="c1"&gt;//we can use contextValue, in third parameter of our resolver&lt;/span&gt;
    &lt;span class="na"&gt;adminExample&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;contextValue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;info&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;contextValue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="nx"&gt;ADMIN&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;GraphQLError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;not admin!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="na"&gt;extensions&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;code&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;UNAUTHENTICATED&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;//You can now allow users to access the data&lt;/span&gt;
       &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this way, using the contextValue we can now make our graphql server that has the ability to authenticate and authorize the user with particular access to the data based on eligibility.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusions:
&lt;/h2&gt;

&lt;p&gt;Overall, by reading this article we can clearly understand how to authenticate the user using contextValue and how to create this contextValue function inside our server which will return an object.&lt;/p&gt;

&lt;p&gt;I hope you find the article valuable and worth reading to gain some insight about contextValue in graphql, I have learned this topic on the internet and as well as graphQL docs which will be shared below, check out these graphql docs to have a better understanding of the topics.&lt;/p&gt;

&lt;p&gt;Liquid error: internal&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>graphql</category>
    </item>
    <item>
      <title>Mutation in GraphQl and how to perform Create,Update and Delete(Part 3)</title>
      <dc:creator>Ganesh Yadav</dc:creator>
      <pubDate>Mon, 01 Jan 2024 08:52:17 +0000</pubDate>
      <link>https://dev.to/ganeshyadav3142/mutation-in-graphql-and-how-to-perform-createupdate-and-deletepart-3-17ae</link>
      <guid>https://dev.to/ganeshyadav3142/mutation-in-graphql-and-how-to-perform-createupdate-and-deletepart-3-17ae</guid>
      <description>&lt;p&gt;Hello Readers and welcome to another article of the series in Mastering Graphql, if you are reading this on a continuous then thank you, and if you are new to this article then I would like you to visit and read the previous parts of the series &lt;a href="https://dev.to/ganeshyadav3142/getting-started-with-graphql-why-it-is-required-part-1-kg5"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Now, let us shift our focus to how to create, update and delete a particular data in graphql using the &lt;strong&gt;mutation&lt;/strong&gt; keyword, as we had learned from the previous article graphql is a query language, and to get a particular data we use the &lt;strong&gt;query&lt;/strong&gt; keyword, we have also seen what the use of &lt;strong&gt;aliases&lt;/strong&gt; and &lt;strong&gt;arguments&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Mutation:
&lt;/h2&gt;

&lt;p&gt;Most discussions of GraphQL focus on data fetching, but any complete data platform needs a way to modify server-side data as well.&lt;/p&gt;

&lt;p&gt;And there comes mutation, as the name suggests, mutation generally refers to the changes, and in graphql mutation is the keyword that is used for mutating the data, which is similar to &lt;strong&gt;POST&lt;/strong&gt;, &lt;strong&gt;DELETE&lt;/strong&gt; and &lt;strong&gt;PATCH&lt;/strong&gt; requests in &lt;strong&gt;REST&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Now let us look at each of the examples one by one, such as creating, updating and deleting data using mutation&lt;/p&gt;

&lt;h2&gt;
  
  
  Create Data using mutation:
&lt;/h2&gt;

&lt;p&gt;Let's now once again head back to our Apollo Studio and the example of adding a post which we discussed in the previous parts, but before moving forward let us see the structure of our mutation, which we going to use to create a post.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--F_qzU2ow--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ehhysvcdy6kzrehn62z3.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--F_qzU2ow--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ehhysvcdy6kzrehn62z3.jpg" alt="Mutation Structure" width="753" height="287"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;you can see that the structure for the creation of a post is what we get as a response.&lt;/p&gt;

&lt;p&gt;But, In general, in Apollo Studio you also have to pass the required variable to create a post in this case the &lt;strong&gt;post&lt;/strong&gt; and &lt;strong&gt;addPostInput&lt;/strong&gt; are the type that we have to include in our typeDefs folder which we created during our server creation&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//tpeDefs.js&lt;/span&gt;
&lt;span class="nx"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Mutation&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="c1"&gt;//addPost is the mutation which returns a particular Post&lt;/span&gt;
&lt;span class="nf"&gt;addPost&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;addPostInput&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;&lt;span class="nx"&gt;Post&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; 

&lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="nx"&gt;addPostInput&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;image&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;
  &lt;span class="nx"&gt;description&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;
  &lt;span class="c1"&gt;//you can more fields&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;//Furthermore this mutation and add Post need to be resolved in resolvers.js&lt;/span&gt;


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

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//resolvers.js&lt;/span&gt;
&lt;span class="nx"&gt;Mutation&lt;/span&gt; &lt;span class="p"&gt;:{&lt;/span&gt;
&lt;span class="c1"&gt;//you can use the async function as well for error handling&lt;/span&gt;
 &lt;span class="nl"&gt;addPost&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="c1"&gt;//this is your request which the user sends and post is a variable&lt;/span&gt;
       &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;image&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;description&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;
        &lt;span class="c1"&gt;//your logic for handling and validating the request and &lt;/span&gt;
        &lt;span class="c1"&gt;//save the data in the particular Database&lt;/span&gt;
        &lt;span class="c1"&gt;//e.g. for MongoDB, you can use Models to save the data&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


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

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ARS80irm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/leh2qmt34l23jiw18dxk.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ARS80irm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/leh2qmt34l23jiw18dxk.jpg" alt="Creating" width="800" height="379"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;we can see, how the data is created using mutation and arguments passed as a variable, such as the Apollo studio responds with data that we have created.&lt;/p&gt;

&lt;h2&gt;
  
  
  Delete Data using mutation:
&lt;/h2&gt;

&lt;p&gt;Deleting data in the graphQL is somewhat similar to what we do in the &lt;strong&gt;REST API&lt;/strong&gt;, in this for deleting a particular data you can pass the argument to a mutation similar to what we did in creating data, but instead, the argument field needs to be a unique value for identifying the particular data or object.&lt;/p&gt;

&lt;p&gt;let us now see an example of deleting a post, with a unique postId&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//typeDefs.js&lt;/span&gt;
&lt;span class="nx"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Mutation&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="c1"&gt;//id represents a unique value for a particular data&lt;/span&gt;
&lt;span class="nf"&gt;deletePost&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;ID&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;&lt;span class="nx"&gt;Post&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;   
&lt;span class="c1"&gt;// mutation and query which we create in typeDefs needs &lt;/span&gt;
&lt;span class="c1"&gt;//to be resolved inside  resolvers.js&lt;/span&gt;

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

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//resolvers.js&lt;/span&gt;
&lt;span class="c1"&gt;//you can use the async function as well for error handling&lt;/span&gt;
&lt;span class="nx"&gt;deletePost&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="c1"&gt;//extract the unique value passed through an argument using a variable&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;args&lt;/span&gt;
       &lt;span class="c1"&gt;//your inside logic for validating a request and deleting a post&lt;/span&gt;
      &lt;span class="c1"&gt;// e.g. in case of MongoDB we use findbyIDAndDelete(id)&lt;/span&gt;
      &lt;span class="c1"&gt;//Return the deleted post or a response&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;


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

&lt;/div&gt;



&lt;p&gt;Let us now actually, see how it works in Apollo Studio, how we pass the argument to delete a post, below is a variable as &lt;strong&gt;deletePostId&lt;/strong&gt; the id of the post which we want to delete&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GMoRi3j---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2sjyjbz932iheewvfd2k.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GMoRi3j---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2sjyjbz932iheewvfd2k.jpg" alt="Deleting" width="800" height="364"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Update Data using Mutation:
&lt;/h2&gt;

&lt;p&gt;Updating data in graphql is also similar to &lt;strong&gt;REST&lt;/strong&gt;, but unlike &lt;strong&gt;REST&lt;/strong&gt; which has both the requests such as &lt;strong&gt;PUT&lt;/strong&gt; and &lt;strong&gt;PATCH&lt;/strong&gt;, graphql offers only mutation which can be utilized in both cases based on developer choice, so you have control over whether you want to update only a certain field which user mentions or update a whole data.&lt;/p&gt;

&lt;p&gt;We will be utilizing mutation for only the field which the user wants to update, based on arguments passed for updating the data.&lt;/p&gt;

&lt;p&gt;let us visualize it in &lt;strong&gt;typeDefs&lt;/strong&gt; and &lt;strong&gt;resolvers&lt;/strong&gt;, which are shown below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//typeDefs.js&lt;/span&gt;
&lt;span class="nx"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Mutation&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="c1"&gt;//we have mention also id here for selecting the post and then updating it&lt;/span&gt;
&lt;span class="nf"&gt;updatePost&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;ID&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;update&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;updatePostInput&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;&lt;span class="nx"&gt;Post&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="nx"&gt;updatePostInput&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="c1"&gt;//you can mention your update post field or add more field&lt;/span&gt;
&lt;span class="nl"&gt;image&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="nx"&gt;description&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//resolvers.js&lt;/span&gt;
&lt;span class="c1"&gt;//you can use the async function as well for error handling&lt;/span&gt;
&lt;span class="nx"&gt;updatePost&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="c1"&gt;//extracting the arguments variable passed &lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;image&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;description&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;update&lt;/span&gt;
      &lt;span class="c1"&gt;//logic for validation of the request for the particular id&lt;/span&gt;
     &lt;span class="c1"&gt;// and then updating the required fields which is mentioned&lt;/span&gt;
    &lt;span class="c1"&gt;// e.g. in MongoDB we can use findByIdAndUpdate(id) to update the data&lt;/span&gt;
    &lt;span class="c1"&gt;//return the updated post &lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let us now, see how it happens in Apollo studio we are going to pass only one field for updating the particular post along with postID which we have created, during our create post segment, see above.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KB44xCll--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eo4x62guodjrt46dm2jh.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KB44xCll--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eo4x62guodjrt46dm2jh.jpg" alt="Updating" width="800" height="361"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above, we can see how the arguments are passed to update a particular post, you can also see that the update variable only contains a description field which is required to update and hence it is updated successfully as seen in the response.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion:
&lt;/h2&gt;

&lt;p&gt;From the Above discussion, we now clearly understand the concept of creating, updating and deleting in GraphQL, how it is done using the &lt;strong&gt;Mutation&lt;/strong&gt; keyword, and why there is only a requirement mutation keyword for making the three types of requests available in graphql, which is not in the case of REST APIs and that why the GraphQl also have the upper hand on this type of usage.&lt;/p&gt;

&lt;p&gt;Furthermore, until now till these &lt;strong&gt;3 Parts&lt;/strong&gt;, we can now clearly understand how to fetch data, create data, delete and update the data in grqpgL and perform &lt;strong&gt;CRUD&lt;/strong&gt; operations using both the &lt;strong&gt;query&lt;/strong&gt; and &lt;strong&gt;mutation&lt;/strong&gt; keywords.&lt;/p&gt;

&lt;p&gt;please leave a comment, if you have any questions or feedback.&lt;/p&gt;

</description>
      <category>graphql</category>
      <category>beginners</category>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>GraphQl with Apollo Server and usage of Query, Aliases,Arguments, Fragments (Part 2)</title>
      <dc:creator>Ganesh Yadav</dc:creator>
      <pubDate>Sun, 24 Dec 2023 15:51:07 +0000</pubDate>
      <link>https://dev.to/ganeshyadav3142/graphql-with-apollo-server-and-usage-of-query-aliasesarguments-fragments-part-2-9f9</link>
      <guid>https://dev.to/ganeshyadav3142/graphql-with-apollo-server-and-usage-of-query-aliasesarguments-fragments-part-2-9f9</guid>
      <description>&lt;p&gt;Hello and welcome back to another article of the GraphQl series, if you are new to this article I recommend you to visit &lt;strong&gt;Part 1&lt;/strong&gt; of this article series by clicking &lt;a href="https://dev.to/ganeshyadav3142/getting-started-with-graphql-why-it-is-required-part-1-kg5"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In the previous article, we learned about what graphql is, why it is used and what problem it solved which was in the case of REST, further we have also set up our graphql server using Apollo Server.&lt;/p&gt;

&lt;p&gt;Now, in this article, we will dive deeper into the functionality of graphql and how to perform advanced Query operations in GraphQL.&lt;/p&gt;

&lt;h2&gt;
  
  
  Query :
&lt;/h2&gt;

&lt;p&gt;As the name suggests, it's just a query for the specific fields of value that you want on objects. Let's start by looking at a very simple query and the result we get when we run it:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--eT0JuhRD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/25q8od8zeu6ski5s96ol.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--eT0JuhRD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/25q8od8zeu6ski5s96ol.jpg" alt="Query format" width="574" height="265"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In GraphQl, we represent a query by the keyword &lt;strong&gt;"query"&lt;/strong&gt; at first followed by the name of that particular query e.g. &lt;strong&gt;"hero"&lt;/strong&gt; and the set of fields you want to get as data, for e.g. in the above case look at the query structure and the response structure we get when we queried &lt;strong&gt;"hero"&lt;/strong&gt; both are exactly same we get what we want and that is the most important aspect using GraphQl over REST.&lt;/p&gt;

&lt;p&gt;Queries are mostly used for getting the data, just like the GET in REST API, while for updating and manipulating the data in graphql we have another keyword called &lt;strong&gt;"Mutation"&lt;/strong&gt; which we will see later. Now let us head to our server playground which we have created in Part 1.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xwOmNFfy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i8xa4xqefsz6ps4k8xcz.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xwOmNFfy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i8xa4xqefsz6ps4k8xcz.jpg" alt="Apollo studio" width="800" height="376"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above image of our Apollo server sandbox, you can see that we have a query &lt;strong&gt;"getAllPost"&lt;/strong&gt; and mention the field "description and look at the response structure we get from the server, which is exactly the same as the query.&lt;/p&gt;

&lt;h2&gt;
  
  
  Arguments :
&lt;/h2&gt;

&lt;p&gt;GraphQl also allows us to pass the argument to our query and we pass different arguments based on our requirements, whenever we pass the arguments to our queries we also have to resolve it in the &lt;strong&gt;"resolvers"&lt;/strong&gt;, that we have passed in while creating the Apollo server.&lt;/p&gt;

&lt;p&gt;Suppose we want to get the posts of a single user, this can be done by just passing userID as an argument and creating the query for it e.g. &lt;strong&gt;userPost(id: ID!)&lt;/strong&gt; Now we have to resolve it in resolvers as shown below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//resolvers.js
 const resolvers={
Query:{
userPost:async(parent,args,context,info)=&amp;gt;{
      try {
          //extract id from args
            const {id} = args
          //your logic for getting single post
          //return postModel.findOne({userID:id})
         }
      catch{
            //default error logic
          } 
      }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;while resolving the particular query which has an argument passed, graphql basically accepts four parameters to resolve the query.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;parent&lt;/strong&gt;: The previous object, which is for a field on the root Query type is often not used.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;args&lt;/strong&gt;: args represents the argument which you specifically passed to a query.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;context&lt;/strong&gt;: A value which is provided to every resolver and holds important contextual information like the currently logged-in user, or access to a database.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;info&lt;/strong&gt;: A value which holds field-specific information relevant to the current query as well as the schema details.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;From the above-mentioned code snippets, we can clearly see that we can extract the variable passed inside arguments by a simple destructuring method, Let us how a query works when an argument is passed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--etmFFgxC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h4kqcqo4jex9nzeo2qm2.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--etmFFgxC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h4kqcqo4jex9nzeo2qm2.jpg" alt="Apollo studio" width="800" height="391"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;from above sandbox, represents how to pass an argument inside a query and extract the relevant data that we want by mentioning required fields.&lt;/p&gt;
&lt;h2&gt;
  
  
  Aliases :
&lt;/h2&gt;

&lt;p&gt;Aliases are nothing much, they are just the way to give our response the name which you want to give, for example in the above query we see that when we query &lt;strong&gt;"userPost"&lt;/strong&gt; we get the response with an array of same name as &lt;strong&gt;"userPost"&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now we can change this name to our appropriate name, which we want as shown below in an image.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TIQTgvNj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8pzg6qz6nufyityhfrdl.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TIQTgvNj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8pzg6qz6nufyityhfrdl.jpg" alt="Apollo Studio" width="800" height="367"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the sandbox, the query structure remains the same but here we now just have added the &lt;strong&gt;"singleUserPost"&lt;/strong&gt; at the beginning of our query &lt;strong&gt;"userPost"&lt;/strong&gt; and we can see that the array name we get as a response also got changed to &lt;strong&gt;"singleUserPost"&lt;/strong&gt; and this is what we called &lt;strong&gt;Aliases&lt;/strong&gt; in GraphQl.&lt;/p&gt;
&lt;h2&gt;
  
  
  Fragments :
&lt;/h2&gt;

&lt;p&gt;Let's say we had a relatively complicated page in our app, which lets us look at two heroes side by side, along with their friends. You can imagine that such a query could quickly get complicated because we would need to repeat the fields at least once - one for each side of the comparison.&lt;/p&gt;

&lt;p&gt;That's why GraphQL includes reusable units called &lt;strong&gt;fragments&lt;/strong&gt;. Fragments let you construct sets of fields and then include them in queries where you need to.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Au9fq6Yl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qz4dc23zvupraw93g7sn.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Au9fq6Yl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qz4dc23zvupraw93g7sn.jpg" alt="Fragments" width="800" height="385"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above image shows fragments example as two separate queries that can be compared on a single go.&lt;/p&gt;
&lt;h2&gt;
  
  
  Conclusion:
&lt;/h2&gt;

&lt;p&gt;Therefore, we once again concluded from this article that there are multiple ways we can curate our query in graphQl and how we modify our query in graphQL such that we can get only the specific fields and data that are relevant to us, which were not case of REST because of over-fetching and under-fetching,&lt;/p&gt;

&lt;p&gt;furthermore, we learned about the different aspects of using Aliases, arguments and fragments efficiently in GraphQL.&lt;/p&gt;

&lt;p&gt;In the Next Article, we will explore more about how to perform the create, update and delete functionality using another keyword called "Mutation" and how to write resolvers for it Efficiently. &lt;strong&gt;So, sit tight because there is much more to explore in the series.&lt;/strong&gt;&lt;/p&gt;


&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
    &lt;a href="https://graphql.org/" rel="noopener noreferrer"&gt;
      graphql.org
    &lt;/a&gt;
&lt;/div&gt;



</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>graphql</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Getting Started With GraphQL &amp; Why it is Required (Part 1)</title>
      <dc:creator>Ganesh Yadav</dc:creator>
      <pubDate>Tue, 19 Dec 2023 18:29:40 +0000</pubDate>
      <link>https://dev.to/ganeshyadav3142/getting-started-with-graphql-why-it-is-required-part-1-kg5</link>
      <guid>https://dev.to/ganeshyadav3142/getting-started-with-graphql-why-it-is-required-part-1-kg5</guid>
      <description>&lt;p&gt;In this article, we will explore the most loved query language which resolves the most important problem statement in modern web app development&lt;/p&gt;

&lt;p&gt;Although there are quite a lot of techniques to work with Graphql, for the sake of simplicity we will be going to use Apollo Server as your server for the Graphql&lt;/p&gt;

&lt;p&gt;But before moving forward let us understand, why there is a need for Graphql.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why Graphql?
&lt;/h2&gt;

&lt;p&gt;Graphql was developed by Facebook in the year 2012, and it was open-sourced in the year 2015, it was developed to address some of the limitations and challenges that were associated with REST APIs, the challenges include&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Under-fetching&lt;/strong&gt;: In REST, endpoints are fixed, and the server determines the structure of the response. This can lead to under-fetching (receiving less data than needed).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Over-fetching&lt;/strong&gt;: The REST also have an issue called Over-fetching(receiving more data than needed), where generally we get more data than is required which can affect the efficiency.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Single Request for Multiple Resources&lt;/strong&gt;: With GraphQL, clients can request multiple resources in a single query, reducing the number of HTTP requests needed to retrieve data. This can result in faster and more efficient communication between clients and servers.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Flexible Schema and Strong Typing&lt;/strong&gt;: GraphQL uses a flexible schema that allows clients to define the shape of the response they expect. It also enforces strong typing, providing clarity about the types of data that can be queried and reducing the chance of runtime errors.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;let us now clearly, understand how Graphql reduces under-fetching and over-fetching, by taking the example of the user, in REST we have to create a different endpoint for getting with id &lt;strong&gt;/user/id&lt;/strong&gt;, similarly for getting a post of a particular user &lt;strong&gt;/user/id/posts&lt;/strong&gt; and for followers we have &lt;strong&gt;/user/id/followers&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ky6-eDzO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5foig2w8gfk1mn9zodph.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ky6-eDzO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5foig2w8gfk1mn9zodph.jpg" alt="Request for particular user with id" width="800" height="248"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6UDI3xLb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m7mg9lm9xva2i74mj9gw.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6UDI3xLb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m7mg9lm9xva2i74mj9gw.jpg" alt="Request for particular user Post" width="800" height="239"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--dqYEXx4E--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3lzql5qff3gc2jg0p91b.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dqYEXx4E--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3lzql5qff3gc2jg0p91b.jpg" alt="Request for particular user Followers" width="800" height="278"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With REST, you have to make three requests to different endpoints to fetch the required data. You’re also overfetching since the endpoints return additional information that’s not needed.&lt;/p&gt;

&lt;p&gt;In GraphQL on the other hand, you’d simply send a single query to the GraphQL server that includes the concrete data requirements. The server then responds with a JSON object where these requirements are fulfilled.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---bA90j13--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fxpwv5cxdkkzjjfidvuq.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---bA90j13--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fxpwv5cxdkkzjjfidvuq.jpg" alt="query using Graphql" width="800" height="519"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;Now, let us head toward creating our first GraphQl server using Apollo Server&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating Our First Server :
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Our First step includes creation of a new Project , you have to ensure that your system has node.js installed
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; mkdir my-first-graphql-server
 cd my-first-graphql-server
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Now, let's Initialize our project using npm(node package manager) and set our type in the package.json file as "module", so that we can use the import statement, the below command will create a package.json file inside your project directory
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm init --yes &amp;amp;&amp;amp; npm pkg set type="module"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Install Dependencies, to use the graphql and Apollo server, we have to install the dependencies and also nodemon for running our server.
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; npm install @apollo/server graphql nodemon
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Now, here comes the most interesting part of creating the server in graphql, we have to define the &lt;strong&gt;Graphql Schema&lt;/strong&gt; which typically represents the structure of our data present and the shape of the query, it is typically denoted with &lt;strong&gt;typeDefs&lt;/strong&gt;, now create an index.js file inside our project directory and import the certain things and create the &lt;strong&gt;typeDefs&lt;/strong&gt; as mentions which refers to ours schema,
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; //index.js
 import { ApolloServer } from '@apollo/server';
 import { startStandaloneServer } from '@apollo/server/standalone';

 // A schema is a collection of type definitions (hence "typeDefs")
 // that together define the "shape" of queries that are executed against
 // your data.
 //Adding #graphql to the beginning of a template literal provides 
 //GraphQL syntax highlighting in supporting IDEs.
 const typeDefs = `#graphql
   type Book {
     title: String
     author: String
   }
   type Query {
     books: [Book]
   }
 `;

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

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Again inside our &lt;strong&gt;index.js&lt;/strong&gt; or in other files you can mention your data and export the data if required, again for a simple purpose we are just taking &lt;strong&gt;books&lt;/strong&gt; which represent the array of objects with a particular book &lt;strong&gt;title&lt;/strong&gt; and &lt;strong&gt;author&lt;/strong&gt;.
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; //index.js 
 const books = [
   {
     title: 'The Awakening',
     author: 'Kate Chopin',
   },
   {
     title: 'City of Glass',
     author: 'Paul Auster',
   },
 ];

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

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Define the resolvers inside &lt;strong&gt;index.js&lt;/strong&gt;, now that we have created our data set we have to create our resolvers which are basically a function that tells the Apollo server how to fetch the particular data when a certain query is made and what the given query returns, below code snippets will return all the books from the array, when the &lt;strong&gt;books&lt;/strong&gt; query is made.
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; //index.js
 // Resolvers define how to fetch the types defined in your schema.
 // This resolver retrieves books from the "books" array above.
 const resolvers = {
   Query: {
     books: () =&amp;gt; books,
   },
 };

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

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Now we need to create our instance of ApolloServer, inside the index.js file which will take &lt;strong&gt;typeDefs and resolvers,&lt;/strong&gt;We've defined our schema, data set, and resolver. Now, we need to provide this information to Apollo Server when we initialize it.
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; //index.js
 // The ApolloServer constructor requires two parameters: your schema
 // definition and your set of resolvers.
 const server = new ApolloServer({
   typeDefs,
   resolvers,
 });

 // Passing an ApolloServer instance to the `startStandaloneServer` function:
 //  1. creates an Express app
 //  2. installs your ApolloServer instance as middleware
 //  3. prepares your app to handle incoming requests
 const { url } = await startStandaloneServer(server, {
   listen: { port: 4000 },
 });

 console.log(`🚀  Server ready at: ${url}`);

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

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Now for &lt;strong&gt;running the server&lt;/strong&gt;, we have to make some changes in our script object present inside the &lt;strong&gt;package.json&lt;/strong&gt; file, since we have already installed &lt;strong&gt;nodemon&lt;/strong&gt;,it is time we have to utilise it while &lt;strong&gt;running the server&lt;/strong&gt;, add the below code snippet to our &lt;strong&gt;package.json&lt;/strong&gt; files scripts &lt;strong&gt;object&lt;/strong&gt;.
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//package.json
 "scripts": {
    "test": "echo \"Error: no test specified\" &amp;amp;&amp;amp; exit 1",
    "server":"nodemon index.js"  //this runs your index.js file
  },

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

&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//now open the terminal and write the start command this will
//start your Apollo server at port 4000 with the URL as http://localhost:4000/
npm run server

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

&lt;/div&gt;


&lt;p&gt;Ahh we now have created our first Graphql server using Apollo Server, opened our localhost and make the query in the playground, provided by ApolloServer playground as shown in the the image.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--95CeKjUX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pezd6yzkyvtiv1tmlkg7.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--95CeKjUX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pezd6yzkyvtiv1tmlkg7.jpg" alt="Apollo Studio" width="800" height="413"&gt;&lt;/a&gt;&lt;/p&gt;


&lt;h2&gt;
  
  
  Conclusion :
&lt;/h2&gt;

&lt;p&gt;So, from this Article, we have learned how GraphQl is better than REST API and the benefits it provides over a conventional REST framework and we have also seen how it resolves the challenges of Under-fetching and Over-fetching by utilising the one-only query for different resources.&lt;/p&gt;

&lt;p&gt;Further, we have created our first GraphQl server using Apollo server and utilised its querying capabilities, in the next article we will further explore some of the different techniques present in graphql for manipulating the data known as Mutation&lt;/p&gt;

&lt;p&gt;I have learned a lot about this topic from the internet and also advise you the same for more exploring and deep diving, do reply and comment on this article for better sharing and good content creation.&lt;/p&gt;




&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
      &lt;div class="c-embed__cover"&gt;
        &lt;a href="https://www.apollographql.com/docs/apollo-server/" class="c-link s:max-w-50 align-middle" rel="noopener noreferrer"&gt;
          &lt;img alt="" src="https://res.cloudinary.com/practicaldev/image/fetch/s--eZvwn1TR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://res.cloudinary.com/apollographql/image/upload/w_1280%2Ch_669%2Cc_fill%2Cq_auto%2Cf_auto/w_1120%2Cc_fit%2Cco_rgb:FFFFFF%2Cg_south_west%2Cx_80%2Cy_254%2Cl_text:Source%2520Sans%2520Pro_80_bold:Introduction%2520to%2520Apollo%2520Server/w_1120%2Cc_fit%2Cco_rgb:FFFFFF%2Cg_north_west%2Cx_80%2Cy_445%2Cl_text:Source%2520Sans%2520Pro_48:Apollo%2520Server/apollo-docs-template2_dohzxt" height="" class="m-0" width=""&gt;
        &lt;/a&gt;
      &lt;/div&gt;
    &lt;div class="c-embed__body"&gt;
      &lt;h2 class="fs-xl lh-tight"&gt;
        &lt;a href="https://www.apollographql.com/docs/apollo-server/" rel="noopener noreferrer" class="c-link"&gt;
          Introduction to Apollo Server - Apollo GraphQL Docs
        &lt;/a&gt;
      &lt;/h2&gt;
      &lt;div class="color-secondary fs-s flex items-center"&gt;
          &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://res.cloudinary.com/practicaldev/image/fetch/s--F6KL6X2H--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://www.apollographql.com/docs/favicon-32x32.png%3Fv%3De03dad83eb16cf475a13342272058ebe" width="32" height="32"&gt;
        apollographql.com
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;



</description>
      <category>graphql</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>map() v/s forEach() array function Comparsion</title>
      <dc:creator>Ganesh Yadav</dc:creator>
      <pubDate>Fri, 27 Oct 2023 17:49:44 +0000</pubDate>
      <link>https://dev.to/ganeshyadav3142/map-vs-foreach-array-function-comparsion-fj0</link>
      <guid>https://dev.to/ganeshyadav3142/map-vs-foreach-array-function-comparsion-fj0</guid>
      <description>&lt;p&gt;This article is all about a comparison between the JavaScript Functions available, which operate on the array only.&lt;/p&gt;

&lt;h2&gt;
  
  
  Function Overview :
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Array.map()
&lt;/h3&gt;

&lt;p&gt;The map function iterates over a given array and returns the newly created array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let array = [1, 2, 3];
array = array.map((item) =&amp;gt; {
  return item * 2;
});

console.log(array);  //output : [2, 4, 6]

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

&lt;/div&gt;

&lt;h3&gt;
  
  
  Array.forEach()
&lt;/h3&gt;

&lt;p&gt;This function allows you to iterate over an array without returning an altered version of the array i.e. basically it doesn't return the new array.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const array = [1, 2, 3];
array.forEach((item) =&amp;gt; {
 return item * 2; // effectively, this operation does nothing
});

console.log(array);  //output: [1,2,3]

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

&lt;/div&gt;


&lt;p&gt;As you can see from above, both functions return a different output, at all one returns a new array by completely manipulating the original array while forEach only iterates over an array without manipulating any items from the array.&lt;/p&gt;

&lt;p&gt;From the above we get the two basic differences between this two functions as mention below.&lt;/p&gt;
&lt;h3&gt;
  
  
  Return value of Both Functions :
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;map() returns a new array containing the results of applying the provided function to each element in the original array.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;forEach() does not return anything (or returns undefined); it simply iterates over the array elements and applies the provided function.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  Use Case of Both Functions:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Use map() when you want to transform each element of an array and create a new array based on the transformation.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use forEach() when you want to iterate over each element of an array without creating a new array.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  Performance comparison of Array.forEach() and Array.map() :
&lt;/h2&gt;

&lt;p&gt;Now, that we have seen and understood the basic difference between the two functions let's now Compare how the performance of these two functions looks like, Do you want to know what the performance impacts are for these two functions?&lt;/p&gt;

&lt;p&gt;A quick comparison on &lt;a href="//jsben.ch"&gt;jsben.ch&lt;/a&gt; suggests that forEach is faster when iterating over an array that contains 1000000 numeric values.&lt;/p&gt;

&lt;p&gt;For te BenchMark Comparision we will take this code a benchmark for testing the performance.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// map benchmark code (code block 1)
array.map(item =&amp;gt; item * 2);
// forEach benchmark code (code block 2)
array.forEach(item =&amp;gt; item * 2);

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

&lt;/div&gt;


&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--OhhpCDGI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tg1htpf42z579dbdcxn2.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OhhpCDGI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tg1htpf42z579dbdcxn2.jpg" alt="peformance" width="657" height="319"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;From the above comparison of both the codes, during the first testing, we see that forEach() outperforms the map()&lt;/p&gt;

&lt;p&gt;However, if we want to keep the comparison fair, we'll have to get the same result for both benchmarks.&lt;/p&gt;

&lt;p&gt;Remember, map returns an altered version of the original array. In this case, it multiplies each value with 2.&lt;/p&gt;

&lt;p&gt;To achieve the same with the forEach function, we'd have to create a new array to which we push each result:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// map benchmark code
array.map(item =&amp;gt; item * 2);

// forEach benchmark code
const result = [];
array.forEach(item =&amp;gt; result.push(item * 2));

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

&lt;/div&gt;


&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--EHR2Cy_J--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n1txu8eg6jvn9y270ayj.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--EHR2Cy_J--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n1txu8eg6jvn9y270ayj.jpg" alt="Image description" width="682" height="257"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;From the above comparison, of the two functions by changing the benchmark code we see a clear difference in performance as map() out-performs forEach() in this fair benchmark comparison.&lt;/p&gt;
&lt;h2&gt;
  
  
  Which one should you use?
&lt;/h2&gt;

&lt;p&gt;From the above, I have already mention depending upon the use Cases which function would be suitable for the usage by keeping in mind the performance factor also.&lt;/p&gt;

&lt;p&gt;Having the performance benchmarks in mind, you should use the function that suits your use case the best.&lt;/p&gt;

&lt;p&gt;Do you want to change each value of the array and also want the newly created array as a return value? Use map.&lt;/p&gt;

&lt;p&gt;Do you want to do other operations for each of the values of a given array by simply iterating over the given array? Use forEach.&lt;/p&gt;

&lt;p&gt;It doesn't make sense to use map if you don't want to change the array, and forEach if you want to change each value. As we saw, performance will suffer and the readability of your code as well.&lt;/p&gt;
&lt;h2&gt;
  
  
  Conclusion:
&lt;/h2&gt;

&lt;p&gt;From this article, you should now have a clear understanding of what is the main difference between the two important array functions i.e. forEach() and map() and you also have a clear understanding of which function to use depending upon the use Cases and the performance factor of the two functions.&lt;/p&gt;

&lt;p&gt;Further to summarize this article, the map() function returns a new array while forEach() does not if you want to perform an operation on an array and return the modified array, you should use map(). If you simply want to loop over the array and do something with its elements, use forEach().&lt;/p&gt;

&lt;p&gt;I have learned a lot about this topic from the internet and also advise you the same for more exploring and deep diving, do reply and comment on this article for better sharing and good content creation.&lt;/p&gt;
&lt;h2&gt;
  
  
  References:
&lt;/h2&gt;


&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
      &lt;div class="c-embed__cover"&gt;
        &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map" class="c-link s:max-w-50 align-middle" rel="noopener noreferrer"&gt;
          &lt;img alt="" src="https://res.cloudinary.com/practicaldev/image/fetch/s--UHjFdr30--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://developer.mozilla.org/mdn-social-share.cd6c4a5a.png" height="450" class="m-0" width="800"&gt;
        &lt;/a&gt;
      &lt;/div&gt;
    &lt;div class="c-embed__body"&gt;
      &lt;h2 class="fs-xl lh-tight"&gt;
        &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map" rel="noopener noreferrer" class="c-link"&gt;
          Array.prototype.map() - JavaScript | MDN
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;p class="truncate-at-3"&gt;
          The map() method of Array instances creates
  a new array populated with the results of calling a provided function on
  every element in the calling array.
        &lt;/p&gt;
      &lt;div class="color-secondary fs-s flex items-center"&gt;
          &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://res.cloudinary.com/practicaldev/image/fetch/s--_bI64N_S--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://developer.mozilla.org/favicon-48x48.cbbd161b.png" width="48" height="48"&gt;
        developer.mozilla.org
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;




&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
      &lt;div class="c-embed__cover"&gt;
        &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach" class="c-link s:max-w-50 align-middle" rel="noopener noreferrer"&gt;
          &lt;img alt="" src="https://res.cloudinary.com/practicaldev/image/fetch/s--UHjFdr30--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://developer.mozilla.org/mdn-social-share.cd6c4a5a.png" height="450" class="m-0" width="800"&gt;
        &lt;/a&gt;
      &lt;/div&gt;
    &lt;div class="c-embed__body"&gt;
      &lt;h2 class="fs-xl lh-tight"&gt;
        &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach" rel="noopener noreferrer" class="c-link"&gt;
          Array.prototype.forEach() - JavaScript | MDN
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;p class="truncate-at-3"&gt;
          The forEach() method of Array instances executes a provided function once
  for each array element.
        &lt;/p&gt;
      &lt;div class="color-secondary fs-s flex items-center"&gt;
          &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://res.cloudinary.com/practicaldev/image/fetch/s--_bI64N_S--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://developer.mozilla.org/favicon-48x48.cbbd161b.png" width="48" height="48"&gt;
        developer.mozilla.org
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;


</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
