<section>
 <h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
  Aggregation with the Zip Code Data Set
  <a class="headerlink leafygreen-ui-1kepx47" href="#aggregation-with-the-zip-code-data-set" 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-with-the-zip-code-data-set">
   </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="#data-model" target="_self">
     <span>
      Data Model
     </span>
    </a>
   </li>
   <li class="leafygreen-ui-1jqtqlb">
    <a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#aggregate---method" target="_self">
     <span>
      <code class="css-1kekw28 e1wawog0">
       aggregate()
      </code>
      Method
     </span>
    </a>
   </li>
   <li class="leafygreen-ui-1jqtqlb">
    <a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#return-states-with-populations-above-10-million" target="_self">
     <span>
      Return States with Populations above 10 Million
     </span>
    </a>
   </li>
   <li class="leafygreen-ui-1jqtqlb">
    <a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#return-average-city-population-by-state" target="_self">
     <span>
      Return Average City Population by State
     </span>
    </a>
   </li>
   <li class="leafygreen-ui-1jqtqlb">
    <a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#return-largest-and-smallest-cities-by-state" target="_self">
     <span>
      Return Largest and Smallest Cities by State
     </span>
    </a>
   </li>
  </ul>
 </div>
 <p class="leafygreen-ui-1kp3ins">
  The examples in this document use the
  <code class="leafygreen-ui-1l06pbn">
   zipcodes
  </code>
  collection. This
collection is available at:
  <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="http://media.mongodb.org/zips.json" rel="noopener noreferrer">
   <span>
    media.mongodb.org/zips.json
   </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>
  . Use
  <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/database-tools/mongoimport/#mongodb-binary-bin.mongoimport" target="_self">
   <span>
    <code class="leafygreen-ui-1l06pbn">
     mongoimport
    </code>
   </span>
  </a>
  to
load this data set into your
  <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
   <code class="leafygreen-ui-1l06pbn">
    mongod
   </code>
  </a>
  instance.
 </p>
 <section>
  <h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
   Data Model
   <a class="headerlink leafygreen-ui-1kepx47" href="#data-model" 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="data-model">
    </div>
   </a>
  </h2>
  <p class="leafygreen-ui-1kp3ins">
   Each document in the
   <code class="leafygreen-ui-1l06pbn">
    zipcodes
   </code>
   collection has the following form:
  </p>
  <script class="structured_data" type="application/ld+json">
   {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n  \"_id\": \"10280\",\n  \"city\": \"NEW YORK\",\n  \"state\": \"NY\",\n  \"pop\": 5574,\n  \"loc\": [\n    -74.016323,\n    40.710537\n  ]\n}","programmingLanguage":"JavaScript"}
  </script>
  <div class="css-16lmvu9">
   <div class="leafygreen-ui-196mwvg">
    <div class="leafygreen-ui-yzcs8i">
     <pre class="leafygreen-ui-11itue3" 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-string">"_id"</span>: <span class="lg-highlight-string">"10280"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <span class="lg-highlight-string">"city"</span>: <span class="lg-highlight-string">"NEW YORK"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <span class="lg-highlight-string">"state"</span>: <span class="lg-highlight-string">"NY"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <span class="lg-highlight-string">"pop"</span>: <span class="lg-highlight-number">5574</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <span class="lg-highlight-string">"loc"</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx">    <!-- -->-<span class="lg-highlight-number">74.016323</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">    <span class="lg-highlight-number">40.710537</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">  ]</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>
  <ul class="leafygreen-ui-rioki0">
   <li class="leafygreen-ui-d7p7b6">
    <p class="leafygreen-ui-1kp3ins">
     The
     <code class="leafygreen-ui-1l06pbn">
      _id
     </code>
     field holds the zip code as a string.
    </p>
   </li>
   <li class="leafygreen-ui-d7p7b6">
    <p class="leafygreen-ui-1kp3ins">
     The
     <code class="leafygreen-ui-1l06pbn">
      city
     </code>
     field holds the city name. A city can have more than one
