<section>
 <h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
  Map-Reduce to Aggregation Pipeline
  <a class="headerlink leafygreen-ui-1kepx47" href="#map-reduce-to-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="map-reduce-to-aggregation-pipeline">
   </div>
  </a>
 </h1>
 <p class="leafygreen-ui-1kp3ins">
  An
  <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/aggregation-pipeline/#std-label-aggregation-pipeline">
   aggregation pipeline
  </a>
  provides
better performance and usability than a
  <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/map-reduce/">
   map-reduce
  </a>
  operation.
 </p>
 <p class="leafygreen-ui-1kp3ins">
  Map-reduce operations can be rewritten 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>
  and
  <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/merge/#mongodb-pipeline-pipe.-merge">
   <code class="leafygreen-ui-1l06pbn">
    $merge
   </code>
   .
  </a>
 </p>
 <p class="leafygreen-ui-1kp3ins">
  For map-reduce operations that require custom functionality, MongoDB
provides 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. Use these operators to
define custom aggregation expressions in JavaScript.
 </p>
 <p class="leafygreen-ui-1kp3ins">
  Map-reduce expressions can be re-written as shown in the following
sections.
 </p>
 <section>
  <h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
   Map-Reduce to Aggregation Pipeline Translation Table
   <a class="headerlink leafygreen-ui-1kepx47" href="#map-reduce-to-aggregation-pipeline-translation-table" 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="map-reduce-to-aggregation-pipeline-translation-table">
    </div>
   </a>
  </h2>
  <p class="leafygreen-ui-1kp3ins">
   The table is only an approximate translation. For instance, the table
shows an approximate translation of
   <code class="leafygreen-ui-1l06pbn">
    mapFunction
   </code>
   using the
   <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/project/#mongodb-pipeline-pipe.-project">
    <code class="leafygreen-ui-1l06pbn">
     $project
    </code>
    .
   </a>
  </p>
  <ul class="leafygreen-ui-rioki0">
   <li class="leafygreen-ui-d7p7b6">
    <p class="leafygreen-ui-1kp3ins">
     However, the
     <code class="leafygreen-ui-1l06pbn">
      mapFunction
     </code>
     logic may require additional stages,
