<section>
 <h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
  Aggregation Pipeline
  <a class="headerlink leafygreen-ui-1kepx47" href="#aggregation-pipeline" title="Permalink to this heading">
   <svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
    <path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
    </path>
    <path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
    </path>
   </svg>
   <div class="css-1btu0w3 evvkmar0" id="aggregation-pipeline">
   </div>
  </a>
 </h1>
 <div class="leafygreen-ui-a5yxub">
  <p class="css-1cmx2d4 ef7gu8j0">
   On this page
  </p>
  <ul class="css-1qb0mst ef7gu8j1">
   <li class="leafygreen-ui-1e7a0ei">
    <a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#complete-aggregation-pipeline-examples" target="_self">
     <span>
      Complete Aggregation Pipeline Examples
     </span>
    </a>
   </li>
   <li class="leafygreen-ui-1jqtqlb">
    <a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#additional-aggregation-pipeline-stage-details" target="_self">
     <span>
      Additional Aggregation Pipeline Stage Details
     </span>
    </a>
   </li>
   <li class="leafygreen-ui-1jqtqlb">
    <a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#run-an-aggregation-pipeline" target="_self">
     <span>
      Run an Aggregation Pipeline
     </span>
    </a>
   </li>
   <li class="leafygreen-ui-1jqtqlb">
    <a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#update-documents-using-an-aggregation-pipeline" target="_self">
     <span>
      Update Documents Using an Aggregation Pipeline
     </span>
    </a>
   </li>
   <li class="leafygreen-ui-1jqtqlb">
    <a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#other-considerations" target="_self">
     <span>
      Other Considerations
     </span>
    </a>
   </li>
   <li class="leafygreen-ui-1jqtqlb">
    <a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#learn-more" target="_self">
     <span>
      Learn More
     </span>
    </a>
   </li>
  </ul>
 </div>
 <p class="leafygreen-ui-1kp3ins">
  An aggregation pipeline consists of one or more
  <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation-pipeline/#std-label-aggregation-pipeline-operator-reference">
   stages
  </a>
  that process documents:
 </p>
 <ul class="leafygreen-ui-rioki0">
  <li class="leafygreen-ui-d7p7b6">
   <p class="leafygreen-ui-1kp3ins">
    Each stage performs an operation on the input documents.
For example, a stage can filter documents, group documents, and
calculate values.
   </p>
  </li>
  <li class="leafygreen-ui-d7p7b6">
   <p class="leafygreen-ui-1kp3ins">
    The documents that are output from a stage are passed to the next
stage.
   </p>
  </li>
  <li class="leafygreen-ui-d7p7b6">
   <p class="leafygreen-ui-1kp3ins">
    An aggregation pipeline can return results for groups of documents.
For example, return the total, average, maximum, and minimum values.
   </p>
  </li>
 </ul>
 <p class="leafygreen-ui-1kp3ins">
  You can update documents with an aggregation pipeline if you use the stages
shown in
  <a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/update-documents-with-aggregation-pipeline/#std-label-updates-agg-pipeline">
   Updates with Aggregation Pipeline.
  </a>
 </p>
 <div class="leafygreen-ui-15mdk4" role="note">
  <h2 class="leafygreen-ui-6n7b7d">
   Note
  </h2>
  <div class="leafygreen-ui-9mycqc">
   <p class="leafygreen-ui-1kp3ins">
    Aggregation pipelines run with the
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.aggregate/#mongodb-method-db.collection.aggregate">
     <code class="leafygreen-ui-1l06pbn">
      db.collection.aggregate()
     </code>
    </a>
    method do not modify documents in
a collection, unless the pipeline contains a
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/merge/#mongodb-pipeline-pipe.-merge">
     <code class="leafygreen-ui-1l06pbn">
      $merge
     </code>
    </a>
    or
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/out/#mongodb-pipeline-pipe.-out">
     <code class="leafygreen-ui-1l06pbn">
      $out
     </code>
    </a>
    stage.
   </p>
  </div>
 </div>
 <a class="css-53dzkk" href="https://www.mongodb.com/docs/atlas/atlas-ui/agg-pipeline">
  <div class="css-z0l49d">
   <div class="css-fw7ipj">
    <svg aria-label="Cloud Icon" class="leafygreen-ui-1t2jgtk" fill="none" height="16" role="img" viewbox="0 0 16 16" width="16" xmlns="http://www.w3.org/2000/svg">
     <path d="M12.571 8.143c0 1.775-.899 3.34-2.267 4.264l-.014.01a5.12 5.12 0 0 1-2.861.869H2.857a2.857 2.857 0 0 1-.545-5.663 5.144 5.144 0 0 1 10.26.52ZM13.821 8.143a6.38 6.38 0 0 1-2.358 4.96 3.429 3.429 0 1 0 2.17-6.506c.123.494.188 1.013.188 1.546Z" fill="currentColor">
     </path>
    </svg>
   </div>
   <p class="leafygreen-ui-1kp3ins">
    You can
    <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/atlas-ui/agg-pipeline/" target="_self">
     <span>
      run aggregation pipelines in the UI
     </span>
    </a>
    for deployments hosted in
    <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas?tck=docs_server" target="_self">
     <span>
      MongoDB Atlas.
     </span>
    </a>
   </p>
  </div>
 </a>
 <p class="leafygreen-ui-1kp3ins">
  When you run aggregation pipelines on MongoDB Atlas deployments in the
MongoDB Atlas UI, you can preview the results at each stage.
 </p>
 <span class="leafygreen-ui-1qnjd6d" id="std-label-aggregation-pipeline-examples">
 </span>
 <section>
  <h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
   Complete Aggregation Pipeline Examples
   <a class="headerlink leafygreen-ui-1kepx47" href="#complete-aggregation-pipeline-examples" title="Permalink to this heading">
    <svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
     <path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
     </path>
     <path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
     </path>
    </svg>
    <div class="css-1btu0w3 evvkmar0" id="complete-aggregation-pipeline-examples">
    </div>
   </a>
  </h2>
  <p class="leafygreen-ui-1kp3ins">
   This section shows aggregation pipeline examples that use the following