zip code associated with it as different sections of the city can
each have a different zip code.
    </p>
   </li>
   <li class="leafygreen-ui-d7p7b6">
    <p class="leafygreen-ui-1kp3ins">
     The
     <code class="leafygreen-ui-1l06pbn">
      state
     </code>
     field holds the two letter state abbreviation.
    </p>
   </li>
   <li class="leafygreen-ui-d7p7b6">
    <p class="leafygreen-ui-1kp3ins">
     The
     <code class="leafygreen-ui-1l06pbn">
      pop
     </code>
     field holds the population.
    </p>
   </li>
   <li class="leafygreen-ui-d7p7b6">
    <p class="leafygreen-ui-1kp3ins">
     The
     <code class="leafygreen-ui-1l06pbn">
      loc
     </code>
     field holds the location as a longitude latitude pair.
    </p>
   </li>
  </ul>
 </section>
 <section>
  <h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
   <code class="leafygreen-ui-1l06pbn">
    aggregate()
   </code>
   Method
   <a class="headerlink leafygreen-ui-1kepx47" href="#aggregate---method" 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="aggregate---method">
    </div>
   </a>
  </h2>
  <p class="leafygreen-ui-1kp3ins">
   All of the following examples use the
   <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.aggregate/#mongodb-method-db.collection.aggregate">
    <code class="leafygreen-ui-1l06pbn">
     aggregate()
    </code>
   </a>
   helper in
   <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
    <span>
     <code class="leafygreen-ui-1l06pbn">
      mongosh
     </code>
     .
    </span>
   </a>
  </p>
  <p class="leafygreen-ui-1kp3ins">
   The
   <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.aggregate/#mongodb-method-db.collection.aggregate">
    <code class="leafygreen-ui-1l06pbn">
     aggregate()
    </code>
   </a>
   method uses the
   <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/aggregation-pipeline/#std-label-aggregation-pipeline">
    aggregation pipeline
   </a>
   to process
documents into aggregated results. An
   <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/aggregation-pipeline/#std-label-aggregation-pipeline">
    aggregation pipeline
   </a>
   consists of
   <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation-pipeline/#std-label-aggregation-pipeline-operator-reference">
    stages
   </a>
   with each stage processing
the documents as they pass along the pipeline. Documents pass through
the stages in sequence.
  </p>
  <p class="leafygreen-ui-1kp3ins">
   The
   <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.aggregate/#mongodb-method-db.collection.aggregate">
    <code class="leafygreen-ui-1l06pbn">
     aggregate()
    </code>
   </a>
   method in
   <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
    <span>
     <code class="leafygreen-ui-1l06pbn">
      mongosh
     </code>
    </span>
   </a>
   provides a wrapper around the
   <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/aggregate/#mongodb-dbcommand-dbcmd.aggregate">
    <code class="leafygreen-ui-1l06pbn">
     aggregate
    </code>
   </a>
   database command. See the documentation for your
   <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/drivers/" target="_self">
    <span>
     driver
    </span>
   </a>
   for a more idiomatic interface
for data aggregation operations.
  </p>
  <span class="leafygreen-ui-1qnjd6d" id="std-label-aggregation-return-states-by-population">
  </span>
 </section>
 <section>
  <h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
   Return States with Populations above 10 Million
   <a class="headerlink leafygreen-ui-1kepx47" href="#return-states-with-populations-above-10-million" 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="return-states-with-populations-above-10-million">
    </div>
   </a>
  </h2>
  <p class="leafygreen-ui-1kp3ins">
   The following aggregation operation returns all states with total