such as if the logic includes iteration over an array:
    </p>
    <script class="structured_data" type="application/ld+json">
     {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"function() {\n   this.items.forEach(function(item){ emit(item.sku, 1); });\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"><span class="lg-highlight-keyword lg-highlight-function">function</span>(<!-- -->)<!-- --> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <span class="lg-highlight-variable lg-highlight-language">this</span>.<span class="lg-highlight-property">items</span>.<span class="lg-highlight-title lg-highlight-function">forEach</span>(<span class="lg-highlight-keyword lg-highlight-function">function</span>(<span class="lg-highlight-params">item</span>)<!-- -->{ <span class="lg-highlight-title lg-highlight-function">emit</span>(<!-- -->item.<span class="lg-highlight-property">sku</span>, <span class="lg-highlight-number">1</span>)<!-- -->; <!-- -->})<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
      </div>
     </div>
    </div>
    <p class="leafygreen-ui-1kp3ins">
     Then, the aggregation pipeline includes an
     <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/unwind/#mongodb-pipeline-pipe.-unwind">
      <code class="leafygreen-ui-1l06pbn">
       $unwind
      </code>
     </a>
     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>
    </p>
    <script class="structured_data" type="application/ld+json">
     {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ $unwind: \"$items \"},\n{ $project: { emits: { key: { \"$items.sku\" }, value: 1 } } },","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">{ <span class="lg-highlight-attr">$unwind</span>: <span class="lg-highlight-string">"$items "</span>}<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">$project</span>: <!-- -->{ <span class="lg-highlight-attr">emits</span>: <!-- -->{ <span class="lg-highlight-attr">key</span>: <!-- -->{ <span class="lg-highlight-string">"$items.sku"</span> }<!-- -->, <span class="lg-highlight-attr">value</span>: <span class="lg-highlight-number">1</span> } } }<!-- -->,</td></tr></tbody></table></code></pre>
      </div>
     </div>
    </div>
   </li>
   <li class="leafygreen-ui-d7p7b6">
    <p class="leafygreen-ui-1kp3ins">
     The
     <code class="leafygreen-ui-1l06pbn">
      emits
     </code>
     field in
     <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/project/#mongodb-pipeline-pipe.-project">
      <code class="leafygreen-ui-1l06pbn">
       $project
      </code>
     </a>
     may be named something
else. For visual comparison, the field name
     <code class="leafygreen-ui-1l06pbn">
      emits
     </code>
     was chosen.
    </p>
   </li>
  </ul>
  <div class="leafygreen-ui-skkc39" tabindex="0">
   <table class="leafygreen-ui-hr8lsv">
    <colgroup>
     <col style="width:45%"/>
     <col style="width:55%"/>
    </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>
         Map-Reduce
        </div>
       </div>
      </th>
      <th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
       <div class="leafygreen-ui-ui0te9">
        <div>
         Aggregation Pipeline
        </div>
       </div>
      </th>
     </tr>
    </thead>
    <tbody>
     <tr class="leafygreen-ui-15dvrnp">
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         <div class="line-block">
          <div class="line">
           db.collection.mapReduce(
          </div>
          <div class="line-block">
           <div class="line">
            <span class="leafygreen-ui-1fku1pu" style="--background-color:#FFEAE5">
             &lt;mapFunction&gt;
            </span>
            ,
           </div>
           <div class="line">
            <span class="leafygreen-ui-1fku1pu" style="--background-color:#FEF7DB">
             &lt;reduceFunction&gt;
            </span>
            ,
           </div>
           <div class="line">
            {
           </div>
           <div class="line-block">
            <div class="line">
             query: &lt;queryFilter&gt;,
            </div>
            <div class="line">
             sort: &lt;sortOrder&gt;,
            </div>
            <div class="line">
             limit: &lt;number&gt;,
            </div>
            <div class="line">
             finalize:
             <span class="leafygreen-ui-1fku1pu" style="--background-color:#E3FCF7">
              &lt;finalizeFunction&gt;
             </span>
             ,
            </div>
            <div class="line">
             out:
             <span class="leafygreen-ui-1fku1pu" style="--background-color:#E1F7FF">
              &lt;collection&gt;
             </span>
            </div>
           </div>
           <div class="line">
            }
           </div>
          </div>
          <div class="line">
           )
          </div>
         </div>
        </div>
       </div>
      </td>
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         <div class="line-block">
          <div class="line">
           db.collection.aggregate( [
          </div>
          <div class="line-block">
           <div class="line">
            { $match: &lt;queryFilter&gt; },
           </div>
           <div class="line">
            { $sort: &lt;sortOrder&gt; },
           </div>
           <div class="line">
            { $limit: &lt;number&gt; },
           </div>
           <div class="line">
            {
            <span class="leafygreen-ui-1fku1pu" style="--background-color:#FFEAE5">
             $project: { emits: { k: &lt;expression&gt;, v: &lt;expression&gt; } }
            </span>
            },
           </div>
           <div class="line">
            {
            <span class="leafygreen-ui-1fku1pu" style="--background-color:#FFEAE5">
             $unwind: "$emits"
            </span>
            },
           </div>
           <div class="line">
            {
            <span class="leafygreen-ui-1fku1pu" style="--background-color:#FEF7DB">
             $group
            </span>
            : {
           </div>
           <div class="line-block">
            <div class="line">
             _id: "$emits.k"},
            </div>
            <div class="line">
             value: {
             <span class="leafygreen-ui-1fku1pu" style="--background-color:#FEF7DB">
              $accumulator
             </span>
             : {
            </div>
            <div class="line-block">
             <div class="line">
              init: &lt;initCode&gt;,
             </div>
             <div class="line">
              accumulate:
              <span class="leafygreen-ui-1fku1pu" style="--background-color:#FEF7DB">
               &lt;reduceFunction&gt;
              </span>
              ,
             </div>
             <div class="line">
              accumulateArgs: [ "$emit.v"],
             </div>
             <div class="line">
              merge:
              <span class="leafygreen-ui-1fku1pu" style="--background-color:#FEF7DB">
               &lt;reduceFunction&gt;
              </span>
              ,
             </div>
             <div class="line">
              finalize:
              <span class="leafygreen-ui-1fku1pu" style="--background-color:#E3FCF7">
               &lt;finalizeFunction&gt;
              </span>
              ,
             </div>
             <div class="line">
              lang: "js" }}
             </div>
            </div>
           </div>
           <div class="line">
            } },
           </div>
           <div class="line">
            { $out:
            <span class="leafygreen-ui-1fku1pu" style="--background-color:#E1F7FF">
             &lt;collection&gt;
            </span>
            }
           </div>
          </div>
          <div class="line">
           ] )
          </div>
         </div>
        </div>
       </div>
      </td>
     </tr>
     <tr class="leafygreen-ui-15dvrnp">
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         <div class="line-block">
          <div class="line">
           db.collection.mapReduce(
          </div>
          <div class="line-block">
           <div class="line">
            <span class="leafygreen-ui-1fku1pu" style="--background-color:#FFEAE5">
             &lt;mapFunction&gt;
            </span>
            ,
           </div>
           <div class="line">
            <span class="leafygreen-ui-1fku1pu" style="--background-color:#FEF7DB">
             &lt;reduceFunction&gt;
            </span>
            ,
           </div>
           <div class="line">
            {
           </div>
           <div class="line-block">
            <div class="line">
             query: &lt;queryFilter&gt;,
            </div>
            <div class="line">
             sort: &lt;sortOrder&gt;,
            </div>
            <div class="line">
             limit: &lt;number&gt;,
            </div>
            <div class="line">
             finalize:
             <span class="leafygreen-ui-1fku1pu" style="--background-color:#E3FCF7">
              &lt;finalizeFunction&gt;
             </span>
             ,
            </div>
            <div class="line">
             out: {
             <span class="leafygreen-ui-1fku1pu" style="--background-color:#E1F7FF">
              replace: &lt;collection&gt;, db:&lt;db&gt;
             </span>
             }
            </div>
           </div>
           <div class="line">
            }
           </div>
          </div>
          <div class="line">
           )
          </div>
         </div>
        </div>
       </div>
      </td>
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         <div class="line-block">
          <div class="line">
           db.collection.aggregate( [
          </div>
          <div class="line-block">
           <div class="line">
            { $match: &lt;queryFilter&gt; },
           </div>
           <div class="line">
            { $sort: &lt;sortOrder&gt; },
           </div>
           <div class="line">
            { $limit: &lt;number&gt; },
           </div>
           <div class="line">
            {
            <span class="leafygreen-ui-1fku1pu" style="--background-color:#FFEAE5">
             $project: { emits: { k: &lt;expression&gt;, v: &lt;expression&gt; } }
            </span>
            },
           </div>
           <div class="line">
            {
            <span class="leafygreen-ui-1fku1pu" style="--background-color:#FFEAE5">
             $unwind: "$emits"
            </span>
            },
           </div>
           <div class="line">
            {
            <span class="leafygreen-ui-1fku1pu" style="--background-color:#FEF7DB">
             $group
            </span>
            : {
           </div>
           <div class="line-block">
            <div class="line">
             _id: "$emits.k"},
            </div>
            <div class="line">
             value: {
             <span class="leafygreen-ui-1fku1pu" style="--background-color:#FEF7DB">
              $accumulator
             </span>
             : {
            </div>
            <div class="line-block">
             <div class="line">
              init: &lt;initCode&gt;,
             </div>
             <div class="line">
              accumulate:
              <span class="leafygreen-ui-1fku1pu" style="--background-color:#FEF7DB">
               &lt;reduceFunction&gt;
              </span>
              ,
             </div>
             <div class="line">
              accumulateArgs: [ "$emit.v"],
             </div>
             <div class="line">
              merge:
              <span class="leafygreen-ui-1fku1pu" style="--background-color:#FEF7DB">
               &lt;reduceFunction&gt;
              </span>
              ,
             </div>
             <div class="line">
              finalize:
              <span class="leafygreen-ui-1fku1pu" style="--background-color:#E3FCF7">
               &lt;finalizeFunction&gt;
              </span>
              ,
             </div>
             <div class="line">
              lang: "js" }}
             </div>
            </div>
           </div>
           <div class="line">
            } },
           </div>
           <div class="line">
            { $out: {
            <span class="leafygreen-ui-1fku1pu" style="--background-color:#E1F7FF">
             db: &lt;db&gt;, coll: &lt;collection&gt;
            </span>
            } }
           </div>
          </div>
          <div class="line">
           ] )
          </div>
         </div>
        </div>
       </div>
      </td>
     </tr>
     <tr class="leafygreen-ui-15dvrnp">
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         <div class="line-block">
          <div class="line">
           db.collection.mapReduce(
          </div>
          <div class="line-block">
           <div class="line">
            <span class="leafygreen-ui-1fku1pu" style="--background-color:#FFEAE5">
             &lt;mapFunction&gt;
            </span>
            ,
           </div>
           <div class="line">
            <span class="leafygreen-ui-1fku1pu" style="--background-color:#FEF7DB">
             &lt;reduceFunction&gt;
            </span>
            ,
           </div>
           <div class="line">
            {
           </div>
           <div class="line-block">
            <div class="line">
             query: &lt;queryFilter&gt;,
            </div>
            <div class="line">
             sort: &lt;sortOrder&gt;,
            </div>
            <div class="line">
             limit: &lt;number&gt;,
            </div>
            <div class="line">
             finalize:
             <span class="leafygreen-ui-1fku1pu" style="--background-color:#E3FCF7">
              &lt;finalizeFunction&gt;
             </span>
             ,
            </div>
            <div class="line">
             out: {
             <span class="leafygreen-ui-1fku1pu" style="--background-color:#E1F7FF">
              merge: &lt;collection&gt;, db: &lt;db&gt;
             </span>
             }
            </div>
           </div>
           <div class="line">
            }
           </div>
          </div>
          <div class="line">
           )
          </div>
         </div>
        </div>
       </div>
      </td>
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         <div class="line-block">
          <div class="line">
           db.collection.aggregate( [
          </div>
          <div class="line-block">
           <div class="line">
            { $match: &lt;queryFilter&gt; },
           </div>
           <div class="line">
            { $sort: &lt;sortOrder&gt; },
           </div>
           <div class="line">
            { $limit: &lt;number&gt; },
           </div>
           <div class="line">
            {
            <span class="leafygreen-ui-1fku1pu" style="--background-color:#FFEAE5">
             $project: { emits: { k: &lt;expression&gt;, v: &lt;expression&gt; } }
            </span>
            },
           </div>
           <div class="line">
            {
            <span class="leafygreen-ui-1fku1pu" style="--background-color:#FFEAE5">
             $unwind: "$emits"
            </span>
            },
           </div>
           <div class="line">
            {
            <span class="leafygreen-ui-1fku1pu" style="--background-color:#FEF7DB">
             $group
            </span>
            : {
           </div>
           <div class="line-block">
            <div class="line">
             _id: "$emits.k"},
            </div>
            <div class="line">
             value: {
             <span class="leafygreen-ui-1fku1pu" style="--background-color:#FEF7DB">
              $accumulator
             </span>
             : {
            </div>
            <div class="line-block">
             <div class="line">
              init: &lt;initCode&gt;,
             </div>
             <div class="line">
              accumulate:
              <span class="leafygreen-ui-1fku1pu" style="--background-color:#FEF7DB">
               &lt;reduceFunction&gt;
              </span>
              ,
             </div>
             <div class="line">
              accumulateArgs: [ "$emit.v"],
             </div>
             <div class="line">
              merge:
              <span class="leafygreen-ui-1fku1pu" style="--background-color:#FEF7DB">
               &lt;reduceFunction&gt;
              </span>
              ,
             </div>
             <div class="line">
              finalize:
              <span class="leafygreen-ui-1fku1pu" style="--background-color:#E3FCF7">
               &lt;finalizeFunction&gt;
              </span>
              ,
             </div>
             <div class="line">
              lang: "js" }}
             </div>
            </div>
           </div>
           <div class="line">
            } },
           </div>
           <div class="line">
            {
            <span class="leafygreen-ui-1fku1pu" style="--background-color:#E1F7FF">
             $merge
            </span>
            : {
           </div>
           <div class="line-block">
            <div class="line">
             into: {
             <span class="leafygreen-ui-1fku1pu" style="--background-color:#E1F7FF">
              db: &lt;db&gt;, coll: &lt;collection&gt;
             </span>
             },
            </div>
            <div class="line">
             on: "_id"
            </div>
            <div class="line">
             whenMatched: "replace",
            </div>
            <div class="line">
             whenNotMatched: "insert"
            </div>
           </div>
           <div class="line">
            } },
           </div>
          </div>
          <div class="line">
           ] )
          </div>
         </div>
        </div>
       </div>
      </td>
     </tr>
     <tr class="leafygreen-ui-15dvrnp">
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         <div class="line-block">
          <div class="line">
           db.collection.mapReduce(
          </div>
          <div class="line-block">
           <div class="line">
            <span class="leafygreen-ui-1fku1pu" style="--background-color:#FFEAE5">
             &lt;mapFunction&gt;
            </span>
            ,
           </div>
           <div class="line">
            <span class="leafygreen-ui-1fku1pu" style="--background-color:#FEF7DB">
             &lt;reduceFunction&gt;
            </span>
            ,
           </div>
           <div class="line">
            {
           </div>
           <div class="line-block">
            <div class="line">
             query: &lt;queryFilter&gt;,
            </div>
            <div class="line">
             sort: &lt;sortOrder&gt;,
            </div>
            <div class="line">
             limit: &lt;number&gt;,
            </div>
            <div class="line">
             finalize:
             <span class="leafygreen-ui-1fku1pu" style="--background-color:#E3FCF7">
              &lt;finalizeFunction&gt;
             </span>
             ,
            </div>
            <div class="line">
             out: {
             <span class="leafygreen-ui-1fku1pu" style="--background-color:#E1F7FF">
              reduce: &lt;collection&gt;, db: &lt;db&gt;
             </span>
             }
            </div>
           </div>
           <div class="line">
            }
           </div>
          </div>
          <div class="line">
           )
          </div>
         </div>
        </div>
       </div>
      </td>
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         <div class="line-block">
          <div class="line">
           db.collection.aggregate( [
          </div>
          <div class="line-block">
           <div class="line">
            { $match: &lt;queryFilter&gt; },
           </div>
           <div class="line">
            { $sort: &lt;sortOrder&gt; },
           </div>
           <div class="line">
            { $limit: &lt;number&gt; },
           </div>
           <div class="line">
            {
            <span class="leafygreen-ui-1fku1pu" style="--background-color:#FFEAE5">
             $project: { emits: { k: &lt;expression&gt;, v: &lt;expression&gt; } }
            </span>
            },
           </div>
           <div class="line">
            {
            <span class="leafygreen-ui-1fku1pu" style="--background-color:#FFEAE5">
             $unwind: "$emits"
            </span>
            },
           </div>
           <div class="line">
            {
            <span class="leafygreen-ui-1fku1pu" style="--background-color:#FEF7DB">
             $group
            </span>
            : {
           </div>
           <div class="line-block">
            <div class="line">
             _id: "$emits.k"},
            </div>
            <div class="line">
             value: {
             <span class="leafygreen-ui-1fku1pu" style="--background-color:#FEF7DB">
              $accumulator
             </span>
             : {
            </div>
            <div class="line-block">
             <div class="line">
              init: &lt;initCode&gt;,
             </div>
             <div class="line">
              accumulate:
              <span class="leafygreen-ui-1fku1pu" style="--background-color:#FEF7DB">
               &lt;reduceFunction&gt;
              </span>
              ,
             </div>
             <div class="line">
              accumulateArgs: [ "$emit.v"],
             </div>
             <div class="line">
              merge:
              <span class="leafygreen-ui-1fku1pu" style="--background-color:#FEF7DB">
               &lt;reduceFunction&gt;
              </span>
              ,
             </div>
             <div class="line">
              finalize:
              <span class="leafygreen-ui-1fku1pu" style="--background-color:#E3FCF7">
               &lt;finalizeFunction&gt;
              </span>
              ,
             </div>
             <div class="line">
              lang: "js" }}
             </div>
            </div>
           </div>
           <div class="line">
            } },
           </div>
           <div class="line">
            {
            <span class="leafygreen-ui-1fku1pu" style="--background-color:#E1F7FF">
             $merge
            </span>
            : {
           </div>
           <div class="line-block">
            <div class="line">
             into: {
             <span class="leafygreen-ui-1fku1pu" style="--background-color:#E1F7FF">
              db: &lt;db&gt;, coll: &lt;collection&gt;
             </span>
             },
            </div>
            <div class="line">
             on: "_id"
            </div>
            <div class="line">
             whenMatched: [
            </div>
            <div class="line-block">
             <div class="line">
              { $project: {
             </div>
             <div class="line-block">
              <div class="line">
               value: { $function: {
              </div>
              <div class="line-block">
               <div class="line">
                body:
                <span class="leafygreen-ui-1fku1pu" style="--background-color:#FEF7DB">
                 &lt;reduceFunction&gt;
                </span>
                ,
               </div>
               <div class="line">
                args: [
               </div>
               <div class="line-block">
                <div class="line">
                 "$_id",
                </div>
                <div class="line">
                 [ "$value", "$$new.value" ]
                </div>
               </div>
               <div class="line">
                ],
               </div>
               <div class="line">
                lang: "js"
               </div>
              </div>
              <div class="line">
               } }
              </div>
             </div>
             <div class="line">
              } }
             </div>
            </div>
            <div class="line">
             ]
            </div>
            <div class="line">
             whenNotMatched: "insert"
            </div>
           </div>
           <div class="line">
            } },
           </div>
          </div>
          <div class="line">
           ] )
          </div>
         </div>
        </div>
       </div>
      </td>
     </tr>
     <tr class="leafygreen-ui-15dvrnp">
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         <div class="line-block">
          <div class="line">
           db.collection.mapReduce(
          </div>
          <div class="line-block">
           <div class="line">
            <span class="leafygreen-ui-1fku1pu" style="--background-color:#FFEAE5">
             &lt;mapFunction&gt;
            </span>
            ,
           </div>
           <div class="line">
            <span class="leafygreen-ui-1fku1pu" style="--background-color:#FEF7DB">
             &lt;reduceFunction&gt;
            </span>
            ,
           </div>
           <div class="line">
            {
           </div>
           <div class="line-block">
            <div class="line">
             query: &lt;queryFilter&gt;,
            </div>
            <div class="line">
             sort: &lt;sortOrder&gt;,
            </div>
            <div class="line">
             limit: &lt;number&gt;,
            </div>
            <div class="line">
             finalize:
             <span class="leafygreen-ui-1fku1pu" style="--background-color:#E3FCF7">
              &lt;finalizeFunction&gt;
             </span>
             ,
            </div>
            <div class="line">
             out: { inline: 1 }
            </div>
           </div>
           <div class="line">
            }
           </div>
          </div>
          <div class="line">
           )
          </div>
         </div>
        </div>
       </div>
      </td>
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         <div class="line-block">
          <div class="line">
           db.collection.aggregate( [
          </div>
          <div class="line-block">
           <div class="line">
            { $match: &lt;queryFilter&gt; },
           </div>
           <div class="line">
            { $sort: &lt;sortOrder&gt; },
           </div>
           <div class="line">
            { $limit: &lt;number&gt; },
           </div>
           <div class="line">
            {
            <span class="leafygreen-ui-1fku1pu" style="--background-color:#FFEAE5">
             $project: { emits: { k: &lt;expression&gt;, v: &lt;expression&gt; } }
            </span>
            },
           </div>
           <div class="line">
            {
            <span class="leafygreen-ui-1fku1pu" style="--background-color:#FFEAE5">
             $unwind: "$emits"
            </span>
            },
           </div>
           <div class="line">
            {
            <span class="leafygreen-ui-1fku1pu" style="--background-color:#FEF7DB">
             $group
            </span>
            : {
           </div>
           <div class="line-block">
            <div class="line">
             _id: "$emits.k"},
            </div>
            <div class="line">
             value: {
             <span class="leafygreen-ui-1fku1pu" style="--background-color:#FEF7DB">
              $accumulator
             </span>
             : {
            </div>
            <div class="line-block">
             <div class="line">
              init: &lt;initCode&gt;,
             </div>
             <div class="line">
              accumulate:
              <span class="leafygreen-ui-1fku1pu" style="--background-color:#FEF7DB">
               &lt;reduceFunction&gt;
              </span>
              ,
             </div>
             <div class="line">
              accumulateArgs: [ "$emit.v"],
             </div>
             <div class="line">
              merge:
              <span class="leafygreen-ui-1fku1pu" style="--background-color:#FEF7DB">
               &lt;reduceFunction&gt;
              </span>
              ,
             </div>
             <div class="line">
              finalize:
              <span class="leafygreen-ui-1fku1pu" style="--background-color:#E3FCF7">
               &lt;finalizeFunction&gt;
              </span>
              ,
             </div>
             <div class="line">
              lang: "js" }}
             </div>
            </div>
           </div>
           <div class="line">
            } }
           </div>
          </div>
          <div class="line">
           ] )
          </div>
         </div>
        </div>
       </div>
      </td>
     </tr>
    </tbody>
   </table>
  </div>
  <span class="leafygreen-ui-1qnjd6d" id="std-label-mr-to-agg-examples">
  </span>
 </section>
 <section>
  <h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
   Examples
   <a class="headerlink leafygreen-ui-1kepx47" href="#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="examples">
    </div>
   </a>
  </h2>
  <p class="leafygreen-ui-1kp3ins">
   Various map-reduce expressions can be rewritten using aggregation