pizza
   <code class="leafygreen-ui-1l06pbn">
    orders
   </code>
   collection:
  </p>
  <script class="structured_data" type="application/ld+json">
   {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.orders.insertMany( [\n   { _id: 0, name: \"Pepperoni\", size: \"small\", price: 19,\n     quantity: 10, date: ISODate( \"2021-03-13T08:14:30Z\" ) },\n   { _id: 1, name: \"Pepperoni\", size: \"medium\", price: 20,\n     quantity: 20, date : ISODate( \"2021-03-13T09:13:24Z\" ) },\n   { _id: 2, name: \"Pepperoni\", size: \"large\", price: 21,\n     quantity: 30, date : ISODate( \"2021-03-17T09:22:12Z\" ) },\n   { _id: 3, name: \"Cheese\", size: \"small\", price: 12,\n     quantity: 15, date : ISODate( \"2021-03-13T11:21:39.736Z\" ) },\n   { _id: 4, name: \"Cheese\", size: \"medium\", price: 13,\n     quantity:50, date : ISODate( \"2022-01-12T21:23:13.331Z\" ) },\n   { _id: 5, name: \"Cheese\", size: \"large\", price: 14,\n     quantity: 10, date : ISODate( \"2022-01-12T05:08:13Z\" ) },\n   { _id: 6, name: \"Vegan\", size: \"small\", price: 17,\n     quantity: 10, date : ISODate( \"2021-01-13T05:08:13Z\" ) },\n   { _id: 7, name: \"Vegan\", size: \"medium\", price: 18,\n     quantity: 10, date : ISODate( \"2021-01-13T05:10:13Z\" ) }\n] )","programmingLanguage":"JavaScript"}
  </script>
  <div class="css-16lmvu9">
   <div class="leafygreen-ui-196mwvg">
    <div class="leafygreen-ui-1b1bvay">
     <pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">orders</span>.<span class="lg-highlight-title lg-highlight-function">insertMany</span>( [</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   { <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">0</span>, <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"Pepperoni"</span>, <span class="lg-highlight-attr">size</span>: <span class="lg-highlight-string">"small"</span>, <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">19</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-number">10</span>, <span class="lg-highlight-attr">date</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>( <span class="lg-highlight-string">"2021-03-13T08:14:30Z"</span> ) },</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   { <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"Pepperoni"</span>, <span class="lg-highlight-attr">size</span>: <span class="lg-highlight-string">"medium"</span>, <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">20</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-number">20</span>, date : <span class="lg-highlight-title lg-highlight-class">ISODate</span>( <span class="lg-highlight-string">"2021-03-13T09:13:24Z"</span> ) },</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   { <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"Pepperoni"</span>, <span class="lg-highlight-attr">size</span>: <span class="lg-highlight-string">"large"</span>, <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">21</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-number">30</span>, date : <span class="lg-highlight-title lg-highlight-class">ISODate</span>( <span class="lg-highlight-string">"2021-03-17T09:22:12Z"</span> ) },</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   { <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"Cheese"</span>, <span class="lg-highlight-attr">size</span>: <span class="lg-highlight-string">"small"</span>, <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">12</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-number">15</span>, date : <span class="lg-highlight-title lg-highlight-class">ISODate</span>( <span class="lg-highlight-string">"2021-03-13T11:21:39.736Z"</span> ) },</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   { <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">4</span>, <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"Cheese"</span>, <span class="lg-highlight-attr">size</span>: <span class="lg-highlight-string">"medium"</span>, <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">13</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-attr">quantity</span>:<span class="lg-highlight-number">50</span>, date : <span class="lg-highlight-title lg-highlight-class">ISODate</span>( <span class="lg-highlight-string">"2022-01-12T21:23:13.331Z"</span> ) },</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   { <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">5</span>, <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"Cheese"</span>, <span class="lg-highlight-attr">size</span>: <span class="lg-highlight-string">"large"</span>, <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">14</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-number">10</span>, date : <span class="lg-highlight-title lg-highlight-class">ISODate</span>( <span class="lg-highlight-string">"2022-01-12T05:08:13Z"</span> ) },</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   { <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">6</span>, <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"Vegan"</span>, <span class="lg-highlight-attr">size</span>: <span class="lg-highlight-string">"small"</span>, <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">17</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-number">10</span>, date : <span class="lg-highlight-title lg-highlight-class">ISODate</span>( <span class="lg-highlight-string">"2021-01-13T05:08:13Z"</span> ) },</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   { <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">7</span>, <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"Vegan"</span>, <span class="lg-highlight-attr">size</span>: <span class="lg-highlight-string">"medium"</span>, <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">18</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-number">10</span>, date : <span class="lg-highlight-title lg-highlight-class">ISODate</span>( <span class="lg-highlight-string">"2021-01-13T05:10:13Z"</span> ) }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">] )</td></tr></tbody></table></code></pre>
     <div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
      <button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
       <div class="leafygreen-ui-xhlipt">
        <svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
         <path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
         </path>
        </svg>
       </div>
      </button>
     </div>
    </div>
   </div>
  </div>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    Calculate Total Order Quantity
    <a class="headerlink leafygreen-ui-1kepx47" href="#calculate-total-order-quantity" title="Permalink to this heading">
     <svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
      <path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
      </path>
      <path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
      </path>
     </svg>
     <div class="css-1btu0w3 evvkmar0" id="calculate-total-order-quantity">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    The following aggregation pipeline example contains two
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation-pipeline/#std-label-aggregation-pipeline-operator-reference">
     stages
    </a>
    and returns the total
order quantity of medium size pizzas grouped by pizza name:
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.orders.aggregate( [\n\n   // Stage 1: Filter pizza order documents by pizza size\n   {\n      $match: { size: \"medium\" }\n   },\n\n   // Stage 2: Group remaining documents by pizza name and calculate total quantity\n   {\n      $group: { _id: \"$name\", totalQuantity: { $sum: \"$quantity\" } }\n   }\n\n] )","programmingLanguage":"JavaScript"}
   </script>
   <div class="css-16lmvu9">
    <div class="leafygreen-ui-196mwvg">
     <div class="leafygreen-ui-1b1bvay">
      <pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">orders</span>.<span class="lg-highlight-title lg-highlight-function">aggregate</span>( [</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <span class="lg-highlight-comment">// Stage 1:</span><span class="lg-highlight-comment"> Filter pizza order </span><span class="lg-highlight-comment">documents by pizza size</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">   {</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <span class="lg-highlight-attr">$match</span>: { <span class="lg-highlight-attr">size</span>: <span class="lg-highlight-string">"medium"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   },</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <span class="lg-highlight-comment">// Stage 2:</span><span class="lg-highlight-comment"> Group remaining documents </span><span class="lg-highlight-comment">by</span><span class="lg-highlight-comment"> pizza name and </span><span class="lg-highlight-comment">calculate total quantity</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">   {</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <span class="lg-highlight-attr">$group</span>: { <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-string">"$name"</span>, <span class="lg-highlight-attr">totalQuantity</span>: { <span class="lg-highlight-attr">$sum</span>: <span class="lg-highlight-string">"$quantity"</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx">] )</td></tr></tbody></table></code></pre>
      <div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
       <button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
        <div class="leafygreen-ui-xhlipt">
         <svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
          <path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
          </path>
         </svg>
        </div>
       </button>
      </div>
     </div>
    </div>
   </div>
   <p class="leafygreen-ui-1kp3ins">
    The
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/match/#mongodb-pipeline-pipe.-match">
     <code class="leafygreen-ui-1l06pbn">
      $match
     </code>
    </a>
    stage:
   </p>
   <ul class="leafygreen-ui-rioki0">
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      Filters the pizza order documents to pizzas with a
      <code class="leafygreen-ui-1l06pbn">
       size
      </code>
      of
      <code class="leafygreen-ui-1l06pbn">
       medium
      </code>
      .
     </p>
    </li>
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      Passes the remaining documents to the
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/group/#mongodb-pipeline-pipe.-group">
       <code class="leafygreen-ui-1l06pbn">
        $group
       </code>
      </a>
      stage.
     </p>
    </li>
   </ul>
   <p class="leafygreen-ui-1kp3ins">
    The
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/group/#mongodb-pipeline-pipe.-group">
     <code class="leafygreen-ui-1l06pbn">
      $group
     </code>
    </a>
    stage:
   </p>
   <ul class="leafygreen-ui-rioki0">
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      Groups the remaining documents by pizza
      <code class="leafygreen-ui-1l06pbn">
       name
      </code>
      .
     </p>
    </li>
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      Uses
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/sum/#mongodb-group-grp.-sum">
       <code class="leafygreen-ui-1l06pbn">
        $sum
       </code>
      </a>
      to calculate the total order
      <code class="leafygreen-ui-1l06pbn">
       quantity
      </code>
      for each
pizza
      <code class="leafygreen-ui-1l06pbn">
       name
      </code>
      . The total is stored in the
      <code class="leafygreen-ui-1l06pbn">
       totalQuantity
      </code>
      field
returned by the aggregation pipeline.
     </p>
    </li>
   </ul>
   <p class="leafygreen-ui-1kp3ins">
    Example output:
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"[\n   { _id: 'Cheese', totalQuantity: 50 },\n   { _id: 'Vegan', totalQuantity: 10 },\n   { _id: 'Pepperoni', totalQuantity: 20 }\n]","programmingLanguage":"JavaScript"}
   </script>
   <div class="css-1lju8ps">
    <div class="leafygreen-ui-196mwvg">
     <div class="leafygreen-ui-1jheeh3">
      <pre class="leafygreen-ui-1453az5" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">[</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   { <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-string">'Cheese'</span>, <span class="lg-highlight-attr">totalQuantity</span>: <span class="lg-highlight-number">50</span> },</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   { <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-string">'Vegan'</span>, <span class="lg-highlight-attr">totalQuantity</span>: <span class="lg-highlight-number">10</span> },</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   { <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-string">'Pepperoni'</span>, <span class="lg-highlight-attr">totalQuantity</span>: <span class="lg-highlight-number">20</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">]</td></tr></tbody></table></code></pre>
     </div>
    </div>
   </div>
  </section>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    Calculate Total Order Value and Average Order Quantity
    <a class="headerlink leafygreen-ui-1kepx47" href="#calculate-total-order-value-and-average-order-quantity" title="Permalink to this heading">
     <svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
      <path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
      </path>
      <path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
      </path>
     </svg>
     <div class="css-1btu0w3 evvkmar0" id="calculate-total-order-value-and-average-order-quantity">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    The following example calculates the total pizza order value and
average order quantity between two dates:
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.orders.aggregate( [\n\n   // Stage 1: Filter pizza order documents by date range\n   {\n      $match:\n      {\n         \"date\": { $gte: new ISODate( \"2020-01-30\" ), $lt: new ISODate( \"2022-01-30\" ) }\n      }\n   },\n\n   // Stage 2: Group remaining documents by date and calculate results\n   {\n      $group:\n      {\n         _id: { $dateToString: { format: \"%Y-%m-%d\", date: \"$date\" } },\n         totalOrderValue: { $sum: { $multiply: [ \"$price\", \"$quantity\" ] } },\n         averageOrderQuantity: { $avg: \"$quantity\" }\n      }\n   },\n\n   // Stage 3: Sort documents by totalOrderValue in descending order\n   {\n      $sort: { totalOrderValue: -1 }\n   }\n\n ] )","programmingLanguage":"JavaScript"}
   </script>
   <div class="css-16lmvu9">
    <div class="leafygreen-ui-196mwvg">
     <div class="leafygreen-ui-1b1bvay">
      <pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">orders</span>.<span class="lg-highlight-title lg-highlight-function">aggregate</span>( [</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <span class="lg-highlight-comment">// Stage 1:</span><span class="lg-highlight-comment"> Filter pizza order </span><span class="lg-highlight-comment">documents by date range</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">   {</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <span class="lg-highlight-attr">$match</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      {</td></tr><tr class=""><td class="leafygreen-ui-7razhx">         <span class="lg-highlight-string">"date"</span>: { <span class="lg-highlight-attr">$gte</span>: <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">ISODate</span>( <span class="lg-highlight-string">"2020-01-30"</span> ), <span class="lg-highlight-attr">$lt</span>: <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">ISODate</span>( <span class="lg-highlight-string">"2022-01-30"</span> ) }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   },</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <span class="lg-highlight-comment">// Stage 2:</span><span class="lg-highlight-comment"> Group remaining documents </span><span class="lg-highlight-comment">by</span><span class="lg-highlight-comment"> date and calculate </span><span class="lg-highlight-comment">results</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">   {</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <span class="lg-highlight-attr">$group</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      {</td></tr><tr class=""><td class="leafygreen-ui-7razhx">         <span class="lg-highlight-attr">_id</span>: { <span class="lg-highlight-attr">$dateToString</span>: { <span class="lg-highlight-attr">format</span>: <span class="lg-highlight-string">"%Y-%m-%d"</span>, <span class="lg-highlight-attr">date</span>: <span class="lg-highlight-string">"$date"</span> } },</td></tr><tr class=""><td class="leafygreen-ui-7razhx">         <span class="lg-highlight-attr">totalOrderValue</span>: { <span class="lg-highlight-attr">$sum</span>: { <span class="lg-highlight-attr">$multiply</span>: [ <span class="lg-highlight-string">"$price"</span>, <span class="lg-highlight-string">"$quantity"</span> ] } },</td></tr><tr class=""><td class="leafygreen-ui-7razhx">         <span class="lg-highlight-attr">averageOrderQuantity</span>: { <span class="lg-highlight-attr">$avg</span>: <span class="lg-highlight-string">"$quantity"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   },</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <span class="lg-highlight-comment">// Stage 3: Sort documents by totalOrderValue in descending order</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">   {</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <span class="lg-highlight-attr">$sort</span>: { <span class="lg-highlight-attr">totalOrderValue</span>: -<span class="lg-highlight-number">1</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ] )</td></tr></tbody></table></code></pre>
      <div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
       <button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
        <div class="leafygreen-ui-xhlipt">
         <svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
          <path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
          </path>
         </svg>
        </div>
       </button>
      </div>
     </div>
    </div>
   </div>
   <p class="leafygreen-ui-1kp3ins">
    The
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/match/#mongodb-pipeline-pipe.-match">
     <code class="leafygreen-ui-1l06pbn">
      $match
     </code>
    </a>
    stage:
   </p>
   <ul class="leafygreen-ui-rioki0">
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      Filters the pizza order documents to those in a date range specified
using
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/gte/#mongodb-expression-exp.-gte">
       <code class="leafygreen-ui-1l06pbn">
        $gte
       </code>
      </a>
      and
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/lt/#mongodb-expression-exp.-lt">
       <code class="leafygreen-ui-1l06pbn">
        $lt
       </code>
       .
      </a>
     </p>
    </li>
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      Passes the remaining documents to the
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/group/#mongodb-pipeline-pipe.-group">
       <code class="leafygreen-ui-1l06pbn">
        $group
       </code>
      </a>
      stage.
     </p>
    </li>
   </ul>
   <p class="leafygreen-ui-1kp3ins">
    The
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/group/#mongodb-pipeline-pipe.-group">
     <code class="leafygreen-ui-1l06pbn">
      $group
     </code>
    </a>
    stage:
   </p>
   <ul class="leafygreen-ui-rioki0">
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      Groups the documents by date using
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/dateToString/#mongodb-expression-exp.-dateToString">
       <code class="leafygreen-ui-1l06pbn">
        $dateToString
       </code>
       .
      </a>
     </p>
    </li>
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      For each group, calculates:
     </p>
     <ul class="leafygreen-ui-rioki0">
      <li class="leafygreen-ui-d7p7b6">
       <p class="leafygreen-ui-1kp3ins">
        Total order value using
        <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/sum/#mongodb-group-grp.-sum">
         <code class="leafygreen-ui-1l06pbn">
          $sum
         </code>
        </a>
        and
        <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/multiply/#mongodb-expression-exp.-multiply">
         <code class="leafygreen-ui-1l06pbn">
          $multiply
         </code>
         .
        </a>
       </p>
      </li>
      <li class="leafygreen-ui-d7p7b6">
       <p class="leafygreen-ui-1kp3ins">
        Average order quantity using
        <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/avg/#mongodb-group-grp.-avg">
         <code class="leafygreen-ui-1l06pbn">
          $avg
         </code>
         .
        </a>
       </p>
      </li>
     </ul>
    </li>
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      Passes the grouped documents to the
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/sort/#mongodb-pipeline-pipe.-sort">
       <code class="leafygreen-ui-1l06pbn">
        $sort
       </code>
      </a>
      stage.
     </p>
    </li>
   </ul>
   <p class="leafygreen-ui-1kp3ins">
    The
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/sort/#mongodb-pipeline-pipe.-sort">
     <code class="leafygreen-ui-1l06pbn">
      $sort
     </code>
    </a>
    stage:
   </p>
   <ul class="leafygreen-ui-rioki0">
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      Sorts the documents by the total order value for each group in
descending order (
      <code class="leafygreen-ui-1l06pbn">
       -1
      </code>
      ).
     </p>
    </li>
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      Returns the sorted documents.
     </p>
    </li>
   </ul>
   <p class="leafygreen-ui-1kp3ins">
    Example output:
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"[\n   { _id: '2022-01-12', totalOrderValue: 790, averageOrderQuantity: 30 },\n   { _id: '2021-03-13', totalOrderValue: 770, averageOrderQuantity: 15 },\n   { _id: '2021-03-17', totalOrderValue: 630, averageOrderQuantity: 30 },\n   { _id: '2021-01-13', totalOrderValue: 350, averageOrderQuantity: 10 }\n]","programmingLanguage":"JavaScript"}
   </script>
   <div class="css-1lju8ps">
    <div class="leafygreen-ui-196mwvg">
     <div class="leafygreen-ui-tyg50d">
      <pre class="leafygreen-ui-1453az5" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">[</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   { <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-string">'2022-01-12'</span>, <span class="lg-highlight-attr">totalOrderValue</span>: <span class="lg-highlight-number">790</span>, <span class="lg-highlight-attr">averageOrderQuantity</span>: <span class="lg-highlight-number">30</span> },</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   { <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-string">'2021-03-13'</span>, <span class="lg-highlight-attr">totalOrderValue</span>: <span class="lg-highlight-number">770</span>, <span class="lg-highlight-attr">averageOrderQuantity</span>: <span class="lg-highlight-number">15</span> },</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   { <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-string">'2021-03-17'</span>, <span class="lg-highlight-attr">totalOrderValue</span>: <span class="lg-highlight-number">630</span>, <span class="lg-highlight-attr">averageOrderQuantity</span>: <span class="lg-highlight-number">30</span> },</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   { <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-string">'2021-01-13'</span>, <span class="lg-highlight-attr">totalOrderValue</span>: <span class="lg-highlight-number">350</span>, <span class="lg-highlight-attr">averageOrderQuantity</span>: <span class="lg-highlight-number">10</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">]</td></tr></tbody></table></code></pre>
     </div>
    </div>
   </div>
   <div class="leafygreen-ui-13vw3ie" role="note">
    <h2 class="leafygreen-ui-1fn4p2b">
     Tip
    </h2>
    <h3 class="leafygreen-ui-144d5kf">
     <strong>
      See also:
     </strong>
    </h3>
    <div class="leafygreen-ui-9mycqc">
     <ul class="leafygreen-ui-rioki0">
      <li class="leafygreen-ui-d7p7b6">
       <p class="leafygreen-ui-1kp3ins">
        <a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/aggregation-with-user-preference-data/">
         Aggregation with User Preference Data
        </a>
       </p>
      </li>
      <li class="leafygreen-ui-d7p7b6">
       <p class="leafygreen-ui-1kp3ins">
        <a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/aggregation-zip-code-data-set/">
         Aggregation with the Zip Code Data Set
        </a>
       </p>
      </li>
      <li class="leafygreen-ui-d7p7b6">
       <p class="leafygreen-ui-1kp3ins">
        <a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/update-documents-with-aggregation-pipeline/">
         Updates with Aggregation Pipeline
        </a>
       </p>
      </li>
     </ul>
    </div>
   </div>
   <span class="leafygreen-ui-1qnjd6d" id="std-label-aggregation-pipeline-stages-details">
   </span>
  </section>
 </section>
 <section>
  <h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
   Additional Aggregation Pipeline Stage Details
   <a class="headerlink leafygreen-ui-1kepx47" href="#additional-aggregation-pipeline-stage-details" title="Permalink to this heading">
    <svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
     <path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
     </path>
     <path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
     </path>
    </svg>
    <div class="css-1btu0w3 evvkmar0" id="additional-aggregation-pipeline-stage-details">
    </div>
   </a>
  </h2>
  <p class="leafygreen-ui-1kp3ins">
   An aggregation pipeline consists of one or more
   <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation-pipeline/#std-label-aggregation-pipeline-operator-reference">
    stages
   </a>
   that process documents:
  </p>
  <ul class="leafygreen-ui-rioki0">
   <li class="leafygreen-ui-d7p7b6">
    <p class="leafygreen-ui-1kp3ins">
     A stage does not have to output one document for every input
document. For example, some stages may produce new documents or
filter out documents.
    </p>
   </li>
   <li class="leafygreen-ui-d7p7b6">
    <p class="leafygreen-ui-1kp3ins">
     The same stage can appear multiple times in the pipeline with these
stage exceptions:
     <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/out/#mongodb-pipeline-pipe.-out">
      <code class="leafygreen-ui-1l06pbn">
       $out
      </code>
     </a>
     ,
     <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/merge/#mongodb-pipeline-pipe.-merge">
      <code class="leafygreen-ui-1l06pbn">
       $merge
      </code>
     </a>
     , and
     <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/geoNear/#mongodb-pipeline-pipe.-geoNear">
      <code class="leafygreen-ui-1l06pbn">
       $geoNear
      </code>
      .
     </a>
    </p>
   </li>
   <li class="leafygreen-ui-d7p7b6">
    <p class="leafygreen-ui-1kp3ins">
     To calculate averages and perform other calculations in a stage, use
     <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-aggregation-expressions">
      aggregation expressions
     </a>
     that specify
     <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-aggregation-expression-operators">
      aggregation operators
     </a>
     . You
will learn more about aggregation expressions in the next section.
    </p>
   </li>
  </ul>
  <p class="leafygreen-ui-1kp3ins">
   For all aggregation stages, see
   <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation-pipeline/#std-label-aggregation-pipeline-operator-reference">
    Aggregation Stages.
   </a>
  </p>
  <span class="leafygreen-ui-1qnjd6d" id="std-label-aggregation-pipeline-expressions">
  </span>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    Aggregation Expressions and Operators
    <a class="headerlink leafygreen-ui-1kepx47" href="#aggregation-expressions-and-operators" title="Permalink to this heading">
     <svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
      <path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
      </path>
      <path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
      </path>
     </svg>
     <div class="css-1btu0w3 evvkmar0" id="aggregation-expressions-and-operators">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    Some aggregation pipeline stages accept
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-expression">
     expressions
    </a>
    . Operators calculate values based on input expressions.
   </p>
   <p class="leafygreen-ui-1kp3ins">
    In the MongoDB Query Language, you can build expressions from the
following components:
   </p>
   <div class="leafygreen-ui-skkc39" tabindex="0">
    <table class="leafygreen-ui-hr8lsv">
     <colgroup>
      <col style="width: 10%;"/>
      <col style="width: 10%;"/>
     </colgroup>
     <thead class="leafygreen-ui-a9ho40">
      <tr data-testid="leafygreen-ui-header-row">
       <th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
        <div class="leafygreen-ui-ui0te9">
         <div>
          Component
         </div>
        </div>
       </th>
       <th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
        <div class="leafygreen-ui-ui0te9">
         <div>
          Example
         </div>
        </div>
       </th>
      </tr>
     </thead>
     <tbody>
      <tr class="">
       <td class="leafygreen-ui-1drjk7h">
        <div class="leafygreen-ui-1n4j8xf">
         <div>
          Constants
         </div>
        </div>
       </td>
       <td class="leafygreen-ui-1drjk7h">
        <div class="leafygreen-ui-1n4j8xf">
         <div>
          <code class="leafygreen-ui-1l06pbn">
           3
          </code>
         </div>
        </div>
       </td>
      </tr>
      <tr class="">
       <td class="leafygreen-ui-1drjk7h">
        <div class="leafygreen-ui-1n4j8xf">
         <div>
          Operators
         </div>
        </div>
       </td>
       <td class="leafygreen-ui-1drjk7h">
        <div class="leafygreen-ui-1n4j8xf">
         <div>
          <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/add/#mongodb-expression-exp.-add">
           <code class="leafygreen-ui-1l06pbn">
            $add
           </code>
          </a>
         </div>
        </div>
       </td>
      </tr>
      <tr class="">
       <td class="leafygreen-ui-1drjk7h">
        <div class="leafygreen-ui-1n4j8xf">
         <div>
          Field path expressions
         </div>
        </div>
       </td>
       <td class="leafygreen-ui-1drjk7h">
        <div class="leafygreen-ui-1n4j8xf">
         <div>
          <code class="leafygreen-ui-1l06pbn">
           "$&lt;path.to.field&gt;"
          </code>
         </div>
        </div>
       </td>
      </tr>
     </tbody>
    </table>
   </div>
   <p class="leafygreen-ui-1kp3ins">
    For example,
    <code class="leafygreen-ui-1l06pbn">
     { $add: [ 3, "$inventory.total" ] }
    </code>
    is an expression
consisting of the
    <code class="leafygreen-ui-1l06pbn">
     $add
    </code>
    operator and two input expressions:
   </p>
   <ul class="leafygreen-ui-rioki0">
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      The constant
      <code class="leafygreen-ui-1l06pbn">
       3
      </code>
     </p>
    </li>
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      The
      <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-agg-quick-ref-field-paths" target="_self">
       <span>
        field path expression
       </span>
      </a>
      <code class="leafygreen-ui-1l06pbn">
       "$inventory.total"
      </code>
     </p>
    </li>
   </ul>
   <p class="leafygreen-ui-1kp3ins">
    The expression returns the result of adding 3 to the value at path
    <code class="leafygreen-ui-1l06pbn">
     inventory.total
    </code>
    of the input document.
   </p>
   <span class="leafygreen-ui-1qnjd6d" id="std-label-agg-quick-ref-field-paths">
   </span>
  </section>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    Field Paths
    <a class="headerlink leafygreen-ui-1kepx47" href="#field-paths" title="Permalink to this heading">
     <svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
      <path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
      </path>
      <path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
      </path>
     </svg>
     <div class="css-1btu0w3 evvkmar0" id="field-paths">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-field-path">
     Field path
    </a>
    expressions are used to access fields in
input documents. To specify a field path, prefix the field name or the
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/document/#std-label-document-dot-notation">
     dotted field name
    </a>
    (if the field is in an
embedded document) with a dollar sign
    <code class="leafygreen-ui-1l06pbn">
     $
    </code>
    . For example,
    <code class="leafygreen-ui-1l06pbn">
     "$user"
    </code>
    to
specify the field path for the
    <code class="leafygreen-ui-1l06pbn">
     user
    </code>
    field or
    <code class="leafygreen-ui-1l06pbn">
     "$user.name"
    </code>
    to
specify the field path to the embedded
    <code class="leafygreen-ui-1l06pbn">
     "user.name"
    </code>
    field.
   </p>
   <p class="leafygreen-ui-1kp3ins">
    <code class="leafygreen-ui-1l06pbn">
     "$&lt;field&gt;"
    </code>
    is equivalent to
    <code class="leafygreen-ui-1l06pbn">
     "$$CURRENT.&lt;field&gt;"
    </code>
    where the
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/aggregation-variables/#mongodb-variable-variable.CURRENT">
     <code class="leafygreen-ui-1l06pbn">
      CURRENT
     </code>
    </a>
    is a system variable that defaults to the root of
the current object, unless stated otherwise in specific stages.
   </p>
  </section>
 </section>
 <section>
  <h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
   Run an Aggregation Pipeline
   <a class="headerlink leafygreen-ui-1kepx47" href="#run-an-aggregation-pipeline" title="Permalink to this heading">
    <svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
     <path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
     </path>
     <path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
     </path>
    </svg>
    <div class="css-1btu0w3 evvkmar0" id="run-an-aggregation-pipeline">
    </div>
   </a>
  </h2>
  <p class="leafygreen-ui-1kp3ins">
   To run an aggregation pipeline, use:
  </p>
  <ul class="leafygreen-ui-rioki0">
   <li class="leafygreen-ui-d7p7b6">
    <p class="leafygreen-ui-1kp3ins">
     <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.aggregate/#mongodb-method-db.collection.aggregate">
      <code class="leafygreen-ui-1l06pbn">
       db.collection.aggregate()
      </code>
     </a>
     or
    </p>
   </li>
   <li class="leafygreen-ui-d7p7b6">
    <p class="leafygreen-ui-1kp3ins">
     <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/aggregate/#mongodb-dbcommand-dbcmd.aggregate">
      <code class="leafygreen-ui-1l06pbn">
       aggregate
      </code>
     </a>
    </p>
   </li>
  </ul>
  <span class="leafygreen-ui-1qnjd6d" id="std-label-aggregation-update-documents">
  </span>
 </section>
 <section>
  <h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
   Update Documents Using an Aggregation Pipeline
   <a class="headerlink leafygreen-ui-1kepx47" href="#update-documents-using-an-aggregation-pipeline" title="Permalink to this heading">
    <svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
     <path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
     </path>
     <path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
     </path>
    </svg>
    <div class="css-1btu0w3 evvkmar0" id="update-documents-using-an-aggregation-pipeline">
    </div>
   </a>
  </h2>
  <p class="leafygreen-ui-1kp3ins">
   To update documents with an aggregation pipeline, use:
  </p>
  <div class="leafygreen-ui-skkc39" tabindex="0">
   <table class="leafygreen-ui-hr8lsv">
    <thead class="leafygreen-ui-a9ho40">
     <tr data-testid="leafygreen-ui-header-row">
      <th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
       <div class="leafygreen-ui-ui0te9">
        <div>
         Command
        </div>
       </div>
      </th>
      <th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
       <div class="leafygreen-ui-ui0te9">
        <div>
         <code class="leafygreen-ui-1l06pbn">
          mongosh
         </code>
         Methods
        </div>
       </div>
      </th>
     </tr>
    </thead>
    <tbody>
     <tr class="">
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/findAndModify/#mongodb-dbcommand-dbcmd.findAndModify">
          <code class="leafygreen-ui-1l06pbn">
           findAndModify
          </code>
         </a>
        </div>
       </div>
      </td>
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         <div class="line-block">
          <div class="line">
           <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.findOneAndUpdate/#std-label-findOneAndUpdate-agg-pipeline">
            db.collection.findOneAndUpdate()
           </a>
          </div>
          <div class="line">
           <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.findAndModify/#std-label-findAndModify-agg-pipeline">
            db.collection.findAndModify()
           </a>
          </div>
         </div>
        </div>
       </div>
      </td>
     </tr>
     <tr class="">
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/update/#mongodb-dbcommand-dbcmd.update">
          <code class="leafygreen-ui-1l06pbn">
           update
          </code>
         </a>
        </div>
       </div>
      </td>
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         <div class="line-block">
          <div class="line">
           <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.updateOne/#std-label-updateOne-example-agg">
            db.collection.updateOne()
           </a>
          </div>
          <div class="line">
           <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.updateMany/#std-label-updateMany-example-agg">
            db.collection.updateMany()
           </a>
          </div>
         </div>
         <div class="line-block">
          <div class="line">
           <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/Bulk.find.update/#std-label-example-bulk-find-update-agg">
            Bulk.find.update()
           </a>
          </div>
          <div class="line">
           <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/Bulk.find.updateOne/#std-label-example-bulk-find-update-one-agg">
            Bulk.find.updateOne()
           </a>
          </div>
          <div class="line">
           <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/Bulk.find.upsert/#std-label-bulk-find-upsert-update-agg-example">
            Bulk.find.upsert()
           </a>
          </div>
         </div>
        </div>
       </div>
      </td>
     </tr>
    </tbody>
   </table>
  </div>
 </section>
 <section>
  <h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
   Other Considerations
   <a class="headerlink leafygreen-ui-1kepx47" href="#other-considerations" title="Permalink to this heading">
    <svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
     <path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
     </path>
     <path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
     </path>
    </svg>
    <div class="css-1btu0w3 evvkmar0" id="other-considerations">
    </div>
   </a>
  </h2>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    Aggregation Pipeline Limitations
    <a class="headerlink leafygreen-ui-1kepx47" href="#aggregation-pipeline-limitations" title="Permalink to this heading">
     <svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
      <path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
      </path>
      <path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
      </path>
     </svg>
     <div class="css-1btu0w3 evvkmar0" id="aggregation-pipeline-limitations">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    An aggregation pipeline has limitations on the value types and the
result size. See
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/aggregation-pipeline-limits/">
     Aggregation Pipeline Limits.
    </a>
   </p>
  </section>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    Aggregation Pipelines and Sharded Collections
    <a class="headerlink leafygreen-ui-1kepx47" href="#aggregation-pipelines-and-sharded-collections" title="Permalink to this heading">
     <svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
      <path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
      </path>
      <path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
      </path>
     </svg>
     <div class="css-1btu0w3 evvkmar0" id="aggregation-pipelines-and-sharded-collections">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    An aggregation pipeline supports operations on sharded collections.
See
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/aggregation-pipeline-sharded-collections/#std-label-aggregation-pipeline-sharded-collection">
     Aggregation Pipeline and Sharded Collections.
    </a>
   </p>
  </section>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    Aggregation Pipelines as an Alternative to Map-Reduce
    <a class="headerlink leafygreen-ui-1kepx47" href="#aggregation-pipelines-as-an-alternative-to-map-reduce" title="Permalink to this heading">
     <svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
      <path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
      </path>
      <path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
      </path>
     </svg>
     <div class="css-1btu0w3 evvkmar0" id="aggregation-pipelines-as-an-alternative-to-map-reduce">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    Starting in MongoDB 5.0,
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/map-reduce/#std-label-map-reduce">
     map-reduce
    </a>
    is
deprecated:
   </p>
   <ul class="leafygreen-ui-rioki0">
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      Instead of
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/map-reduce/#std-label-map-reduce">
       map-reduce
      </a>
      , you should use an
      <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-aggregation-pipeline" target="_self">
       <span>
        aggregation pipeline
       </span>
      </a>
      . Aggregation
pipelines provide better performance and usability than map-reduce.
     </p>
    </li>
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      You can rewrite map-reduce operations using
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation-pipeline/#std-label-aggregation-pipeline-operator-reference">
       aggregation pipeline
stages
      </a>
      , such as
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/group/#mongodb-pipeline-pipe.-group">
       <code class="leafygreen-ui-1l06pbn">
        $group
       </code>
      </a>
      ,
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/merge/#mongodb-pipeline-pipe.-merge">
       <code class="leafygreen-ui-1l06pbn">
        $merge
       </code>
      </a>
      , and others.
     </p>
    </li>
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      For map-reduce operations that require custom functionality, you can
use the
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/accumulator/#mongodb-group-grp.-accumulator">
       <code class="leafygreen-ui-1l06pbn">
        $accumulator
       </code>
      </a>
      and
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/function/#mongodb-expression-exp.-function">
       <code class="leafygreen-ui-1l06pbn">
        $function
       </code>
      </a>
      aggregation
operators. You can use those
operators to define custom aggregation expressions in JavaScript.
     </p>
    </li>
   </ul>
   <p class="leafygreen-ui-1kp3ins">
    For examples of aggregation pipeline alternatives to map-reduce, see:
   </p>
   <ul class="leafygreen-ui-rioki0">
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/map-reduce-to-aggregation-pipeline/">
       Map-Reduce to Aggregation Pipeline
      </a>
     </p>
    </li>
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/map-reduce-examples/">
       Map-Reduce Examples
      </a>
     </p>
    </li>
   </ul>
  </section>
 </section>
 <section>
  <h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
   Learn More
   <a class="headerlink leafygreen-ui-1kepx47" href="#learn-more" title="Permalink to this heading">
    <svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
     <path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
     </path>
     <path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
     </path>
    </svg>
    <div class="css-1btu0w3 evvkmar0" id="learn-more">
    </div>
   </a>
  </h2>
  <p class="leafygreen-ui-1kp3ins">
   To learn more about aggregation pipelines, see:
  </p>
  <ul class="leafygreen-ui-rioki0">
   <li class="leafygreen-ui-d7p7b6">
    <p class="leafygreen-ui-1kp3ins">
     <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-aggregation-expression-operators">
      Expression Operators
     </a>
    </p>
   </li>
   <li class="leafygreen-ui-d7p7b6">
    <p class="leafygreen-ui-1kp3ins">
     <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation-pipeline/#std-label-aggregation-pipeline-operator-reference">
      Aggregation Stages
     </a>
    </p>
   </li>
   <li class="leafygreen-ui-d7p7b6">
    <p class="leafygreen-ui-1kp3ins">
     <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.practical-mongodb-aggregations.com" rel="noopener noreferrer">
      <span>
       Practical MongoDB Aggregations
      </span>
      <svg alt="" aria-hidden="true" class="leafygreen-ui-1ie5kuq" height="16" role="presentation" viewbox="0 0 16 16" width="16">
       <path d="M13.823 2.4491C13.8201 2.30008 13.6999 2.17994 13.5509 2.17704L9.5062 2.09836C9.25654 2.09351 9.12821 2.39519 9.30482 2.5718L10.3856 3.65257L7.93433 6.10383C7.87964 6.15852 7.83047 6.21665 7.78683 6.27752L5.99909 8.06525C5.46457 8.59977 5.46457 9.4664 5.99909 10.0009C6.53361 10.5354 7.40023 10.5354 7.93475 10.0009L9.72249 8.21317C9.78336 8.16953 9.84148 8.12037 9.89618 8.06567L12.3474 5.61441L13.4282 6.69518C13.6048 6.87179 13.9065 6.74347 13.9016 6.4938L13.823 2.4491Z" fill="currentColor">
       </path>
       <path d="M7.25 3.12893C7.66421 3.12893 8 3.46472 8 3.87893C8 4.29315 7.66421 4.62893 7.25 4.62893H4C3.72386 4.62893 3.5 4.85279 3.5 5.12893V11.9929C3.5 12.2691 3.72386 12.4929 4 12.4929H10.864C11.1401 12.4929 11.364 12.2691 11.364 11.9929V8.75C11.364 8.33579 11.6998 8 12.114 8C12.5282 8 12.864 8.33579 12.864 8.75V11.9929C12.864 13.0975 11.9686 13.9929 10.864 13.9929H4C2.89543 13.9929 2 13.0975 2 11.9929V5.12893C2 4.02436 2.89543 3.12893 4 3.12893H7.25Z" fill="currentColor">
       </path>
      </svg>
     </a>
    </p>
   </li>
  </ul>
 </section>
</section>