population greater than 10 million:
  </p>
  <script class="structured_data" type="application/ld+json">
   {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.zipcodes.aggregate( [\n   { $group: { _id: \"$state\", totalPop: { $sum: \"$pop\" } } },\n   { $match: { totalPop: { $gte: 10*1000*1000 } } }\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">zipcodes</span>.<span class="lg-highlight-title lg-highlight-function">aggregate</span>( <!-- -->[</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">"$state"</span>, <span class="lg-highlight-attr">totalPop</span>: <!-- -->{ <span class="lg-highlight-attr">$sum</span>: <span class="lg-highlight-string">"$pop"</span> } } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">$match</span>: <!-- -->{ <span class="lg-highlight-attr">totalPop</span>: <!-- -->{ <span class="lg-highlight-attr">$gte</span>: <span class="lg-highlight-number">10</span>*<span class="lg-highlight-number">1000</span>*<span class="lg-highlight-number">1000</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>
  <p class="leafygreen-ui-1kp3ins">
   In this example, the
   <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/aggregation-pipeline/#std-label-aggregation-pipeline">
    aggregation pipeline
   </a>
   consists of 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 followed by 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">
     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 groups the documents of the
     <code class="leafygreen-ui-1l06pbn">
      zipcode
     </code>
     collection by the
     <code class="leafygreen-ui-1l06pbn">
      state
     </code>
     field, calculates the
     <code class="leafygreen-ui-1l06pbn">
      totalPop
     </code>
     field
for each state, and outputs a document for each unique state.
    </p>
    <p class="leafygreen-ui-1kp3ins">
     The new per-state documents have two fields: the
     <code class="leafygreen-ui-1l06pbn">
      _id
     </code>
     field and
the
     <code class="leafygreen-ui-1l06pbn">
      totalPop
     </code>
     field. The
     <code class="leafygreen-ui-1l06pbn">
      _id
     </code>
     field contains the value of the
     <code class="leafygreen-ui-1l06pbn">
      state
     </code>
     ; i.e. the group by field. The
     <code class="leafygreen-ui-1l06pbn">
      totalPop
     </code>
     field is a
calculated field that contains the total population of each state. To
calculate the value,
     <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/group/#mongodb-pipeline-pipe.-group">
      <code class="leafygreen-ui-1l06pbn">
       $group
      </code>
     </a>
     uses the
     <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/sum/#mongodb-group-grp.-sum">
      <code class="leafygreen-ui-1l06pbn">
       $sum
      </code>
     </a>
     operator to add the population field (
     <code class="leafygreen-ui-1l06pbn">
      pop
     </code>
     ) for each state.
    </p>
    <p class="leafygreen-ui-1kp3ins">
     After 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, the documents in the
pipeline resemble the following:
    </p>
    <script class="structured_data" type="application/ld+json">
     {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n  \"_id\" : \"AK\",\n  \"totalPop\" : 550043\n}","programmingLanguage":"JavaScript"}
    </script>
    <div class="css-16lmvu9">
     <div class="leafygreen-ui-196mwvg">
      <div class="leafygreen-ui-yzcs8i">
       <pre class="leafygreen-ui-11itue3" 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-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"AK"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <span class="lg-highlight-string">"totalPop"</span> <!-- -->: <span class="lg-highlight-number">550043</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>
   </li>
   <li class="leafygreen-ui-d7p7b6">
    <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 filters these grouped documents to
output only those documents whose
     <code class="leafygreen-ui-1l06pbn">
      totalPop
     </code>
     value is greater than
or equal to 10 million. 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 does not alter
the matching documents but outputs the matching documents unmodified.
    </p>
   </li>
  </ul>
  <p class="leafygreen-ui-1kp3ins">
   The equivalent
   <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-SQL">
    SQL
   </a>
   for this aggregation operation is:
  </p>
  <script class="structured_data" type="application/ld+json">
   {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"SELECT state, SUM(pop) AS totalPop\nFROM zipcodes\nGROUP BY state\nHAVING totalPop &gt;= (10*1000*1000)","programmingLanguage":"SQL"}
  </script>
  <div class="css-16lmvu9">
   <div class="leafygreen-ui-196mwvg">
    <div class="leafygreen-ui-yzcs8i">
     <pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light sql leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">SELECT</span> state, <span class="lg-highlight-built_in">SUM</span>(<!-- -->pop) <span class="lg-highlight-keyword">AS</span> totalPop</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">FROM</span> zipcodes</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">GROUP</span> <span class="lg-highlight-keyword">BY</span> state</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">HAVING</span> totalPop <span class="lg-highlight-operator">&gt;=</span> (<span class="lg-highlight-number">10</span><span class="lg-highlight-operator">*</span><span class="lg-highlight-number">1000</span><span class="lg-highlight-operator">*</span><span class="lg-highlight-number">1000</span>)</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>
  <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/reference/operator/aggregation/group/#mongodb-pipeline-pipe.-group">
        <code class="leafygreen-ui-1l06pbn">
         $group
        </code>
       </a>
      </p>
     </li>
     <li class="leafygreen-ui-d7p7b6">
      <p class="leafygreen-ui-1kp3ins">
       <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/match/#mongodb-pipeline-pipe.-match">
        <code class="leafygreen-ui-1l06pbn">
         $match
        </code>
       </a>
      </p>
     </li>
     <li class="leafygreen-ui-d7p7b6">
      <p class="leafygreen-ui-1kp3ins">
       <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/sum/#mongodb-group-grp.-sum">
        <code class="leafygreen-ui-1l06pbn">
         $sum
        </code>
       </a>
      </p>
     </li>
    </ul>
   </div>
  </div>
 </section>
 <section>
  <h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
   Return Average City Population by State
   <a class="headerlink leafygreen-ui-1kepx47" href="#return-average-city-population-by-state" 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="return-average-city-population-by-state">
    </div>
   </a>
  </h2>
  <p class="leafygreen-ui-1kp3ins">
   The following aggregation operation returns the average populations for
cities in each state:
  </p>
  <script class="structured_data" type="application/ld+json">
   {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.zipcodes.aggregate( [\n   { $group: { _id: { state: \"$state\", city: \"$city\" }, pop: { $sum: \"$pop\" } } },\n   { $group: { _id: \"$_id.state\", avgCityPop: { $avg: \"$pop\" } } }\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">zipcodes</span>.<span class="lg-highlight-title lg-highlight-function">aggregate</span>( <!-- -->[</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-attr">state</span>: <span class="lg-highlight-string">"$state"</span>, <span class="lg-highlight-attr">city</span>: <span class="lg-highlight-string">"$city"</span> }<!-- -->, <span class="lg-highlight-attr">pop</span>: <!-- -->{ <span class="lg-highlight-attr">$sum</span>: <span class="lg-highlight-string">"$pop"</span> } } }<!-- -->,</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">"$_id.state"</span>, <span class="lg-highlight-attr">avgCityPop</span>: <!-- -->{ <span class="lg-highlight-attr">$avg</span>: <span class="lg-highlight-string">"$pop"</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>
  <p class="leafygreen-ui-1kp3ins">
   In this example, the
   <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/aggregation-pipeline/#std-label-aggregation-pipeline">
    aggregation pipeline
   </a>
   consists of 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 followed by another
   <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">
     The first
     <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 groups the documents by the
combination of
     <code class="leafygreen-ui-1l06pbn">
      city
     </code>
     and
     <code class="leafygreen-ui-1l06pbn">
      state
     </code>
     , uses the
     <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/sum/#mongodb-group-grp.-sum">
      <code class="leafygreen-ui-1l06pbn">
       $sum
      </code>
     </a>
     expression to calculate the population for each combination, and
outputs a document for each
     <code class="leafygreen-ui-1l06pbn">
      city
     </code>
     and
     <code class="leafygreen-ui-1l06pbn">
      state
     </code>
     combination.
     <a class="footnote-reference header-buffer" href="#footnote-multiple-zips-per-city" id="ref-multiple-zips-per-city-id1">
      [
      <!-- -->
      1
      <!-- -->
      ]
     </a>
    </p>
    <p class="leafygreen-ui-1kp3ins">
     After this stage in the pipeline, the documents resemble the
following:
    </p>
    <script class="structured_data" type="application/ld+json">
     {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n  \"_id\" : {\n    \"state\" : \"CO\",\n    \"city\" : \"EDGEWATER\"\n  },\n  \"pop\" : 13154\n}","programmingLanguage":"JavaScript"}
    </script>
    <div class="css-16lmvu9">
     <div class="leafygreen-ui-196mwvg">
      <div class="leafygreen-ui-yzcs8i">
       <pre class="leafygreen-ui-11itue3" 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-string">"_id"</span> <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">    <span class="lg-highlight-string">"state"</span> <!-- -->: <span class="lg-highlight-string">"CO"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">    <span class="lg-highlight-string">"city"</span> <!-- -->: <span class="lg-highlight-string">"EDGEWATER"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">  }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <span class="lg-highlight-string">"pop"</span> <!-- -->: <span class="lg-highlight-number">13154</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>
   </li>
   <li class="leafygreen-ui-d7p7b6">
    <p class="leafygreen-ui-1kp3ins">
     A second
     <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 groups the documents in the
pipeline by the
     <code class="leafygreen-ui-1l06pbn">
      _id.state
     </code>
     field (i.e. the
     <code class="leafygreen-ui-1l06pbn">
      state
     </code>
     field inside
the
     <code class="leafygreen-ui-1l06pbn">
      _id
     </code>
     document), uses the
     <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/avg/#mongodb-group-grp.-avg">
      <code class="leafygreen-ui-1l06pbn">
       $avg
      </code>
     </a>
     expression to calculate
the average city population (
     <code class="leafygreen-ui-1l06pbn">
      avgCityPop
     </code>
     ) for each state, and
outputs a document for each state.
    </p>
   </li>
  </ul>
  <p class="leafygreen-ui-1kp3ins">
   The documents that result from this aggregation operation resembles the
following:
  </p>
  <script class="structured_data" type="application/ld+json">
   {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n  \"_id\" : \"MN\",\n  \"avgCityPop\" : 5335\n}","programmingLanguage":"JavaScript"}
  </script>
  <div class="css-16lmvu9">
   <div class="leafygreen-ui-196mwvg">
    <div class="leafygreen-ui-yzcs8i">
     <pre class="leafygreen-ui-11itue3" 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-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"MN"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <span class="lg-highlight-string">"avgCityPop"</span> <!-- -->: <span class="lg-highlight-number">5335</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>
  <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/reference/operator/aggregation/group/#mongodb-pipeline-pipe.-group">
        <code class="leafygreen-ui-1l06pbn">
         $group
        </code>
       </a>
      </p>
     </li>
     <li class="leafygreen-ui-d7p7b6">
      <p class="leafygreen-ui-1kp3ins">
       <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/sum/#mongodb-group-grp.-sum">
        <code class="leafygreen-ui-1l06pbn">
         $sum
        </code>
       </a>
      </p>
     </li>
     <li class="leafygreen-ui-d7p7b6">
      <p class="leafygreen-ui-1kp3ins">
       <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>
   </div>
  </div>
 </section>
 <section>
  <h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
   Return Largest and Smallest Cities by State
   <a class="headerlink leafygreen-ui-1kepx47" href="#return-largest-and-smallest-cities-by-state" 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="return-largest-and-smallest-cities-by-state">
    </div>
   </a>
  </h2>
  <p class="leafygreen-ui-1kp3ins">
   The following aggregation operation returns the smallest and largest
cities by population for each state:
  </p>
  <script class="structured_data" type="application/ld+json">
   {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.zipcodes.aggregate( [\n   { $group:\n      {\n        _id: { state: \"$state\", city: \"$city\" },\n        pop: { $sum: \"$pop\" }\n      }\n   },\n   { $sort: { pop: 1 } },\n   { $group:\n      {\n        _id : \"$_id.state\",\n        biggestCity:  { $last: \"$_id.city\" },\n        biggestPop:   { $last: \"$pop\" },\n        smallestCity: { $first: \"$_id.city\" },\n        smallestPop:  { $first: \"$pop\" }\n      }\n   },\n\n  // the following $project is optional, and\n  // modifies the output format.\n\n  { $project:\n    { _id: 0,\n      state: \"$_id\",\n      biggestCity:  { name: \"$biggestCity\",  pop: \"$biggestPop\" },\n      smallestCity: { name: \"$smallestCity\", pop: \"$smallestPop\" }\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">zipcodes</span>.<span class="lg-highlight-title lg-highlight-function">aggregate</span>( <!-- -->[</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">state</span>: <span class="lg-highlight-string">"$state"</span>, <span class="lg-highlight-attr">city</span>: <span class="lg-highlight-string">"$city"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">        <span class="lg-highlight-attr">pop</span>: <!-- -->{ <span class="lg-highlight-attr">$sum</span>: <span class="lg-highlight-string">"$pop"</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">   <!-- -->{ <span class="lg-highlight-attr">$sort</span>: <!-- -->{ <span class="lg-highlight-attr">pop</span>: <span class="lg-highlight-number">1</span> } }<!-- -->,</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">        <!-- -->_id <!-- -->: <span class="lg-highlight-string">"$_id.state"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">        <span class="lg-highlight-attr">biggestCity</span>:  <!-- -->{ <span class="lg-highlight-attr">$last</span>: <span class="lg-highlight-string">"$_id.city"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">        <span class="lg-highlight-attr">biggestPop</span>:   <!-- -->{ <span class="lg-highlight-attr">$last</span>: <span class="lg-highlight-string">"$pop"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">        <span class="lg-highlight-attr">smallestCity</span>: <!-- -->{ <span class="lg-highlight-attr">$first</span>: <span class="lg-highlight-string">"$_id.city"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">        <span class="lg-highlight-attr">smallestPop</span>:  <!-- -->{ <span class="lg-highlight-attr">$first</span>: <span class="lg-highlight-string">"$pop"</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">// the following $project is optional, and</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> modifies the output </span><span class="lg-highlight-comment">format.</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-attr">$project</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx">    <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">0</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <span class="lg-highlight-attr">state</span>: <span class="lg-highlight-string">"$_id"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <span class="lg-highlight-attr">biggestCity</span>:  <!-- -->{ <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"$biggestCity"</span>,  <span class="lg-highlight-attr">pop</span>: <span class="lg-highlight-string">"$biggestPop"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <span class="lg-highlight-attr">smallestCity</span>: <!-- -->{ <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"$smallestCity"</span>, <span class="lg-highlight-attr">pop</span>: <span class="lg-highlight-string">"$smallestPop"</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">] )</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">
   In this example, the
   <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/aggregation-pipeline/#std-label-aggregation-pipeline">
    aggregation pipeline
   </a>
   consists of a
   <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, a
   <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,
another
   <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, and a
   <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/project/#mongodb-pipeline-pipe.-project">
    <code class="leafygreen-ui-1l06pbn">
     $project
    </code>
   </a>
   stage:
  </p>
  <ul class="leafygreen-ui-rioki0">
   <li class="leafygreen-ui-d7p7b6">
    <p class="leafygreen-ui-1kp3ins">
     The first
     <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 groups the documents by the
combination of the
     <code class="leafygreen-ui-1l06pbn">
      city
     </code>
     and
     <code class="leafygreen-ui-1l06pbn">
      state
     </code>
     , calculates the
     <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/sum/#mongodb-group-grp.-sum">
      <code class="leafygreen-ui-1l06pbn">
       sum
      </code>
     </a>
     of the
     <code class="leafygreen-ui-1l06pbn">
      pop
     </code>
     values for each combination, and outputs a
document for each
     <code class="leafygreen-ui-1l06pbn">
      city
     </code>
     and
     <code class="leafygreen-ui-1l06pbn">
      state
     </code>
     combination.
    </p>
    <p class="leafygreen-ui-1kp3ins">
     At this stage in the pipeline, the documents resemble the following:
    </p>
    <script class="structured_data" type="application/ld+json">
     {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n  \"_id\" : {\n    \"state\" : \"CO\",\n    \"city\" : \"EDGEWATER\"\n  },\n  \"pop\" : 13154\n}","programmingLanguage":"JavaScript"}
    </script>
    <div class="css-16lmvu9">
     <div class="leafygreen-ui-196mwvg">
      <div class="leafygreen-ui-yzcs8i">
       <pre class="leafygreen-ui-11itue3" 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-string">"_id"</span> <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">    <span class="lg-highlight-string">"state"</span> <!-- -->: <span class="lg-highlight-string">"CO"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">    <span class="lg-highlight-string">"city"</span> <!-- -->: <span class="lg-highlight-string">"EDGEWATER"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">  }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <span class="lg-highlight-string">"pop"</span> <!-- -->: <span class="lg-highlight-number">13154</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>
   </li>
   <li class="leafygreen-ui-d7p7b6">
    <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 orders the documents in the pipeline by
the
     <code class="leafygreen-ui-1l06pbn">
      pop
     </code>
     field value, from smallest to largest; i.e. by
increasing order. This operation does not alter the documents.
    </p>
   </li>
   <li class="leafygreen-ui-d7p7b6">
    <p class="leafygreen-ui-1kp3ins">
     The next
     <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 groups the now-sorted documents
by the
     <code class="leafygreen-ui-1l06pbn">
      _id.state
     </code>
     field (i.e. the
     <code class="leafygreen-ui-1l06pbn">
      state
     </code>
     field inside the
     <code class="leafygreen-ui-1l06pbn">
      _id
     </code>
     document) and outputs a document for each state.
    </p>
    <p class="leafygreen-ui-1kp3ins">
     The stage also calculates the following four fields for each state.
Using the
     <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/last/#mongodb-group-grp.-last">
      <code class="leafygreen-ui-1l06pbn">
       $last
      </code>
     </a>
     expression, 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>
     operator
creates the
     <code class="leafygreen-ui-1l06pbn">
      biggestCity
     </code>
     and
     <code class="leafygreen-ui-1l06pbn">
      biggestPop
     </code>
     fields that store the
city with the largest population and that population. Using the
     <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/first/#mongodb-group-grp.-first">
      <code class="leafygreen-ui-1l06pbn">
       $first
      </code>
     </a>
     expression, 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>
     operator creates
the
     <code class="leafygreen-ui-1l06pbn">
      smallestCity
     </code>
     and
     <code class="leafygreen-ui-1l06pbn">
      smallestPop
     </code>
     fields that store the city
with the smallest population and that population.
    </p>
    <p class="leafygreen-ui-1kp3ins">
     The documents, at this stage in the pipeline, resemble the following:
    </p>
    <script class="structured_data" type="application/ld+json">
     {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n  \"_id\" : \"WA\",\n  \"biggestCity\" : \"SEATTLE\",\n  \"biggestPop\" : 520096,\n  \"smallestCity\" : \"BENGE\",\n  \"smallestPop\" : 2\n}","programmingLanguage":"JavaScript"}
    </script>
    <div class="css-16lmvu9">
     <div class="leafygreen-ui-196mwvg">
      <div class="leafygreen-ui-yzcs8i">
       <pre class="leafygreen-ui-11itue3" 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-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"WA"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <span class="lg-highlight-string">"biggestCity"</span> <!-- -->: <span class="lg-highlight-string">"SEATTLE"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <span class="lg-highlight-string">"biggestPop"</span> <!-- -->: <span class="lg-highlight-number">520096</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <span class="lg-highlight-string">"smallestCity"</span> <!-- -->: <span class="lg-highlight-string">"BENGE"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <span class="lg-highlight-string">"smallestPop"</span> <!-- -->: <span class="lg-highlight-number">2</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>
   </li>
   <li class="leafygreen-ui-d7p7b6">
    <p class="leafygreen-ui-1kp3ins">
     The final
     <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/project/#mongodb-pipeline-pipe.-project">
      <code class="leafygreen-ui-1l06pbn">
       $project
      </code>
     </a>
     stage renames the
     <code class="leafygreen-ui-1l06pbn">
      _id
     </code>
     field to
     <code class="leafygreen-ui-1l06pbn">
      state
     </code>
     and moves the
     <code class="leafygreen-ui-1l06pbn">
      biggestCity
     </code>
     ,
     <code class="leafygreen-ui-1l06pbn">
      biggestPop
     </code>
     ,
     <code class="leafygreen-ui-1l06pbn">
      smallestCity
     </code>
     , and
     <code class="leafygreen-ui-1l06pbn">
      smallestPop
     </code>
     into
     <code class="leafygreen-ui-1l06pbn">
      biggestCity
     </code>
     and
     <code class="leafygreen-ui-1l06pbn">
      smallestCity
     </code>
     embedded documents.
    </p>
   </li>
  </ul>
  <p class="leafygreen-ui-1kp3ins">
   The output documents of this aggregation operation resemble the following:
  </p>
  <script class="structured_data" type="application/ld+json">
   {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n  \"state\" : \"RI\",\n  \"biggestCity\" : {\n    \"name\" : \"CRANSTON\",\n    \"pop\" : 176404\n  },\n  \"smallestCity\" : {\n    \"name\" : \"CLAYVILLE\",\n    \"pop\" : 45\n  }\n}","programmingLanguage":"JavaScript"}
  </script>
  <div class="css-16lmvu9">
   <div class="leafygreen-ui-196mwvg">
    <div class="leafygreen-ui-yzcs8i">
     <pre class="leafygreen-ui-11itue3" 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-string">"state"</span> <!-- -->: <span class="lg-highlight-string">"RI"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <span class="lg-highlight-string">"biggestCity"</span> <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">    <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"CRANSTON"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">    <span class="lg-highlight-string">"pop"</span> <!-- -->: <span class="lg-highlight-number">176404</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">  }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <span class="lg-highlight-string">"smallestCity"</span> <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">    <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"CLAYVILLE"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">    <span class="lg-highlight-string">"pop"</span> <!-- -->: <span class="lg-highlight-number">45</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">  }</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>
  <table class="header-buffer leafygreen-ui-rbqgrl" frame="void" id="footnote-multiple-zips-per-city" rules="none">
   <colgroup>
    <col/>
   </colgroup>
   <tbody valign="top">
    <tr>
     <td class="leafygreen-ui-17r2xs0">
      [
      <a href="#ref-multiple-zips-per-city-id1">
       1
      </a>
      ]
     </td>
     <td class="leafygreen-ui-17r2xs0">
      <!-- -->
      A city can have more than one zip
code associated with it as different sections of the city can each
have a different zip code.
     </td>
    </tr>
   </tbody>
  </table>
 </section>
</section>