pipeline operators, 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, without requiring custom functions. However, for illustrative
purposes, the following examples provide both alternatives.
  </p>
  <span class="leafygreen-ui-1qnjd6d" id="std-label-mr-to-agg-examples1">
  </span>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    Example 1
    <a class="headerlink leafygreen-ui-1kepx47" href="#example-1" 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="example-1">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    The  following map-reduce operation on the
    <code class="leafygreen-ui-1l06pbn">
     orders
    </code>
    collection groups
by the
    <code class="leafygreen-ui-1l06pbn">
     cust_id
    </code>
    , and calculates the sum of the
    <code class="leafygreen-ui-1l06pbn">
     price
    </code>
    for each
    <code class="leafygreen-ui-1l06pbn">
     cust_id
    </code>
    :
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"var mapFunction1 = function() {\n   emit(this.cust_id, this.price);\n};\n\nvar reduceFunction1 = function(keyCustId, valuesPrices) {\n   return Array.sum(valuesPrices);\n};\n\ndb.orders.mapReduce(\n   mapFunction1,\n   reduceFunction1,\n   { out: \"map_reduce_example\" }\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"><span class="lg-highlight-keyword">var</span> mapFunction1 <!-- -->= <span class="lg-highlight-keyword lg-highlight-function">function</span>(<!-- -->)<!-- --> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <span class="lg-highlight-title lg-highlight-function">emit</span>(<span class="lg-highlight-variable lg-highlight-language">this</span>.<span class="lg-highlight-property">cust_id</span>, <span class="lg-highlight-variable lg-highlight-language">this</span>.<span class="lg-highlight-property">price</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-keyword">var</span> reduceFunction1 <!-- -->= <span class="lg-highlight-keyword lg-highlight-function">function</span>(<span class="lg-highlight-params">keyCustId, valuesPrices</span>)<!-- --> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <span class="lg-highlight-keyword">return</span> <span class="lg-highlight-title lg-highlight-class">Array</span>.<span class="lg-highlight-title lg-highlight-function">sum</span>(<!-- -->valuesPrices)<!-- -->;</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">db.<span class="lg-highlight-property">orders</span>.<span class="lg-highlight-title lg-highlight-function">mapReduce</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->mapFunction1<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->reduceFunction1<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">out</span>: <span class="lg-highlight-string">"map_reduce_example"</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">
    <strong>
     Alternative 1: (Recommended)
    </strong>
    You can rewrite the operation into
an aggregation pipeline without translating the map-reduce function
to equivalent pipeline stages:
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.orders.aggregate([\n   { $group: { _id: \"$cust_id\", value: { $sum: \"$price\" } } },\n   { $out: \"agg_alternative_1\" }\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">   <!-- -->{ <span class="lg-highlight-attr">$group</span>: <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-string">"$cust_id"</span>, <span class="lg-highlight-attr">value</span>: <!-- -->{ <span class="lg-highlight-attr">$sum</span>: <span class="lg-highlight-string">"$price"</span> } } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">$out</span>: <span class="lg-highlight-string">"agg_alternative_1"</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">
    <strong>
     Alternative 2: (For illustrative purposes only)
    </strong>
    The
following aggregation pipeline provides a translation of the various
map-reduce functions, using
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/accumulator/#mongodb-group-grp.-accumulator">
     <code class="leafygreen-ui-1l06pbn">
      $accumulator
     </code>
    </a>
    to define custom
functions:
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.orders.aggregate( [\n   { $project: { emit: { key: \"$cust_id\", value: \"$price\" } } },  // equivalent to the map function\n   { $group: {                                                    // equivalent to the reduce function\n         _id: \"$emit.key\",\n         valuesPrices: { $accumulator: {\n                     init: function() { return 0; },\n                     initArgs: [],\n                     accumulate: function(state, value) { return state + value; },\n                     accumulateArgs: [ \"$emit.value\" ],\n                     merge: function(state1, state2) { return state1 + state2; },\n                     lang: \"js\"\n         } }\n   } },\n   { $out: \"agg_alternative_2\" }\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">   <!-- -->{ <span class="lg-highlight-attr">$project</span>: <!-- -->{ <span class="lg-highlight-attr">emit</span>: <!-- -->{ <span class="lg-highlight-attr">key</span>: <span class="lg-highlight-string">"$cust_id"</span>, <span class="lg-highlight-attr">value</span>: <span class="lg-highlight-string">"$price"</span> } } }<!-- -->,  <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> equivalent to the </span><span class="lg-highlight-comment">map function</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">$group</span>: <!-- -->{                                                    <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> equivalent to the </span><span class="lg-highlight-comment">reduce function</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">         <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-string">"$emit.key"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">         <span class="lg-highlight-attr">valuesPrices</span>: <!-- -->{ <span class="lg-highlight-attr">$accumulator</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">                     <span class="lg-highlight-attr">init</span>: <span class="lg-highlight-keyword lg-highlight-function">function</span>(<!-- -->)<!-- --> <!-- -->{ <span class="lg-highlight-keyword">return</span> <span class="lg-highlight-number">0</span>; <!-- -->}<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">                     <span class="lg-highlight-attr">initArgs</span>: <!-- -->[<!-- -->]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">                     <span class="lg-highlight-attr">accumulate</span>: <span class="lg-highlight-keyword lg-highlight-function">function</span>(<span class="lg-highlight-params">state, value</span>)<!-- --> <!-- -->{ <span class="lg-highlight-keyword">return</span> <!-- -->state <!-- -->+ <!-- -->value<!-- -->; <!-- -->}<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">                     <span class="lg-highlight-attr">accumulateArgs</span>: <!-- -->[ <span class="lg-highlight-string">"$emit.value"</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">                     <span class="lg-highlight-attr">merge</span>: <span class="lg-highlight-keyword lg-highlight-function">function</span>(<span class="lg-highlight-params">state1, state2</span>)<!-- --> <!-- -->{ <span class="lg-highlight-keyword">return</span> <!-- -->state1 <!-- -->+ <!-- -->state2<!-- -->; <!-- -->}<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">                     <span class="lg-highlight-attr">lang</span>: <span class="lg-highlight-string">"js"</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">$out</span>: <span class="lg-highlight-string">"agg_alternative_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>
   <ol class="leafygreen-ui-rioki0" type="1">
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      First, the
      <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 outputs documents with an
      <code class="leafygreen-ui-1l06pbn">
       emit
      </code>
      field. The
      <code class="leafygreen-ui-1l06pbn">
       emit
      </code>
      field is a document with the fields:
     </p>
     <ul class="leafygreen-ui-rioki0">
      <li class="leafygreen-ui-d7p7b6">
       <p class="leafygreen-ui-1kp3ins">
        <code class="leafygreen-ui-1l06pbn">
         key
        </code>
        that contains the
        <code class="leafygreen-ui-1l06pbn">
         cust_id
        </code>
        value for the document
       </p>
      </li>
      <li class="leafygreen-ui-d7p7b6">
       <p class="leafygreen-ui-1kp3ins">
        <code class="leafygreen-ui-1l06pbn">
         value
        </code>
        that contains the
        <code class="leafygreen-ui-1l06pbn">
         price
        </code>
        value for the document
       </p>
      </li>
     </ul>
     <script class="structured_data" type="application/ld+json">
      {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : 1, \"emit\" : { \"key\" : \"Ant O. Knee\", \"value\" : 25 } }\n{ \"_id\" : 2, \"emit\" : { \"key\" : \"Ant O. Knee\", \"value\" : 70 } }\n{ \"_id\" : 3, \"emit\" : { \"key\" : \"Busby Bee\", \"value\" : 50 } }\n{ \"_id\" : 4, \"emit\" : { \"key\" : \"Busby Bee\", \"value\" : 25 } }\n{ \"_id\" : 5, \"emit\" : { \"key\" : \"Busby Bee\", \"value\" : 50 } }\n{ \"_id\" : 6, \"emit\" : { \"key\" : \"Cam Elot\", \"value\" : 35 } }\n{ \"_id\" : 7, \"emit\" : { \"key\" : \"Cam Elot\", \"value\" : 25 } }\n{ \"_id\" : 8, \"emit\" : { \"key\" : \"Don Quis\", \"value\" : 75 } }\n{ \"_id\" : 9, \"emit\" : { \"key\" : \"Don Quis\", \"value\" : 55 } }\n{ \"_id\" : 10, \"emit\" : { \"key\" : \"Don Quis\", \"value\" : 25 } }","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">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"emit"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"key"</span> <!-- -->: <span class="lg-highlight-string">"Ant O. Knee"</span>, <span class="lg-highlight-string">"value"</span> <!-- -->: <span class="lg-highlight-number">25</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-string">"emit"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"key"</span> <!-- -->: <span class="lg-highlight-string">"Ant O. Knee"</span>, <span class="lg-highlight-string">"value"</span> <!-- -->: <span class="lg-highlight-number">70</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-string">"emit"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"key"</span> <!-- -->: <span class="lg-highlight-string">"Busby Bee"</span>, <span class="lg-highlight-string">"value"</span> <!-- -->: <span class="lg-highlight-number">50</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">4</span>, <span class="lg-highlight-string">"emit"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"key"</span> <!-- -->: <span class="lg-highlight-string">"Busby Bee"</span>, <span class="lg-highlight-string">"value"</span> <!-- -->: <span class="lg-highlight-number">25</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">5</span>, <span class="lg-highlight-string">"emit"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"key"</span> <!-- -->: <span class="lg-highlight-string">"Busby Bee"</span>, <span class="lg-highlight-string">"value"</span> <!-- -->: <span class="lg-highlight-number">50</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">6</span>, <span class="lg-highlight-string">"emit"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"key"</span> <!-- -->: <span class="lg-highlight-string">"Cam Elot"</span>, <span class="lg-highlight-string">"value"</span> <!-- -->: <span class="lg-highlight-number">35</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">7</span>, <span class="lg-highlight-string">"emit"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"key"</span> <!-- -->: <span class="lg-highlight-string">"Cam Elot"</span>, <span class="lg-highlight-string">"value"</span> <!-- -->: <span class="lg-highlight-number">25</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">8</span>, <span class="lg-highlight-string">"emit"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"key"</span> <!-- -->: <span class="lg-highlight-string">"Don Quis"</span>, <span class="lg-highlight-string">"value"</span> <!-- -->: <span class="lg-highlight-number">75</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">9</span>, <span class="lg-highlight-string">"emit"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"key"</span> <!-- -->: <span class="lg-highlight-string">"Don Quis"</span>, <span class="lg-highlight-string">"value"</span> <!-- -->: <span class="lg-highlight-number">55</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">10</span>, <span class="lg-highlight-string">"emit"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"key"</span> <!-- -->: <span class="lg-highlight-string">"Don Quis"</span>, <span class="lg-highlight-string">"value"</span> <!-- -->: <span class="lg-highlight-number">25</span> } }</td></tr></tbody></table></code></pre>
       </div>
      </div>
     </div>
    </li>
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      Then, 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>
      uses 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>
      operator to add the emitted values:
     </p>
     <script class="structured_data" type="application/ld+json">
      {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : \"Don Quis\", \"valuesPrices\" : 155 }\n{ \"_id\" : \"Cam Elot\", \"valuesPrices\" : 60 }\n{ \"_id\" : \"Ant O. Knee\", \"valuesPrices\" : 95 }\n{ \"_id\" : \"Busby Bee\", \"valuesPrices\" : 125 }","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">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"Don Quis"</span>, <span class="lg-highlight-string">"valuesPrices"</span> <!-- -->: <span class="lg-highlight-number">155</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"Cam Elot"</span>, <span class="lg-highlight-string">"valuesPrices"</span> <!-- -->: <span class="lg-highlight-number">60</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"Ant O. Knee"</span>, <span class="lg-highlight-string">"valuesPrices"</span> <!-- -->: <span class="lg-highlight-number">95</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"Busby Bee"</span>, <span class="lg-highlight-string">"valuesPrices"</span> <!-- -->: <span class="lg-highlight-number">125</span> }</td></tr></tbody></table></code></pre>
       </div>
      </div>
     </div>
    </li>
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      Finally, the
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/out/#mongodb-pipeline-pipe.-out">
       <code class="leafygreen-ui-1l06pbn">
        $out
       </code>
      </a>
      writes the output to the collection
      <code class="leafygreen-ui-1l06pbn">
       agg_alternative_2
      </code>
      . Alternatively, you could use
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/merge/#mongodb-pipeline-pipe.-merge">
       <code class="leafygreen-ui-1l06pbn">
        $merge
       </code>
      </a>
      instead of
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/out/#mongodb-pipeline-pipe.-out">
       <code class="leafygreen-ui-1l06pbn">
        $out
       </code>
       .
      </a>
     </p>
    </li>
   </ol>
   <span class="leafygreen-ui-1qnjd6d" id="std-label-mr-to-agg-examples2">
   </span>
  </section>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    Example 2
    <a class="headerlink leafygreen-ui-1kepx47" href="#example-2" 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="example-2">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    The following map-reduce operation on the
    <code class="leafygreen-ui-1l06pbn">
     orders
    </code>
    collection
groups by the
    <code class="leafygreen-ui-1l06pbn">
     item.sku
    </code>
    field and calculates the number of
orders and the total quantity ordered for each sku. The operation
then calculates the average quantity per order for each sku value
and merges the results into the output collection.
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"var mapFunction2 = function() {\n      for (var idx = 0; idx &lt; this.items.length; idx++) {\n         var key = this.items[idx].sku;\n         var value = { count: 1, qty: this.items[idx].qty };\n\n         emit(key, value);\n      }\n};\n\nvar reduceFunction2 = function(keySKU, countObjVals) {\n   reducedVal = { count: 0, qty: 0 };\n\n   for (var idx = 0; idx &lt; countObjVals.length; idx++) {\n         reducedVal.count += countObjVals[idx].count;\n         reducedVal.qty += countObjVals[idx].qty;\n   }\n\n   return reducedVal;\n};\n\nvar finalizeFunction2 = function (key, reducedVal) {\n   reducedVal.avg = reducedVal.qty/reducedVal.count;\n   return reducedVal;\n};\n\ndb.orders.mapReduce(\n   mapFunction2,\n   reduceFunction2,\n   {\n      out: { merge: \"map_reduce_example2\" },\n      query: { ord_date: { $gte: new Date(\"2020-03-01\") } },\n      finalize: finalizeFunction2\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"><span class="lg-highlight-keyword">var</span> mapFunction2 <!-- -->= <span class="lg-highlight-keyword lg-highlight-function">function</span>(<!-- -->)<!-- --> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <span class="lg-highlight-keyword">for</span> <!-- -->(<span class="lg-highlight-keyword">var</span> idx <!-- -->= <span class="lg-highlight-number">0</span>; <!-- -->idx <!-- -->&lt; <span class="lg-highlight-variable lg-highlight-language">this</span>.<span class="lg-highlight-property">items</span>.<span class="lg-highlight-property">length</span>; <!-- -->idx<!-- -->+<!-- -->+<!-- -->) <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">         <span class="lg-highlight-keyword">var</span> key <!-- -->= <span class="lg-highlight-variable lg-highlight-language">this</span>.<span class="lg-highlight-property">items</span>[<!-- -->idx].<span class="lg-highlight-property">sku</span>;</td></tr><tr class=""><td class="leafygreen-ui-7razhx">         <span class="lg-highlight-keyword">var</span> value <!-- -->= <!-- -->{ <span class="lg-highlight-attr">count</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">qty</span>: <span class="lg-highlight-variable lg-highlight-language">this</span>.<span class="lg-highlight-property">items</span>[<!-- -->idx].<span class="lg-highlight-property">qty</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-title lg-highlight-function">emit</span>(<!-- -->key<!-- -->, <!-- -->value)<!-- -->;</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-keyword">var</span> reduceFunction2 <!-- -->= <span class="lg-highlight-keyword lg-highlight-function">function</span>(<span class="lg-highlight-params">keySKU, countObjVals</span>)<!-- --> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->reducedVal <!-- -->= <!-- -->{ <span class="lg-highlight-attr">count</span>: <span class="lg-highlight-number">0</span>, <span class="lg-highlight-attr">qty</span>: <span class="lg-highlight-number">0</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-keyword">for</span> <!-- -->(<span class="lg-highlight-keyword">var</span> idx <!-- -->= <span class="lg-highlight-number">0</span>; <!-- -->idx <!-- -->&lt; <!-- -->countObjVals.<span class="lg-highlight-property">length</span>; <!-- -->idx<!-- -->+<!-- -->+<!-- -->) <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">         <!-- -->reducedVal.<span class="lg-highlight-property">count</span> <!-- -->+<!-- -->= <!-- -->countObjVals<!-- -->[<!-- -->idx].<span class="lg-highlight-property">count</span>;</td></tr><tr class=""><td class="leafygreen-ui-7razhx">         <!-- -->reducedVal.<span class="lg-highlight-property">qty</span> <!-- -->+<!-- -->= <!-- -->countObjVals<!-- -->[<!-- -->idx].<span class="lg-highlight-property">qty</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-keyword">return</span> <!-- -->reducedVal<!-- -->;</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-keyword">var</span> finalizeFunction2 <!-- -->= <span class="lg-highlight-keyword lg-highlight-function">function</span> <!-- -->(<span class="lg-highlight-params">key, reducedVal</span>)<!-- --> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->reducedVal.<span class="lg-highlight-property">avg</span> <!-- -->= <!-- -->reducedVal.<span class="lg-highlight-property">qty</span>/<!-- -->reducedVal.<span class="lg-highlight-property">count</span>;</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <span class="lg-highlight-keyword">return</span> <!-- -->reducedVal<!-- -->;</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">db.<span class="lg-highlight-property">orders</span>.<span class="lg-highlight-title lg-highlight-function">mapReduce</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->mapFunction2<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->reduceFunction2<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <span class="lg-highlight-attr">out</span>: <!-- -->{ <span class="lg-highlight-attr">merge</span>: <span class="lg-highlight-string">"map_reduce_example2"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <span class="lg-highlight-attr">query</span>: <!-- -->{ <span class="lg-highlight-attr">ord_date</span>: <!-- -->{ <span class="lg-highlight-attr">$gte</span>: <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">Date</span>(<span class="lg-highlight-string">"2020-03-01"</span>) } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <span class="lg-highlight-attr">finalize</span>: <!-- -->finalizeFunction2</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">
    <strong>
     Alternative 1: (Recommended)
    </strong>
    You can rewrite the operation into
an aggregation pipeline without translating the map-reduce function
to equivalent pipeline stages:
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.orders.aggregate( [\n   { $match: { ord_date: { $gte: new Date(\"2020-03-01\") } } },\n   { $unwind: \"$items\" },\n   { $group: { _id: \"$items.sku\", qty: { $sum: \"$items.qty\" }, orders_ids: { $addToSet: \"$_id\" } }  },\n   { $project: { value: { count: { $size: \"$orders_ids\" }, qty: \"$qty\", avg: { $divide: [ \"$qty\", { $size: \"$orders_ids\" } ] } } } },\n   { $merge: { into: \"agg_alternative_3\", on: \"_id\", whenMatched: \"replace\",  whenNotMatched: \"insert\" } }\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">   <!-- -->{ <span class="lg-highlight-attr">$match</span>: <!-- -->{ <span class="lg-highlight-attr">ord_date</span>: <!-- -->{ <span class="lg-highlight-attr">$gte</span>: <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">Date</span>(<span class="lg-highlight-string">"2020-03-01"</span>) } } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">$unwind</span>: <span class="lg-highlight-string">"$items"</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">"$items.sku"</span>, <span class="lg-highlight-attr">qty</span>: <!-- -->{ <span class="lg-highlight-attr">$sum</span>: <span class="lg-highlight-string">"$items.qty"</span> }<!-- -->, <span class="lg-highlight-attr">orders_ids</span>: <!-- -->{ <span class="lg-highlight-attr">$addToSet</span>: <span class="lg-highlight-string">"$_id"</span> } }  }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">$project</span>: <!-- -->{ <span class="lg-highlight-attr">value</span>: <!-- -->{ <span class="lg-highlight-attr">count</span>: <!-- -->{ <span class="lg-highlight-attr">$size</span>: <span class="lg-highlight-string">"$orders_ids"</span> }<!-- -->, <span class="lg-highlight-attr">qty</span>: <span class="lg-highlight-string">"$qty"</span>, <span class="lg-highlight-attr">avg</span>: <!-- -->{ <span class="lg-highlight-attr">$divide</span>: <!-- -->[ <span class="lg-highlight-string">"$qty"</span>, <!-- -->{ <span class="lg-highlight-attr">$size</span>: <span class="lg-highlight-string">"$orders_ids"</span> } ] } } } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">$merge</span>: <!-- -->{ <span class="lg-highlight-attr">into</span>: <span class="lg-highlight-string">"agg_alternative_3"</span>, <span class="lg-highlight-attr">on</span>: <span class="lg-highlight-string">"_id"</span>, <span class="lg-highlight-attr">whenMatched</span>: <span class="lg-highlight-string">"replace"</span>,  <span class="lg-highlight-attr">whenNotMatched</span>: <span class="lg-highlight-string">"insert"</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">
    <strong>
     Alternative 2: (For illustrative purposes only)
    </strong>
    The following
aggregation pipeline provides a translation of the various
map-reduce functions, using
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/accumulator/#mongodb-group-grp.-accumulator">
     <code class="leafygreen-ui-1l06pbn">
      $accumulator
     </code>
    </a>
    to define custom
functions:
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.orders.aggregate( [\n      { $match: { ord_date: {$gte: new Date(\"2020-03-01\") } } },\n      { $unwind: \"$items\" },\n      { $project: { emit: { key: \"$items.sku\", value: { count: { $literal: 1 }, qty: \"$items.qty\" } } } },\n      { $group: {\n            _id: \"$emit.key\",\n            value: { $accumulator: {\n               init: function() { return { count: 0, qty: 0 }; },\n               initArgs: [],\n               accumulate: function(state, value) {\n                  state.count += value.count;\n                  state.qty += value.qty;\n                  return state;\n               },\n               accumulateArgs: [ \"$emit.value\" ],\n               merge: function(state1, state2) {\n                  return { count: state1.count + state2.count, qty: state1.qty + state2.qty };\n               },\n               finalize: function(state) {\n                  state.avg = state.qty / state.count;\n                  return state;\n               },\n               lang: \"js\"}\n            }\n      } },\n      { $merge: {\n         into: \"agg_alternative_4\",\n         on: \"_id\",\n         whenMatched: \"replace\",\n         whenNotMatched: \"insert\"\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">      <!-- -->{ <span class="lg-highlight-attr">$match</span>: <!-- -->{ <span class="lg-highlight-attr">ord_date</span>: <!-- -->{<span class="lg-highlight-attr">$gte</span>: <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">Date</span>(<span class="lg-highlight-string">"2020-03-01"</span>) } } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <!-- -->{ <span class="lg-highlight-attr">$unwind</span>: <span class="lg-highlight-string">"$items"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <!-- -->{ <span class="lg-highlight-attr">$project</span>: <!-- -->{ <span class="lg-highlight-attr">emit</span>: <!-- -->{ <span class="lg-highlight-attr">key</span>: <span class="lg-highlight-string">"$items.sku"</span>, <span class="lg-highlight-attr">value</span>: <!-- -->{ <span class="lg-highlight-attr">count</span>: <!-- -->{ <span class="lg-highlight-attr">$literal</span>: <span class="lg-highlight-number">1</span> }<!-- -->, <span class="lg-highlight-attr">qty</span>: <span class="lg-highlight-string">"$items.qty"</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">            <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-string">"$emit.key"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">            <span class="lg-highlight-attr">value</span>: <!-- -->{ <span class="lg-highlight-attr">$accumulator</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">               <span class="lg-highlight-attr">init</span>: <span class="lg-highlight-keyword lg-highlight-function">function</span>(<!-- -->)<!-- --> <!-- -->{ <span class="lg-highlight-keyword">return</span> <!-- -->{ <span class="lg-highlight-attr">count</span>: <span class="lg-highlight-number">0</span>, <span class="lg-highlight-attr">qty</span>: <span class="lg-highlight-number">0</span> }<!-- -->; <!-- -->}<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">               <span class="lg-highlight-attr">initArgs</span>: <!-- -->[<!-- -->]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">               <span class="lg-highlight-attr">accumulate</span>: <span class="lg-highlight-keyword lg-highlight-function">function</span>(<span class="lg-highlight-params">state, value</span>)<!-- --> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">                  <!-- -->state.<span class="lg-highlight-property">count</span> <!-- -->+<!-- -->= <!-- -->value.<span class="lg-highlight-property">count</span>;</td></tr><tr class=""><td class="leafygreen-ui-7razhx">                  <!-- -->state.<span class="lg-highlight-property">qty</span> <!-- -->+<!-- -->= <!-- -->value.<span class="lg-highlight-property">qty</span>;</td></tr><tr class=""><td class="leafygreen-ui-7razhx">                  <span class="lg-highlight-keyword">return</span> <!-- -->state<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx">               <!-- -->}<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">               <span class="lg-highlight-attr">accumulateArgs</span>: <!-- -->[ <span class="lg-highlight-string">"$emit.value"</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">               <span class="lg-highlight-attr">merge</span>: <span class="lg-highlight-keyword lg-highlight-function">function</span>(<span class="lg-highlight-params">state1, state2</span>)<!-- --> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">                  <span class="lg-highlight-keyword">return</span> <!-- -->{ <span class="lg-highlight-attr">count</span>: <!-- -->state1.<span class="lg-highlight-property">count</span> <!-- -->+ <!-- -->state2.<span class="lg-highlight-property">count</span>, <span class="lg-highlight-attr">qty</span>: <!-- -->state1.<span class="lg-highlight-property">qty</span> <!-- -->+ <!-- -->state2.<span class="lg-highlight-property">qty</span> }<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx">               <!-- -->}<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">               <span class="lg-highlight-attr">finalize</span>: <span class="lg-highlight-keyword lg-highlight-function">function</span>(<span class="lg-highlight-params">state</span>)<!-- --> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">                  <!-- -->state.<span class="lg-highlight-property">avg</span> <!-- -->= <!-- -->state.<span class="lg-highlight-property">qty</span> <!-- -->/ <!-- -->state.<span class="lg-highlight-property">count</span>;</td></tr><tr class=""><td class="leafygreen-ui-7razhx">                  <span class="lg-highlight-keyword">return</span> <!-- -->state<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx">               <!-- -->}<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">               <span class="lg-highlight-attr">lang</span>: <span class="lg-highlight-string">"js"</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">$merge</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">         <span class="lg-highlight-attr">into</span>: <span class="lg-highlight-string">"agg_alternative_4"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">         <span class="lg-highlight-attr">on</span>: <span class="lg-highlight-string">"_id"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">         <span class="lg-highlight-attr">whenMatched</span>: <span class="lg-highlight-string">"replace"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">         <span class="lg-highlight-attr">whenNotMatched</span>: <span class="lg-highlight-string">"insert"</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>
   <ol class="leafygreen-ui-rioki0" type="1">
    <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 selects only those
documents with
      <code class="leafygreen-ui-1l06pbn">
       ord_date
      </code>
      greater than or equal to
      <code class="leafygreen-ui-1l06pbn">
       new
Date("2020-03-01")
      </code>
      .
     </p>
    </li>
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      The
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/unwind/#mongodb-pipeline-pipe.-unwind">
       <code class="leafygreen-ui-1l06pbn">
        $unwind
       </code>
      </a>
      stage breaks down the document by
the
      <code class="leafygreen-ui-1l06pbn">
       items
      </code>
      array field to output a document for each array
element. For example:
     </p>
     <script class="structured_data" type="application/ld+json">
      {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : 1, \"cust_id\" : \"Ant O. Knee\", \"ord_date\" : ISODate(\"2020-03-01T00:00:00Z\"), \"price\" : 25, \"items\" : { \"sku\" : \"oranges\", \"qty\" : 5, \"price\" : 2.5 }, \"status\" : \"A\" }\n{ \"_id\" : 1, \"cust_id\" : \"Ant O. Knee\", \"ord_date\" : ISODate(\"2020-03-01T00:00:00Z\"), \"price\" : 25, \"items\" : { \"sku\" : \"apples\", \"qty\" : 5, \"price\" : 2.5 }, \"status\" : \"A\" }\n{ \"_id\" : 2, \"cust_id\" : \"Ant O. Knee\", \"ord_date\" : ISODate(\"2020-03-08T00:00:00Z\"), \"price\" : 70, \"items\" : { \"sku\" : \"oranges\", \"qty\" : 8, \"price\" : 2.5 }, \"status\" : \"A\" }\n{ \"_id\" : 2, \"cust_id\" : \"Ant O. Knee\", \"ord_date\" : ISODate(\"2020-03-08T00:00:00Z\"), \"price\" : 70, \"items\" : { \"sku\" : \"chocolates\", \"qty\" : 5, \"price\" : 10 }, \"status\" : \"A\" }\n{ \"_id\" : 3, \"cust_id\" : \"Busby Bee\", \"ord_date\" : ISODate(\"2020-03-08T00:00:00Z\"), \"price\" : 50, \"items\" : { \"sku\" : \"oranges\", \"qty\" : 10, \"price\" : 2.5 }, \"status\" : \"A\" }\n{ \"_id\" : 3, \"cust_id\" : \"Busby Bee\", \"ord_date\" : ISODate(\"2020-03-08T00:00:00Z\"), \"price\" : 50, \"items\" : { \"sku\" : \"pears\", \"qty\" : 10, \"price\" : 2.5 }, \"status\" : \"A\" }\n{ \"_id\" : 4, \"cust_id\" : \"Busby Bee\", \"ord_date\" : ISODate(\"2020-03-18T00:00:00Z\"), \"price\" : 25, \"items\" : { \"sku\" : \"oranges\", \"qty\" : 10, \"price\" : 2.5 }, \"status\" : \"A\" }\n{ \"_id\" : 5, \"cust_id\" : \"Busby Bee\", \"ord_date\" : ISODate(\"2020-03-19T00:00:00Z\"), \"price\" : 50, \"items\" : { \"sku\" : \"chocolates\", \"qty\" : 5, \"price\" : 10 }, \"status\" : \"A\" }\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">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"cust_id"</span> <!-- -->: <span class="lg-highlight-string">"Ant O. Knee"</span>, <span class="lg-highlight-string">"ord_date"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2020-03-01T00:00:00Z"</span>)<!-- -->, <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-number">25</span>, <span class="lg-highlight-string">"items"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"sku"</span> <!-- -->: <span class="lg-highlight-string">"oranges"</span>, <span class="lg-highlight-string">"qty"</span> <!-- -->: <span class="lg-highlight-number">5</span>, <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-number">2.5</span> }<!-- -->, <span class="lg-highlight-string">"status"</span> <!-- -->: <span class="lg-highlight-string">"A"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"cust_id"</span> <!-- -->: <span class="lg-highlight-string">"Ant O. Knee"</span>, <span class="lg-highlight-string">"ord_date"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2020-03-01T00:00:00Z"</span>)<!-- -->, <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-number">25</span>, <span class="lg-highlight-string">"items"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"sku"</span> <!-- -->: <span class="lg-highlight-string">"apples"</span>, <span class="lg-highlight-string">"qty"</span> <!-- -->: <span class="lg-highlight-number">5</span>, <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-number">2.5</span> }<!-- -->, <span class="lg-highlight-string">"status"</span> <!-- -->: <span class="lg-highlight-string">"A"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-string">"cust_id"</span> <!-- -->: <span class="lg-highlight-string">"Ant O. Knee"</span>, <span class="lg-highlight-string">"ord_date"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2020-03-08T00:00:00Z"</span>)<!-- -->, <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-number">70</span>, <span class="lg-highlight-string">"items"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"sku"</span> <!-- -->: <span class="lg-highlight-string">"oranges"</span>, <span class="lg-highlight-string">"qty"</span> <!-- -->: <span class="lg-highlight-number">8</span>, <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-number">2.5</span> }<!-- -->, <span class="lg-highlight-string">"status"</span> <!-- -->: <span class="lg-highlight-string">"A"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-string">"cust_id"</span> <!-- -->: <span class="lg-highlight-string">"Ant O. Knee"</span>, <span class="lg-highlight-string">"ord_date"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2020-03-08T00:00:00Z"</span>)<!-- -->, <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-number">70</span>, <span class="lg-highlight-string">"items"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"sku"</span> <!-- -->: <span class="lg-highlight-string">"chocolates"</span>, <span class="lg-highlight-string">"qty"</span> <!-- -->: <span class="lg-highlight-number">5</span>, <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-number">10</span> }<!-- -->, <span class="lg-highlight-string">"status"</span> <!-- -->: <span class="lg-highlight-string">"A"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-string">"cust_id"</span> <!-- -->: <span class="lg-highlight-string">"Busby Bee"</span>, <span class="lg-highlight-string">"ord_date"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2020-03-08T00:00:00Z"</span>)<!-- -->, <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-number">50</span>, <span class="lg-highlight-string">"items"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"sku"</span> <!-- -->: <span class="lg-highlight-string">"oranges"</span>, <span class="lg-highlight-string">"qty"</span> <!-- -->: <span class="lg-highlight-number">10</span>, <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-number">2.5</span> }<!-- -->, <span class="lg-highlight-string">"status"</span> <!-- -->: <span class="lg-highlight-string">"A"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-string">"cust_id"</span> <!-- -->: <span class="lg-highlight-string">"Busby Bee"</span>, <span class="lg-highlight-string">"ord_date"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2020-03-08T00:00:00Z"</span>)<!-- -->, <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-number">50</span>, <span class="lg-highlight-string">"items"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"sku"</span> <!-- -->: <span class="lg-highlight-string">"pears"</span>, <span class="lg-highlight-string">"qty"</span> <!-- -->: <span class="lg-highlight-number">10</span>, <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-number">2.5</span> }<!-- -->, <span class="lg-highlight-string">"status"</span> <!-- -->: <span class="lg-highlight-string">"A"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">4</span>, <span class="lg-highlight-string">"cust_id"</span> <!-- -->: <span class="lg-highlight-string">"Busby Bee"</span>, <span class="lg-highlight-string">"ord_date"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2020-03-18T00:00:00Z"</span>)<!-- -->, <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-number">25</span>, <span class="lg-highlight-string">"items"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"sku"</span> <!-- -->: <span class="lg-highlight-string">"oranges"</span>, <span class="lg-highlight-string">"qty"</span> <!-- -->: <span class="lg-highlight-number">10</span>, <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-number">2.5</span> }<!-- -->, <span class="lg-highlight-string">"status"</span> <!-- -->: <span class="lg-highlight-string">"A"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">5</span>, <span class="lg-highlight-string">"cust_id"</span> <!-- -->: <span class="lg-highlight-string">"Busby Bee"</span>, <span class="lg-highlight-string">"ord_date"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2020-03-19T00:00:00Z"</span>)<!-- -->, <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-number">50</span>, <span class="lg-highlight-string">"items"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"sku"</span> <!-- -->: <span class="lg-highlight-string">"chocolates"</span>, <span class="lg-highlight-string">"qty"</span> <!-- -->: <span class="lg-highlight-number">5</span>, <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-number">10</span> }<!-- -->, <span class="lg-highlight-string">"status"</span> <!-- -->: <span class="lg-highlight-string">"A"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">...</td></tr></tbody></table></code></pre>
       </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/project/#mongodb-pipeline-pipe.-project">
       <code class="leafygreen-ui-1l06pbn">
        $project
       </code>
      </a>
      stage outputs documents with an
      <code class="leafygreen-ui-1l06pbn">
       emit
      </code>
      field. The
      <code class="leafygreen-ui-1l06pbn">
       emit
      </code>
      field is a document with the fields:
     </p>
     <ul class="leafygreen-ui-rioki0">
      <li class="leafygreen-ui-d7p7b6">
       <p class="leafygreen-ui-1kp3ins">
        <code class="leafygreen-ui-1l06pbn">
         key
        </code>
        that contains the
        <code class="leafygreen-ui-1l06pbn">
         items.sku
        </code>
        value
       </p>
      </li>
      <li class="leafygreen-ui-d7p7b6">
       <p class="leafygreen-ui-1kp3ins">
        <code class="leafygreen-ui-1l06pbn">
         value
        </code>
        that contains a document with the
        <code class="leafygreen-ui-1l06pbn">
         qty
        </code>
        value and a
        <code class="leafygreen-ui-1l06pbn">
         count
        </code>
        value
       </p>
      </li>
     </ul>
     <script class="structured_data" type="application/ld+json">
      {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : 1, \"emit\" : { \"key\" : \"oranges\", \"value\" : { \"count\" : 1, \"qty\" : 5 } } }\n{ \"_id\" : 1, \"emit\" : { \"key\" : \"apples\", \"value\" : { \"count\" : 1, \"qty\" : 5 } } }\n{ \"_id\" : 2, \"emit\" : { \"key\" : \"oranges\", \"value\" : { \"count\" : 1, \"qty\" : 8 } } }\n{ \"_id\" : 2, \"emit\" : { \"key\" : \"chocolates\", \"value\" : { \"count\" : 1, \"qty\" : 5 } } }\n{ \"_id\" : 3, \"emit\" : { \"key\" : \"oranges\", \"value\" : { \"count\" : 1, \"qty\" : 10 } } }\n{ \"_id\" : 3, \"emit\" : { \"key\" : \"pears\", \"value\" : { \"count\" : 1, \"qty\" : 10 } } }\n{ \"_id\" : 4, \"emit\" : { \"key\" : \"oranges\", \"value\" : { \"count\" : 1, \"qty\" : 10 } } }\n{ \"_id\" : 5, \"emit\" : { \"key\" : \"chocolates\", \"value\" : { \"count\" : 1, \"qty\" : 5 } } }\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">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"emit"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"key"</span> <!-- -->: <span class="lg-highlight-string">"oranges"</span>, <span class="lg-highlight-string">"value"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"count"</span> <!-- -->: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"qty"</span> <!-- -->: <span class="lg-highlight-number">5</span> } } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"emit"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"key"</span> <!-- -->: <span class="lg-highlight-string">"apples"</span>, <span class="lg-highlight-string">"value"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"count"</span> <!-- -->: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"qty"</span> <!-- -->: <span class="lg-highlight-number">5</span> } } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-string">"emit"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"key"</span> <!-- -->: <span class="lg-highlight-string">"oranges"</span>, <span class="lg-highlight-string">"value"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"count"</span> <!-- -->: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"qty"</span> <!-- -->: <span class="lg-highlight-number">8</span> } } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-string">"emit"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"key"</span> <!-- -->: <span class="lg-highlight-string">"chocolates"</span>, <span class="lg-highlight-string">"value"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"count"</span> <!-- -->: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"qty"</span> <!-- -->: <span class="lg-highlight-number">5</span> } } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-string">"emit"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"key"</span> <!-- -->: <span class="lg-highlight-string">"oranges"</span>, <span class="lg-highlight-string">"value"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"count"</span> <!-- -->: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"qty"</span> <!-- -->: <span class="lg-highlight-number">10</span> } } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-string">"emit"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"key"</span> <!-- -->: <span class="lg-highlight-string">"pears"</span>, <span class="lg-highlight-string">"value"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"count"</span> <!-- -->: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"qty"</span> <!-- -->: <span class="lg-highlight-number">10</span> } } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">4</span>, <span class="lg-highlight-string">"emit"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"key"</span> <!-- -->: <span class="lg-highlight-string">"oranges"</span>, <span class="lg-highlight-string">"value"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"count"</span> <!-- -->: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"qty"</span> <!-- -->: <span class="lg-highlight-number">10</span> } } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">5</span>, <span class="lg-highlight-string">"emit"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"key"</span> <!-- -->: <span class="lg-highlight-string">"chocolates"</span>, <span class="lg-highlight-string">"value"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"count"</span> <!-- -->: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"qty"</span> <!-- -->: <span class="lg-highlight-number">5</span> } } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">...</td></tr></tbody></table></code></pre>
       </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/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/accumulator/#mongodb-group-grp.-accumulator">
       <code class="leafygreen-ui-1l06pbn">
        $accumulator
       </code>
      </a>
      operator to add the emitted
      <code class="leafygreen-ui-1l06pbn">
       count
      </code>
      and
      <code class="leafygreen-ui-1l06pbn">
       qty
      </code>
      and calculate the
      <code class="leafygreen-ui-1l06pbn">
       avg
      </code>
      field:
     </p>
     <script class="structured_data" type="application/ld+json">
      {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : \"chocolates\", \"value\" : { \"count\" : 3, \"qty\" : 15, \"avg\" : 5 } }\n{ \"_id\" : \"oranges\", \"value\" : { \"count\" : 7, \"qty\" : 63, \"avg\" : 9 } }\n{ \"_id\" : \"carrots\", \"value\" : { \"count\" : 2, \"qty\" : 15, \"avg\" : 7.5 } }\n{ \"_id\" : \"apples\", \"value\" : { \"count\" : 4, \"qty\" : 35, \"avg\" : 8.75 } }\n{ \"_id\" : \"pears\", \"value\" : { \"count\" : 1, \"qty\" : 10, \"avg\" : 10 } }","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">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"chocolates"</span>, <span class="lg-highlight-string">"value"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"count"</span> <!-- -->: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-string">"qty"</span> <!-- -->: <span class="lg-highlight-number">15</span>, <span class="lg-highlight-string">"avg"</span> <!-- -->: <span class="lg-highlight-number">5</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"oranges"</span>, <span class="lg-highlight-string">"value"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"count"</span> <!-- -->: <span class="lg-highlight-number">7</span>, <span class="lg-highlight-string">"qty"</span> <!-- -->: <span class="lg-highlight-number">63</span>, <span class="lg-highlight-string">"avg"</span> <!-- -->: <span class="lg-highlight-number">9</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"carrots"</span>, <span class="lg-highlight-string">"value"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"count"</span> <!-- -->: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-string">"qty"</span> <!-- -->: <span class="lg-highlight-number">15</span>, <span class="lg-highlight-string">"avg"</span> <!-- -->: <span class="lg-highlight-number">7.5</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"apples"</span>, <span class="lg-highlight-string">"value"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"count"</span> <!-- -->: <span class="lg-highlight-number">4</span>, <span class="lg-highlight-string">"qty"</span> <!-- -->: <span class="lg-highlight-number">35</span>, <span class="lg-highlight-string">"avg"</span> <!-- -->: <span class="lg-highlight-number">8.75</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"pears"</span>, <span class="lg-highlight-string">"value"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"count"</span> <!-- -->: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"qty"</span> <!-- -->: <span class="lg-highlight-number">10</span>, <span class="lg-highlight-string">"avg"</span> <!-- -->: <span class="lg-highlight-number">10</span> } }</td></tr></tbody></table></code></pre>
       </div>
      </div>
     </div>
    </li>
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      Finally, the
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/merge/#mongodb-pipeline-pipe.-merge">
       <code class="leafygreen-ui-1l06pbn">
        $merge
       </code>
      </a>
      writes the output to the
collection
      <code class="leafygreen-ui-1l06pbn">
       agg_alternative_4
      </code>
      . If an existing document has the same
key
      <code class="leafygreen-ui-1l06pbn">
       _id
      </code>
      as the new result, the operation overwrites the existing
document. If there is no existing document with the same key, the
operation inserts the document.
     </p>
    </li>
   </ol>
   <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">
     <p class="leafygreen-ui-1kp3ins">
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/aggregation-commands-comparison/">
       Aggregation Commands Comparison
      </a>
     </p>
    </div>
   </div>
  </section>
 </section>
</section>
