<section>
 <h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
  Perform Incremental Map-Reduce
  <a class="headerlink leafygreen-ui-1kepx47" href="#perform-incremental-map-reduce" title="Permalink to this heading">
   <svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
    <path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
    </path>
    <path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
    </path>
   </svg>
   <div class="css-1btu0w3 evvkmar0" id="perform-incremental-map-reduce">
   </div>
  </a>
 </h1>
 <div class="leafygreen-ui-a5yxub">
  <p class="css-1cmx2d4 ef7gu8j0">
   On this page
  </p>
  <ul class="css-1qb0mst ef7gu8j1">
   <li class="leafygreen-ui-1e7a0ei">
    <a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#data-setup" target="_self">
     <span>
      Data Setup
     </span>
    </a>
   </li>
   <li class="leafygreen-ui-1jqtqlb">
    <a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#initial-map-reduce-of-current-collection" target="_self">
     <span>
      Initial Map-Reduce of Current Collection
     </span>
    </a>
   </li>
   <li class="leafygreen-ui-1jqtqlb">
    <a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#subsequent-incremental-map-reduce" target="_self">
     <span>
      Subsequent Incremental Map-Reduce
     </span>
    </a>
   </li>
   <li class="leafygreen-ui-1jqtqlb">
    <a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#aggregation-alternative" target="_self">
     <span>
      Aggregation Alternative
     </span>
    </a>
   </li>
  </ul>
 </div>
 <div class="leafygreen-ui-15mdk4" role="note">
  <h2 class="leafygreen-ui-6n7b7d">
   Note
  </h2>
  <h3 class="leafygreen-ui-144d5kf">
   <strong>
    Aggregation Pipeline as Alternative
   </strong>
  </h3>
  <div class="leafygreen-ui-9mycqc">
   <p class="leafygreen-ui-1kp3ins">
    Starting in MongoDB 5.0,
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/map-reduce/#std-label-map-reduce">
     map-reduce
    </a>
    is
deprecated:
   </p>
   <ul class="leafygreen-ui-rioki0">
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      Instead of
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/map-reduce/#std-label-map-reduce">
       map-reduce
      </a>
      , you should use an
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/aggregation-pipeline/#std-label-aggregation-pipeline">
       aggregation pipeline
      </a>
      . Aggregation
pipelines provide better performance and usability than map-reduce.
     </p>
    </li>
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      You can rewrite map-reduce operations using
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation-pipeline/#std-label-aggregation-pipeline-operator-reference">
       aggregation pipeline
stages
      </a>
      , such as
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/group/#mongodb-pipeline-pipe.-group">
       <code class="leafygreen-ui-1l06pbn">
        $group
       </code>
      </a>
      ,
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/merge/#mongodb-pipeline-pipe.-merge">
       <code class="leafygreen-ui-1l06pbn">
        $merge
       </code>
      </a>
      , and others.
     </p>
    </li>
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      For map-reduce operations that require custom functionality, you can
use the
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/accumulator/#mongodb-group-grp.-accumulator">
       <code class="leafygreen-ui-1l06pbn">
        $accumulator
       </code>
      </a>
      and
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/function/#mongodb-expression-exp.-function">
       <code class="leafygreen-ui-1l06pbn">
        $function
       </code>
      </a>
      aggregation
operators. You can use those
operators to define custom aggregation expressions in JavaScript.
     </p>
    </li>
   </ul>
   <p class="leafygreen-ui-1kp3ins">
    For examples of aggregation pipeline alternatives to map-reduce, see:
   </p>
   <ul class="leafygreen-ui-rioki0">
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/map-reduce-to-aggregation-pipeline/">
       Map-Reduce to Aggregation Pipeline
      </a>
     </p>
    </li>
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/map-reduce-examples/">
       Map-Reduce Examples
      </a>
     </p>
    </li>
   </ul>
   <p class="leafygreen-ui-1kp3ins">
    This section has an example aggregation pipeline alternative to
map-reduce that does not use a custom function. For an example that
uses a custom function, see
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/map-reduce-to-aggregation-pipeline/">
     Map-Reduce to Aggregation Pipeline
     <!-- -->
     .
    </a>
   </p>
  </div>
 </div>
 <p class="leafygreen-ui-1kp3ins">
  To perform map-reduce operations, MongoDB provides the
  <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/mapReduce/#mongodb-dbcommand-dbcmd.mapReduce">
   <code class="leafygreen-ui-1l06pbn">
    mapReduce
   </code>
  </a>
  command and, in
  <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
   <span>
    <code class="leafygreen-ui-1l06pbn">
     mongosh
    </code>
   </span>
  </a>
  ,
the
  <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.mapReduce/#mongodb-method-db.collection.mapReduce">
   <code class="leafygreen-ui-1l06pbn">
    db.collection.mapReduce()
   </code>
  </a>
  wrapper method.
 </p>
 <p class="leafygreen-ui-1kp3ins">
  If the map-reduce data set is constantly growing, you may want to
perform an incremental map-reduce rather than
performing the map-reduce operation over the entire data set each time.
 </p>
 <p class="leafygreen-ui-1kp3ins">
  To perform incremental map-reduce:
 </p>
 <ol class="leafygreen-ui-rioki0" type="1">
  <li class="leafygreen-ui-d7p7b6">
   <p class="leafygreen-ui-1kp3ins">
    Run a map-reduce job over the current collection and output the
result to a separate collection.
   </p>
  </li>
  <li class="leafygreen-ui-d7p7b6">
   <p class="leafygreen-ui-1kp3ins">
    When you have more data to process, run subsequent map-reduce jobs
with:
   </p>
   <ul class="leafygreen-ui-rioki0">
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      the
      <code class="leafygreen-ui-1l06pbn">
       query
      </code>
      parameter that specifies conditions that match
      <em>
       only
      </em>
      the new documents.
     </p>
    </li>
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      the
      <code class="leafygreen-ui-1l06pbn">
       out
      </code>
      parameter that specifies the
      <code class="leafygreen-ui-1l06pbn">
       reduce
      </code>
      action to
merge the new results into the existing output collection.
     </p>
    </li>
   </ul>
  </li>
 </ol>
 <p class="leafygreen-ui-1kp3ins">
  Consider the following example where you schedule a map-reduce
operation on a
  <code class="leafygreen-ui-1l06pbn">
   usersessions
  </code>
  collection to run at the end of each day.
 </p>
 <section>
  <h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
   Data Setup
   <a class="headerlink leafygreen-ui-1kepx47" href="#data-setup" title="Permalink to this heading">
    <svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
     <path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
     </path>
     <path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
     </path>
    </svg>
    <div class="css-1btu0w3 evvkmar0" id="data-setup">
    </div>
   </a>
  </h2>
  <p class="leafygreen-ui-1kp3ins">
   The
   <code class="leafygreen-ui-1l06pbn">
    usersessions
   </code>
   collection contains documents that log users' sessions
each day, for example:
  </p>
  <script class="structured_data" type="application/ld+json">
   {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.usersessions.insertMany([\n   { userid: \"a\", start: ISODate('2020-03-03 14:17:00'), length: 95 },\n   { userid: \"b\", start: ISODate('2020-03-03 14:23:00'), length: 110 },\n   { userid: \"c\", start: ISODate('2020-03-03 15:02:00'), length: 120 },\n   { userid: \"d\", start: ISODate('2020-03-03 16:45:00'), length: 45 },\n   { userid: \"a\", start: ISODate('2020-03-04 11:05:00'), length: 105 },\n   { userid: \"b\", start: ISODate('2020-03-04 13:14:00'), length: 120 },\n   { userid: \"c\", start: ISODate('2020-03-04 17:00:00'), length: 130 },\n   { userid: \"d\", start: ISODate('2020-03-04 15:37:00'), length: 65 }\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">usersessions</span>.<span class="lg-highlight-title lg-highlight-function">insertMany</span>(<!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">userid</span>: <span class="lg-highlight-string">"a"</span>, <span class="lg-highlight-attr">start</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">'2020-03-03 14:17:00'</span>)<!-- -->, <span class="lg-highlight-attr">length</span>: <span class="lg-highlight-number">95</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">userid</span>: <span class="lg-highlight-string">"b"</span>, <span class="lg-highlight-attr">start</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">'2020-03-03 14:23:00'</span>)<!-- -->, <span class="lg-highlight-attr">length</span>: <span class="lg-highlight-number">110</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">userid</span>: <span class="lg-highlight-string">"c"</span>, <span class="lg-highlight-attr">start</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">'2020-03-03 15:02:00'</span>)<!-- -->, <span class="lg-highlight-attr">length</span>: <span class="lg-highlight-number">120</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">userid</span>: <span class="lg-highlight-string">"d"</span>, <span class="lg-highlight-attr">start</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">'2020-03-03 16:45:00'</span>)<!-- -->, <span class="lg-highlight-attr">length</span>: <span class="lg-highlight-number">45</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">userid</span>: <span class="lg-highlight-string">"a"</span>, <span class="lg-highlight-attr">start</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">'2020-03-04 11:05:00'</span>)<!-- -->, <span class="lg-highlight-attr">length</span>: <span class="lg-highlight-number">105</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">userid</span>: <span class="lg-highlight-string">"b"</span>, <span class="lg-highlight-attr">start</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">'2020-03-04 13:14:00'</span>)<!-- -->, <span class="lg-highlight-attr">length</span>: <span class="lg-highlight-number">120</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">userid</span>: <span class="lg-highlight-string">"c"</span>, <span class="lg-highlight-attr">start</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">'2020-03-04 17:00:00'</span>)<!-- -->, <span class="lg-highlight-attr">length</span>: <span class="lg-highlight-number">130</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">userid</span>: <span class="lg-highlight-string">"d"</span>, <span class="lg-highlight-attr">start</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">'2020-03-04 15:37:00'</span>)<!-- -->, <span class="lg-highlight-attr">length</span>: <span class="lg-highlight-number">65</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">])</td></tr></tbody></table></code></pre>
     <div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
      <button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
       <div class="leafygreen-ui-xhlipt">
        <svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
         <path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
         </path>
        </svg>
       </div>
      </button>
     </div>
    </div>
   </div>
  </div>
 </section>
 <section>
  <h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
   Initial Map-Reduce of Current Collection
   <a class="headerlink leafygreen-ui-1kepx47" href="#initial-map-reduce-of-current-collection" 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="initial-map-reduce-of-current-collection">
    </div>
   </a>
  </h2>
  <p class="leafygreen-ui-1kp3ins">
   Run the first map-reduce operation as follows:
  </p>
  <ol class="leafygreen-ui-rioki0" type="1">
   <li class="leafygreen-ui-d7p7b6">
    <p class="leafygreen-ui-1kp3ins">
     Define the map function that maps the
     <code class="leafygreen-ui-1l06pbn">
      userid
     </code>
     to an
object that contains the fields
     <code class="leafygreen-ui-1l06pbn">
      total_time
     </code>
     ,
     <code class="leafygreen-ui-1l06pbn">
      count
     </code>
     ,
and
     <code class="leafygreen-ui-1l06pbn">
      avg_time
     </code>
     :
    </p>
    <script class="structured_data" type="application/ld+json">
     {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"var mapFunction = function() {\n    var key = this.userid;\n    var value = { total_time: this.length, count: 1, avg_time: 0 };\n\n    emit( key, value );\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> mapFunction <!-- -->= <span class="lg-highlight-keyword lg-highlight-function">function</span>(<!-- -->)<!-- --> <!-- -->{</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">userid</span>;</td></tr><tr class=""><td class="leafygreen-ui-7razhx">    <span class="lg-highlight-keyword">var</span> value <!-- -->= <!-- -->{ <span class="lg-highlight-attr">total_time</span>: <span class="lg-highlight-variable lg-highlight-language">this</span>.<span class="lg-highlight-property">length</span>, <span class="lg-highlight-attr">count</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">avg_time</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-title lg-highlight-function">emit</span>( <!-- -->key<!-- -->, <!-- -->value )<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}<!-- -->;</td></tr></tbody></table></code></pre>
       <div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
        <button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
         <div class="leafygreen-ui-xhlipt">
          <svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
           <path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
           </path>
          </svg>
         </div>
        </button>
       </div>
      </div>
     </div>
    </div>
   </li>
   <li class="leafygreen-ui-d7p7b6">
    <p class="leafygreen-ui-1kp3ins">
     Define the corresponding reduce function with two arguments
     <code class="leafygreen-ui-1l06pbn">
      key
     </code>
     and
     <code class="leafygreen-ui-1l06pbn">
      values
     </code>
     to calculate the total time and the count.
The
     <code class="leafygreen-ui-1l06pbn">
      key
     </code>
     corresponds to the
     <code class="leafygreen-ui-1l06pbn">
      userid
     </code>
     , and the
     <code class="leafygreen-ui-1l06pbn">
      values
     </code>
     is an
array whose elements corresponds to the individual objects mapped to the
     <code class="leafygreen-ui-1l06pbn">
      userid
     </code>
     in the
     <code class="leafygreen-ui-1l06pbn">
      mapFunction
     </code>
     .
    </p>
    <script class="structured_data" type="application/ld+json">
     {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"var reduceFunction = function(key, values) {\n\n   var reducedObject = { total_time: 0, count:0, avg_time:0 };\n\n   values.forEach(function(value) {\n      reducedObject.total_time += value.total_time;\n      reducedObject.count += value.count;\n   });\n\n   return reducedObject;\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> reduceFunction <!-- -->= <span class="lg-highlight-keyword lg-highlight-function">function</span>(<span class="lg-highlight-params">key, values</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">var</span> reducedObject <!-- -->= <!-- -->{ <span class="lg-highlight-attr">total_time</span>: <span class="lg-highlight-number">0</span>, <span class="lg-highlight-attr">count</span>:<span class="lg-highlight-number">0</span>, <span class="lg-highlight-attr">avg_time</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">   <!-- -->values.<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">value</span>)<!-- --> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <!-- -->reducedObject.<span class="lg-highlight-property">total_time</span> <!-- -->+<!-- -->= <!-- -->value.<span class="lg-highlight-property">total_time</span>;</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <!-- -->reducedObject.<span class="lg-highlight-property">count</span> <!-- -->+<!-- -->= <!-- -->value.<span class="lg-highlight-property">count</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> <!-- -->reducedObject<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}<!-- -->;</td></tr></tbody></table></code></pre>
       <div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
        <button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
         <div class="leafygreen-ui-xhlipt">
          <svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
           <path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
           </path>
          </svg>
         </div>
        </button>
       </div>
      </div>
     </div>
    </div>
   </li>
   <li class="leafygreen-ui-d7p7b6">
    <p class="leafygreen-ui-1kp3ins">
     Define the finalize function with two arguments
     <code class="leafygreen-ui-1l06pbn">
      key
     </code>
     and
     <code class="leafygreen-ui-1l06pbn">
      reducedValue
     </code>
     . The function modifies the
     <code class="leafygreen-ui-1l06pbn">
      reducedValue
     </code>
     document
to add another field
     <code class="leafygreen-ui-1l06pbn">
      average
     </code>
     and returns the modified document.
    </p>
    <script class="structured_data" type="application/ld+json">
     {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"var finalizeFunction = function(key, reducedValue) {\n\n   if (reducedValue.count &gt; 0)\n      reducedValue.avg_time = reducedValue.total_time / reducedValue.count;\n\n   return reducedValue;\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> finalizeFunction <!-- -->= <span class="lg-highlight-keyword lg-highlight-function">function</span>(<span class="lg-highlight-params">key, reducedValue</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">if</span> <!-- -->(<!-- -->reducedValue.<span class="lg-highlight-property">count</span> <!-- -->&gt; <span class="lg-highlight-number">0</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      reducedValue.<span class="lg-highlight-property">avg_time</span> <!-- -->= <!-- -->reducedValue.<span class="lg-highlight-property">total_time</span> <!-- -->/ <!-- -->reducedValue.<span class="lg-highlight-property">count</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">return</span> <!-- -->reducedValue<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}<!-- -->;</td></tr></tbody></table></code></pre>
       <div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
        <button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
         <div class="leafygreen-ui-xhlipt">
          <svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
           <path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
           </path>
          </svg>
         </div>
        </button>
       </div>
      </div>
     </div>
    </div>
   </li>
   <li class="leafygreen-ui-d7p7b6">
    <p class="leafygreen-ui-1kp3ins">
     Perform map-reduce on the
     <code class="leafygreen-ui-1l06pbn">
      usersessions
     </code>
     collection using the
     <code class="leafygreen-ui-1l06pbn">
      mapFunction
     </code>
     , the
     <code class="leafygreen-ui-1l06pbn">
      reduceFunction
     </code>
     , and the
     <code class="leafygreen-ui-1l06pbn">
      finalizeFunction
     </code>
     functions. Output the results to a collection
     <code class="leafygreen-ui-1l06pbn">
      session_stats
     </code>
     . If the
     <code class="leafygreen-ui-1l06pbn">
      session_stats
     </code>
     collection already exists,
the operation will replace the contents:
    </p>
    <script class="structured_data" type="application/ld+json">
     {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.usersessions.mapReduce(\n   mapFunction,\n   reduceFunction,\n   {\n     out: \"session_stats\",\n     finalize: finalizeFunction\n   }\n)","programmingLanguage":"JavaScript"}
    </script>
    <div class="css-16lmvu9">
     <div class="leafygreen-ui-196mwvg">
      <div class="leafygreen-ui-yzcs8i">
       <pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">usersessions</span>.<span class="lg-highlight-title lg-highlight-function">mapReduce</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->mapFunction<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->reduceFunction<!-- -->,</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">"session_stats"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-attr">finalize</span>: <!-- -->finalizeFunction</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>
   </li>
   <li class="leafygreen-ui-d7p7b6">
    <p class="leafygreen-ui-1kp3ins">
     Query the
     <code class="leafygreen-ui-1l06pbn">
      session_stats
     </code>
     collection to verify the results:
    </p>
    <script class="structured_data" type="application/ld+json">
     {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.session_stats.find().sort( { _id: 1 } )","programmingLanguage":"JavaScript"}
    </script>
    <div class="css-16lmvu9">
     <div class="leafygreen-ui-196mwvg">
      <div class="leafygreen-ui-yzcs8i">
       <pre class="leafygreen-ui-1q4bxgx" 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">db.<span class="lg-highlight-property">session_stats</span>.<span class="lg-highlight-title lg-highlight-function">find</span>(<!-- -->).<span class="lg-highlight-title lg-highlight-function">sort</span>( <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">1</span> } )</td></tr></tbody></table></code></pre>
       <div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
        <button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
         <div class="leafygreen-ui-xhlipt">
          <svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
           <path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
           </path>
          </svg>
         </div>
        </button>
       </div>
      </div>
     </div>
    </div>
    <p class="leafygreen-ui-1kp3ins">
     The operation returns the following documents:
    </p>
    <script class="structured_data" type="application/ld+json">
     {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : \"a\", \"value\" : { \"total_time\" : 200, \"count\" : 2, \"avg_time\" : 100 } }\n{ \"_id\" : \"b\", \"value\" : { \"total_time\" : 230, \"count\" : 2, \"avg_time\" : 115 } }\n{ \"_id\" : \"c\", \"value\" : { \"total_time\" : 250, \"count\" : 2, \"avg_time\" : 125 } }\n{ \"_id\" : \"d\", \"value\" : { \"total_time\" : 110, \"count\" : 2, \"avg_time\" : 55 } }","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">"a"</span>, <span class="lg-highlight-string">"value"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"total_time"</span> <!-- -->: <span class="lg-highlight-number">200</span>, <span class="lg-highlight-string">"count"</span> <!-- -->: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-string">"avg_time"</span> <!-- -->: <span class="lg-highlight-number">100</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"b"</span>, <span class="lg-highlight-string">"value"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"total_time"</span> <!-- -->: <span class="lg-highlight-number">230</span>, <span class="lg-highlight-string">"count"</span> <!-- -->: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-string">"avg_time"</span> <!-- -->: <span class="lg-highlight-number">115</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"c"</span>, <span class="lg-highlight-string">"value"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"total_time"</span> <!-- -->: <span class="lg-highlight-number">250</span>, <span class="lg-highlight-string">"count"</span> <!-- -->: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-string">"avg_time"</span> <!-- -->: <span class="lg-highlight-number">125</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"d"</span>, <span class="lg-highlight-string">"value"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"total_time"</span> <!-- -->: <span class="lg-highlight-number">110</span>, <span class="lg-highlight-string">"count"</span> <!-- -->: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-string">"avg_time"</span> <!-- -->: <span class="lg-highlight-number">55</span> } }</td></tr></tbody></table></code></pre>
      </div>
     </div>
    </div>
   </li>
  </ol>
 </section>
 <section>
  <h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
   Subsequent Incremental Map-Reduce
   <a class="headerlink leafygreen-ui-1kepx47" href="#subsequent-incremental-map-reduce" title="Permalink to this heading">
    <svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
     <path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
     </path>
     <path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
     </path>
    </svg>
    <div class="css-1btu0w3 evvkmar0" id="subsequent-incremental-map-reduce">
    </div>
   </a>
  </h2>
  <p class="leafygreen-ui-1kp3ins">
   Later, as the
   <code class="leafygreen-ui-1l06pbn">
    usersessions
   </code>
   collection grows, you can run additional
map-reduce operations. For example, add new documents to the
   <code class="leafygreen-ui-1l06pbn">
    usersessions
   </code>
   collection:
  </p>
  <script class="structured_data" type="application/ld+json">
   {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.usersessions.insertMany([\n   { userid: \"a\", ts: ISODate('2020-03-05 14:17:00'), length: 130 },\n   { userid: \"b\", ts: ISODate('2020-03-05 14:23:00'), length: 40 },\n   { userid: \"c\", ts: ISODate('2020-03-05 15:02:00'), length: 110 },\n   { userid: \"d\", ts: ISODate('2020-03-05 16:45:00'), length: 100 }\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">usersessions</span>.<span class="lg-highlight-title lg-highlight-function">insertMany</span>(<!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">userid</span>: <span class="lg-highlight-string">"a"</span>, <span class="lg-highlight-attr">ts</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">'2020-03-05 14:17:00'</span>)<!-- -->, <span class="lg-highlight-attr">length</span>: <span class="lg-highlight-number">130</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">userid</span>: <span class="lg-highlight-string">"b"</span>, <span class="lg-highlight-attr">ts</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">'2020-03-05 14:23:00'</span>)<!-- -->, <span class="lg-highlight-attr">length</span>: <span class="lg-highlight-number">40</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">userid</span>: <span class="lg-highlight-string">"c"</span>, <span class="lg-highlight-attr">ts</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">'2020-03-05 15:02:00'</span>)<!-- -->, <span class="lg-highlight-attr">length</span>: <span class="lg-highlight-number">110</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">userid</span>: <span class="lg-highlight-string">"d"</span>, <span class="lg-highlight-attr">ts</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">'2020-03-05 16:45:00'</span>)<!-- -->, <span class="lg-highlight-attr">length</span>: <span class="lg-highlight-number">100</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">
   At the end of the day, perform incremental map-reduce on the
   <code class="leafygreen-ui-1l06pbn">
    usersessions
   </code>
   collection, but use the
   <code class="leafygreen-ui-1l06pbn">
    query
   </code>
   field to select only the
new documents. Output the results to the collection
   <code class="leafygreen-ui-1l06pbn">
    session_stats
   </code>
   ,
but
   <code class="leafygreen-ui-1l06pbn">
    reduce
   </code>
   the contents with the results of the incremental
map-reduce:
  </p>
  <script class="structured_data" type="application/ld+json">
   {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.usersessions.mapReduce(\n   mapFunction,\n   reduceFunction,\n   {\n     query: { ts: { $gte: ISODate('2020-03-05 00:00:00') } },\n     out: { reduce: \"session_stats\" },\n     finalize: finalizeFunction\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">usersessions</span>.<span class="lg-highlight-title lg-highlight-function">mapReduce</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->mapFunction<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->reduceFunction<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-attr">query</span>: <!-- -->{ <span class="lg-highlight-attr">ts</span>: <!-- -->{ <span class="lg-highlight-attr">$gte</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">'2020-03-05 00:00:00'</span>) } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-attr">out</span>: <!-- -->{ <span class="lg-highlight-attr">reduce</span>: <span class="lg-highlight-string">"session_stats"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">     <span class="lg-highlight-attr">finalize</span>: <!-- -->finalizeFunction</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">
   Query the
   <code class="leafygreen-ui-1l06pbn">
    session_stats
   </code>
   collection to verify the results:
  </p>
  <script class="structured_data" type="application/ld+json">
   {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.session_stats.find().sort( { _id: 1 } )","programmingLanguage":"JavaScript"}
  </script>
  <div class="css-16lmvu9">
   <div class="leafygreen-ui-196mwvg">
    <div class="leafygreen-ui-yzcs8i">
     <pre class="leafygreen-ui-1q4bxgx" 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">db.<span class="lg-highlight-property">session_stats</span>.<span class="lg-highlight-title lg-highlight-function">find</span>(<!-- -->).<span class="lg-highlight-title lg-highlight-function">sort</span>( <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">1</span> } )</td></tr></tbody></table></code></pre>
     <div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
      <button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
       <div class="leafygreen-ui-xhlipt">
        <svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
         <path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
         </path>
        </svg>
       </div>
      </button>
     </div>
    </div>
   </div>
  </div>
  <p class="leafygreen-ui-1kp3ins">
   The operation returns the following documents:
  </p>
  <script class="structured_data" type="application/ld+json">
   {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : \"a\", \"value\" : { \"total_time\" : 330, \"count\" : 3, \"avg_time\" : 110 } }\n{ \"_id\" : \"b\", \"value\" : { \"total_time\" : 270, \"count\" : 3, \"avg_time\" : 90 } }\n{ \"_id\" : \"c\", \"value\" : { \"total_time\" : 360, \"count\" : 3, \"avg_time\" : 120 } }\n{ \"_id\" : \"d\", \"value\" : { \"total_time\" : 210, \"count\" : 3, \"avg_time\" : 70 } }","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">"a"</span>, <span class="lg-highlight-string">"value"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"total_time"</span> <!-- -->: <span class="lg-highlight-number">330</span>, <span class="lg-highlight-string">"count"</span> <!-- -->: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-string">"avg_time"</span> <!-- -->: <span class="lg-highlight-number">110</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"b"</span>, <span class="lg-highlight-string">"value"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"total_time"</span> <!-- -->: <span class="lg-highlight-number">270</span>, <span class="lg-highlight-string">"count"</span> <!-- -->: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-string">"avg_time"</span> <!-- -->: <span class="lg-highlight-number">90</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"c"</span>, <span class="lg-highlight-string">"value"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"total_time"</span> <!-- -->: <span class="lg-highlight-number">360</span>, <span class="lg-highlight-string">"count"</span> <!-- -->: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-string">"avg_time"</span> <!-- -->: <span class="lg-highlight-number">120</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"d"</span>, <span class="lg-highlight-string">"value"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"total_time"</span> <!-- -->: <span class="lg-highlight-number">210</span>, <span class="lg-highlight-string">"count"</span> <!-- -->: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-string">"avg_time"</span> <!-- -->: <span class="lg-highlight-number">70</span> } }</td></tr></tbody></table></code></pre>
    </div>
   </div>
  </div>
 </section>
 <section>
  <h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
   Aggregation Alternative
   <a class="headerlink leafygreen-ui-1kepx47" href="#aggregation-alternative" title="Permalink to this heading">
    <svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
     <path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
     </path>
     <path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
     </path>
    </svg>
    <div class="css-1btu0w3 evvkmar0" id="aggregation-alternative">
    </div>
   </a>
  </h2>
  <p class="leafygreen-ui-1kp3ins">
   As an alternative to map-reduce, you can use an
   <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/aggregation-pipeline/#std-label-aggregation-pipeline">
    aggregation
pipeline
   </a>
   that combines
   <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>
   stages to achieve the same result in a more
flexible operation.
  </p>
  <p class="leafygreen-ui-1kp3ins">
   Recreate the
   <code class="leafygreen-ui-1l06pbn">
    usersessions
   </code>
   collection:
  </p>
  <script class="structured_data" type="application/ld+json">
   {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.usersessions.drop();\n\ndb.usersessions.insertMany([\n   { userid: \"a\", start: ISODate('2020-03-03 14:17:00'), length: 95 },\n   { userid: \"b\", start: ISODate('2020-03-03 14:23:00'), length: 110 },\n   { userid: \"c\", start: ISODate('2020-03-03 15:02:00'), length: 120 },\n   { userid: \"d\", start: ISODate('2020-03-03 16:45:00'), length: 45 },\n   { userid: \"a\", start: ISODate('2020-03-04 11:05:00'), length: 105 },\n   { userid: \"b\", start: ISODate('2020-03-04 13:14:00'), length: 120 },\n   { userid: \"c\", start: ISODate('2020-03-04 17:00:00'), length: 130 },\n   { userid: \"d\", start: ISODate('2020-03-04 15:37:00'), length: 65 }\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">usersessions</span>.<span class="lg-highlight-title lg-highlight-function">drop</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">db.<span class="lg-highlight-property">usersessions</span>.<span class="lg-highlight-title lg-highlight-function">insertMany</span>(<!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">userid</span>: <span class="lg-highlight-string">"a"</span>, <span class="lg-highlight-attr">start</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">'2020-03-03 14:17:00'</span>)<!-- -->, <span class="lg-highlight-attr">length</span>: <span class="lg-highlight-number">95</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">userid</span>: <span class="lg-highlight-string">"b"</span>, <span class="lg-highlight-attr">start</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">'2020-03-03 14:23:00'</span>)<!-- -->, <span class="lg-highlight-attr">length</span>: <span class="lg-highlight-number">110</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">userid</span>: <span class="lg-highlight-string">"c"</span>, <span class="lg-highlight-attr">start</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">'2020-03-03 15:02:00'</span>)<!-- -->, <span class="lg-highlight-attr">length</span>: <span class="lg-highlight-number">120</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">userid</span>: <span class="lg-highlight-string">"d"</span>, <span class="lg-highlight-attr">start</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">'2020-03-03 16:45:00'</span>)<!-- -->, <span class="lg-highlight-attr">length</span>: <span class="lg-highlight-number">45</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">userid</span>: <span class="lg-highlight-string">"a"</span>, <span class="lg-highlight-attr">start</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">'2020-03-04 11:05:00'</span>)<!-- -->, <span class="lg-highlight-attr">length</span>: <span class="lg-highlight-number">105</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">userid</span>: <span class="lg-highlight-string">"b"</span>, <span class="lg-highlight-attr">start</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">'2020-03-04 13:14:00'</span>)<!-- -->, <span class="lg-highlight-attr">length</span>: <span class="lg-highlight-number">120</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">userid</span>: <span class="lg-highlight-string">"c"</span>, <span class="lg-highlight-attr">start</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">'2020-03-04 17:00:00'</span>)<!-- -->, <span class="lg-highlight-attr">length</span>: <span class="lg-highlight-number">130</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">userid</span>: <span class="lg-highlight-string">"d"</span>, <span class="lg-highlight-attr">start</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">'2020-03-04 15:37:00'</span>)<!-- -->, <span class="lg-highlight-attr">length</span>: <span class="lg-highlight-number">65</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">
   Using the available aggregation pipeline operators, you can rewrite the
map-reduce example without defining custom functions:
  </p>
  <script class="structured_data" type="application/ld+json">
   {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.usersessions.aggregate([\n   { $group: { _id: \"$userid\", total_time: { $sum: \"$length\" }, count: { $sum: 1 }, avg_time: { $avg: \"$length\" } } },\n   { $project: { value: { total_time: \"$total_time\", count: \"$count\", avg_time: \"$avg_time\" } } },\n   { $merge: {\n      into: \"session_stats_agg\",\n      whenMatched: [ { $set: {\n         \"value.total_time\": { $add: [ \"$value.total_time\", \"$$new.value.total_time\" ] },\n         \"value.count\": { $add: [ \"$value.count\", \"$$new.value.count\" ] },\n         \"value.avg\": { $divide: [ { $add: [ \"$value.total_time\", \"$$new.value.total_time\" ] },  { $add: [ \"$value.count\", \"$$new.value.count\" ] } ] }\n      } } ],\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">usersessions</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">"$userid"</span>, <span class="lg-highlight-attr">total_time</span>: <!-- -->{ <span class="lg-highlight-attr">$sum</span>: <span class="lg-highlight-string">"$length"</span> }<!-- -->, <span class="lg-highlight-attr">count</span>: <!-- -->{ <span class="lg-highlight-attr">$sum</span>: <span class="lg-highlight-number">1</span> }<!-- -->, <span class="lg-highlight-attr">avg_time</span>: <!-- -->{ <span class="lg-highlight-attr">$avg</span>: <span class="lg-highlight-string">"$length"</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">total_time</span>: <span class="lg-highlight-string">"$total_time"</span>, <span class="lg-highlight-attr">count</span>: <span class="lg-highlight-string">"$count"</span>, <span class="lg-highlight-attr">avg_time</span>: <span class="lg-highlight-string">"$avg_time"</span> } } }<!-- -->,</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">"session_stats_agg"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <span class="lg-highlight-attr">whenMatched</span>: <!-- -->[ <!-- -->{ <span class="lg-highlight-attr">$set</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">         <span class="lg-highlight-string">"value.total_time"</span>: <!-- -->{ <span class="lg-highlight-attr">$add</span>: <!-- -->[ <span class="lg-highlight-string">"$value.total_time"</span>, <span class="lg-highlight-string">"$$new.value.total_time"</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">         <span class="lg-highlight-string">"value.count"</span>: <!-- -->{ <span class="lg-highlight-attr">$add</span>: <!-- -->[ <span class="lg-highlight-string">"$value.count"</span>, <span class="lg-highlight-string">"$$new.value.count"</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">         <span class="lg-highlight-string">"value.avg"</span>: <!-- -->{ <span class="lg-highlight-attr">$divide</span>: <!-- -->[ <!-- -->{ <span class="lg-highlight-attr">$add</span>: <!-- -->[ <span class="lg-highlight-string">"$value.total_time"</span>, <span class="lg-highlight-string">"$$new.value.total_time"</span> ] }<!-- -->,  <!-- -->{ <span class="lg-highlight-attr">$add</span>: <!-- -->[ <span class="lg-highlight-string">"$value.count"</span>, <span class="lg-highlight-string">"$$new.value.count"</span> ] } ] }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      } } ]<!-- -->,</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/group/#mongodb-pipeline-pipe.-group">
      <code class="leafygreen-ui-1l06pbn">
       $group
      </code>
     </a>
     groups by the
     <code class="leafygreen-ui-1l06pbn">
      userid
     </code>
     and calculates:
    </p>
    <ul class="leafygreen-ui-rioki0">
     <li class="leafygreen-ui-d7p7b6">
      <p class="leafygreen-ui-1kp3ins">
       The
       <code class="leafygreen-ui-1l06pbn">
        total_time
       </code>
       using the
       <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/sum/#mongodb-group-grp.-sum">
        <code class="leafygreen-ui-1l06pbn">
         $sum
        </code>
       </a>
       operator
      </p>
     </li>
     <li class="leafygreen-ui-d7p7b6">
      <p class="leafygreen-ui-1kp3ins">
       The
       <code class="leafygreen-ui-1l06pbn">
        count
       </code>
       using the
       <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/sum/#mongodb-group-grp.-sum">
        <code class="leafygreen-ui-1l06pbn">
         $sum
        </code>
       </a>
       operator
      </p>
     </li>
     <li class="leafygreen-ui-d7p7b6">
      <p class="leafygreen-ui-1kp3ins">
       The
       <code class="leafygreen-ui-1l06pbn">
        avg_time
       </code>
       using the
       <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/avg/#mongodb-group-grp.-avg">
        <code class="leafygreen-ui-1l06pbn">
         $avg
        </code>
       </a>
       operator
      </p>
     </li>
    </ul>
    <p class="leafygreen-ui-1kp3ins">
     The operation returns the following documents:
    </p>
    <script class="structured_data" type="application/ld+json">
     {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : \"c\", \"total_time\" : 250, \"count\" : 2, \"avg_time\" : 125 }\n{ \"_id\" : \"d\", \"total_time\" : 110, \"count\" : 2, \"avg_time\" : 55 }\n{ \"_id\" : \"a\", \"total_time\" : 200, \"count\" : 2, \"avg_time\" : 100 }\n{ \"_id\" : \"b\", \"total_time\" : 230, \"count\" : 2, \"avg_time\" : 115 }","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">"c"</span>, <span class="lg-highlight-string">"total_time"</span> <!-- -->: <span class="lg-highlight-number">250</span>, <span class="lg-highlight-string">"count"</span> <!-- -->: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-string">"avg_time"</span> <!-- -->: <span class="lg-highlight-number">125</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"d"</span>, <span class="lg-highlight-string">"total_time"</span> <!-- -->: <span class="lg-highlight-number">110</span>, <span class="lg-highlight-string">"count"</span> <!-- -->: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-string">"avg_time"</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-string">"a"</span>, <span class="lg-highlight-string">"total_time"</span> <!-- -->: <span class="lg-highlight-number">200</span>, <span class="lg-highlight-string">"count"</span> <!-- -->: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-string">"avg_time"</span> <!-- -->: <span class="lg-highlight-number">100</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"b"</span>, <span class="lg-highlight-string">"total_time"</span> <!-- -->: <span class="lg-highlight-number">230</span>, <span class="lg-highlight-string">"count"</span> <!-- -->: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-string">"avg_time"</span> <!-- -->: <span class="lg-highlight-number">115</span> }</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 reshapes the output document to
mirror the map-reduce's output to have two fields
     <code class="leafygreen-ui-1l06pbn">
      _id
     </code>
     and
     <code class="leafygreen-ui-1l06pbn">
      value
     </code>
     . The stage is optional if you do not need to mirror the
     <code class="leafygreen-ui-1l06pbn">
      _id
     </code>
     and
     <code class="leafygreen-ui-1l06pbn">
      value
     </code>
     structure.
    </p>
    <script class="structured_data" type="application/ld+json">
     {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : \"a\", \"value\" : { \"total_time\" : 200, \"count\" : 2, \"avg_time\" : 100 } }\n{ \"_id\" : \"d\", \"value\" : { \"total_time\" : 110, \"count\" : 2, \"avg_time\" : 55 } }\n{ \"_id\" : \"b\", \"value\" : { \"total_time\" : 230, \"count\" : 2, \"avg_time\" : 115 } }\n{ \"_id\" : \"c\", \"value\" : { \"total_time\" : 250, \"count\" : 2, \"avg_time\" : 125 } }","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">"a"</span>, <span class="lg-highlight-string">"value"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"total_time"</span> <!-- -->: <span class="lg-highlight-number">200</span>, <span class="lg-highlight-string">"count"</span> <!-- -->: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-string">"avg_time"</span> <!-- -->: <span class="lg-highlight-number">100</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"d"</span>, <span class="lg-highlight-string">"value"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"total_time"</span> <!-- -->: <span class="lg-highlight-number">110</span>, <span class="lg-highlight-string">"count"</span> <!-- -->: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-string">"avg_time"</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-string">"b"</span>, <span class="lg-highlight-string">"value"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"total_time"</span> <!-- -->: <span class="lg-highlight-number">230</span>, <span class="lg-highlight-string">"count"</span> <!-- -->: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-string">"avg_time"</span> <!-- -->: <span class="lg-highlight-number">115</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"c"</span>, <span class="lg-highlight-string">"value"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"total_time"</span> <!-- -->: <span class="lg-highlight-number">250</span>, <span class="lg-highlight-string">"count"</span> <!-- -->: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-string">"avg_time"</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">
     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>
     stage outputs the results to a
     <code class="leafygreen-ui-1l06pbn">
      session_stats_agg
     </code>
     collection. If an existing document has the
same
     <code class="leafygreen-ui-1l06pbn">
      _id
     </code>
     as the new result, the operation applies the specified
pipeline to calculate the total_time, count, and avg_time from the
result and the existing document. If there is no existing document
with the same
     <code class="leafygreen-ui-1l06pbn">
      _id
     </code>
     in the
     <code class="leafygreen-ui-1l06pbn">
      session_stats_agg
     </code>
     , the operation
inserts the document.
    </p>
   </li>
   <li class="leafygreen-ui-d7p7b6">
    <p class="leafygreen-ui-1kp3ins">
     Query the
     <code class="leafygreen-ui-1l06pbn">
      session_stats_agg
     </code>
     collection to verify the results:
    </p>
    <script class="structured_data" type="application/ld+json">
     {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.session_stats_agg.find().sort( { _id: 1 } )","programmingLanguage":"JavaScript"}
    </script>
    <div class="css-16lmvu9">
     <div class="leafygreen-ui-196mwvg">
      <div class="leafygreen-ui-yzcs8i">
       <pre class="leafygreen-ui-1q4bxgx" 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">db.<span class="lg-highlight-property">session_stats_agg</span>.<span class="lg-highlight-title lg-highlight-function">find</span>(<!-- -->).<span class="lg-highlight-title lg-highlight-function">sort</span>( <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">1</span> } )</td></tr></tbody></table></code></pre>
       <div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
        <button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
         <div class="leafygreen-ui-xhlipt">
          <svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
           <path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
           </path>
          </svg>
         </div>
        </button>
       </div>
      </div>
     </div>
    </div>
    <p class="leafygreen-ui-1kp3ins">
     The operation returns the following documents:
    </p>
    <script class="structured_data" type="application/ld+json">
     {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : \"a\", \"value\" : { \"total_time\" : 200, \"count\" : 2, \"avg_time\" : 100 } }\n{ \"_id\" : \"b\", \"value\" : { \"total_time\" : 230, \"count\" : 2, \"avg_time\" : 115 } }\n{ \"_id\" : \"c\", \"value\" : { \"total_time\" : 250, \"count\" : 2, \"avg_time\" : 125 } }\n{ \"_id\" : \"d\", \"value\" : { \"total_time\" : 110, \"count\" : 2, \"avg_time\" : 55 } }","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">"a"</span>, <span class="lg-highlight-string">"value"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"total_time"</span> <!-- -->: <span class="lg-highlight-number">200</span>, <span class="lg-highlight-string">"count"</span> <!-- -->: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-string">"avg_time"</span> <!-- -->: <span class="lg-highlight-number">100</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"b"</span>, <span class="lg-highlight-string">"value"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"total_time"</span> <!-- -->: <span class="lg-highlight-number">230</span>, <span class="lg-highlight-string">"count"</span> <!-- -->: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-string">"avg_time"</span> <!-- -->: <span class="lg-highlight-number">115</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"c"</span>, <span class="lg-highlight-string">"value"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"total_time"</span> <!-- -->: <span class="lg-highlight-number">250</span>, <span class="lg-highlight-string">"count"</span> <!-- -->: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-string">"avg_time"</span> <!-- -->: <span class="lg-highlight-number">125</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"d"</span>, <span class="lg-highlight-string">"value"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"total_time"</span> <!-- -->: <span class="lg-highlight-number">110</span>, <span class="lg-highlight-string">"count"</span> <!-- -->: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-string">"avg_time"</span> <!-- -->: <span class="lg-highlight-number">55</span> } }</td></tr></tbody></table></code></pre>
      </div>
     </div>
    </div>
   </li>
   <li class="leafygreen-ui-d7p7b6">
    <p class="leafygreen-ui-1kp3ins">
     Add new documents to the
     <code class="leafygreen-ui-1l06pbn">
      usersessions
     </code>
     collection:
    </p>
    <script class="structured_data" type="application/ld+json">
     {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.usersessions.insertMany([\n   { userid: \"a\", ts: ISODate('2020-03-05 14:17:00'), length: 130 },\n   { userid: \"b\", ts: ISODate('2020-03-05 14:23:00'), length: 40 },\n   { userid: \"c\", ts: ISODate('2020-03-05 15:02:00'), length: 110 },\n   { userid: \"d\", ts: ISODate('2020-03-05 16:45:00'), length: 100 }\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">usersessions</span>.<span class="lg-highlight-title lg-highlight-function">insertMany</span>(<!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">userid</span>: <span class="lg-highlight-string">"a"</span>, <span class="lg-highlight-attr">ts</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">'2020-03-05 14:17:00'</span>)<!-- -->, <span class="lg-highlight-attr">length</span>: <span class="lg-highlight-number">130</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">userid</span>: <span class="lg-highlight-string">"b"</span>, <span class="lg-highlight-attr">ts</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">'2020-03-05 14:23:00'</span>)<!-- -->, <span class="lg-highlight-attr">length</span>: <span class="lg-highlight-number">40</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">userid</span>: <span class="lg-highlight-string">"c"</span>, <span class="lg-highlight-attr">ts</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">'2020-03-05 15:02:00'</span>)<!-- -->, <span class="lg-highlight-attr">length</span>: <span class="lg-highlight-number">110</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">userid</span>: <span class="lg-highlight-string">"d"</span>, <span class="lg-highlight-attr">ts</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">'2020-03-05 16:45:00'</span>)<!-- -->, <span class="lg-highlight-attr">length</span>: <span class="lg-highlight-number">100</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">])</td></tr></tbody></table></code></pre>
       <div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
        <button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
         <div class="leafygreen-ui-xhlipt">
          <svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
           <path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
           </path>
          </svg>
         </div>
        </button>
       </div>
      </div>
     </div>
    </div>
   </li>
   <li class="leafygreen-ui-d7p7b6">
    <p class="leafygreen-ui-1kp3ins">
     Add a
     <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 at the start of the pipeline to
specify the date filter:
    </p>
    <script class="structured_data" type="application/ld+json">
     {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.usersessions.aggregate([\n   { $match: { ts: { $gte: ISODate('2020-03-05 00:00:00') } } },\n   { $group: { _id: \"$userid\", total_time: { $sum: \"$length\" }, count: { $sum: 1 }, avg_time: { $avg: \"$length\" } } },\n   { $project: { value: { total_time: \"$total_time\", count: \"$count\", avg_time: \"$avg_time\" } } },\n   { $merge: {\n      into: \"session_stats_agg\",\n      whenMatched: [ { $set: {\n         \"value.total_time\": { $add: [ \"$value.total_time\", \"$$new.value.total_time\" ] },\n         \"value.count\": { $add: [ \"$value.count\", \"$$new.value.count\" ] },\n         \"value.avg_time\": { $divide: [ { $add: [ \"$value.total_time\", \"$$new.value.total_time\" ] },  { $add: [ \"$value.count\", \"$$new.value.count\" ] } ] }\n      } } ],\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">usersessions</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">ts</span>: <!-- -->{ <span class="lg-highlight-attr">$gte</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">'2020-03-05 00:00:00'</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">"$userid"</span>, <span class="lg-highlight-attr">total_time</span>: <!-- -->{ <span class="lg-highlight-attr">$sum</span>: <span class="lg-highlight-string">"$length"</span> }<!-- -->, <span class="lg-highlight-attr">count</span>: <!-- -->{ <span class="lg-highlight-attr">$sum</span>: <span class="lg-highlight-number">1</span> }<!-- -->, <span class="lg-highlight-attr">avg_time</span>: <!-- -->{ <span class="lg-highlight-attr">$avg</span>: <span class="lg-highlight-string">"$length"</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">total_time</span>: <span class="lg-highlight-string">"$total_time"</span>, <span class="lg-highlight-attr">count</span>: <span class="lg-highlight-string">"$count"</span>, <span class="lg-highlight-attr">avg_time</span>: <span class="lg-highlight-string">"$avg_time"</span> } } }<!-- -->,</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">"session_stats_agg"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <span class="lg-highlight-attr">whenMatched</span>: <!-- -->[ <!-- -->{ <span class="lg-highlight-attr">$set</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">         <span class="lg-highlight-string">"value.total_time"</span>: <!-- -->{ <span class="lg-highlight-attr">$add</span>: <!-- -->[ <span class="lg-highlight-string">"$value.total_time"</span>, <span class="lg-highlight-string">"$$new.value.total_time"</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">         <span class="lg-highlight-string">"value.count"</span>: <!-- -->{ <span class="lg-highlight-attr">$add</span>: <!-- -->[ <span class="lg-highlight-string">"$value.count"</span>, <span class="lg-highlight-string">"$$new.value.count"</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">         <span class="lg-highlight-string">"value.avg_time"</span>: <!-- -->{ <span class="lg-highlight-attr">$divide</span>: <!-- -->[ <!-- -->{ <span class="lg-highlight-attr">$add</span>: <!-- -->[ <span class="lg-highlight-string">"$value.total_time"</span>, <span class="lg-highlight-string">"$$new.value.total_time"</span> ] }<!-- -->,  <!-- -->{ <span class="lg-highlight-attr">$add</span>: <!-- -->[ <span class="lg-highlight-string">"$value.count"</span>, <span class="lg-highlight-string">"$$new.value.count"</span> ] } ] }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      } } ]<!-- -->,</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>
   </li>
   <li class="leafygreen-ui-d7p7b6">
    <p class="leafygreen-ui-1kp3ins">
     Query the
     <code class="leafygreen-ui-1l06pbn">
      session_stats_agg
     </code>
     collection to verify the results:
    </p>
    <script class="structured_data" type="application/ld+json">
     {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.session_stats_agg.find().sort( { _id: 1 } )","programmingLanguage":"JavaScript"}
    </script>
    <div class="css-16lmvu9">
     <div class="leafygreen-ui-196mwvg">
      <div class="leafygreen-ui-yzcs8i">
       <pre class="leafygreen-ui-1q4bxgx" 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">db.<span class="lg-highlight-property">session_stats_agg</span>.<span class="lg-highlight-title lg-highlight-function">find</span>(<!-- -->).<span class="lg-highlight-title lg-highlight-function">sort</span>( <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">1</span> } )</td></tr></tbody></table></code></pre>
       <div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
        <button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
         <div class="leafygreen-ui-xhlipt">
          <svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
           <path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
           </path>
          </svg>
         </div>
        </button>
       </div>
      </div>
     </div>
    </div>
    <p class="leafygreen-ui-1kp3ins">
     The operation returns the following documents:
    </p>
    <script class="structured_data" type="application/ld+json">
     {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : \"a\", \"value\" : { \"total_time\" : 330, \"count\" : 3, \"avg_time\" : 110 } }\n{ \"_id\" : \"b\", \"value\" : { \"total_time\" : 270, \"count\" : 3, \"avg_time\" : 90 } }\n{ \"_id\" : \"c\", \"value\" : { \"total_time\" : 360, \"count\" : 3, \"avg_time\" : 120 } }\n{ \"_id\" : \"d\", \"value\" : { \"total_time\" : 210, \"count\" : 3, \"avg_time\" : 70 } }","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">"a"</span>, <span class="lg-highlight-string">"value"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"total_time"</span> <!-- -->: <span class="lg-highlight-number">330</span>, <span class="lg-highlight-string">"count"</span> <!-- -->: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-string">"avg_time"</span> <!-- -->: <span class="lg-highlight-number">110</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"b"</span>, <span class="lg-highlight-string">"value"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"total_time"</span> <!-- -->: <span class="lg-highlight-number">270</span>, <span class="lg-highlight-string">"count"</span> <!-- -->: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-string">"avg_time"</span> <!-- -->: <span class="lg-highlight-number">90</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"c"</span>, <span class="lg-highlight-string">"value"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"total_time"</span> <!-- -->: <span class="lg-highlight-number">360</span>, <span class="lg-highlight-string">"count"</span> <!-- -->: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-string">"avg_time"</span> <!-- -->: <span class="lg-highlight-number">120</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"d"</span>, <span class="lg-highlight-string">"value"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"total_time"</span> <!-- -->: <span class="lg-highlight-number">210</span>, <span class="lg-highlight-string">"count"</span> <!-- -->: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-string">"avg_time"</span> <!-- -->: <span class="lg-highlight-number">70</span> } }</td></tr></tbody></table></code></pre>
      </div>
     </div>
    </div>
   </li>
   <li class="leafygreen-ui-d7p7b6">
    <p class="leafygreen-ui-1kp3ins">
     Optional. To avoid having to modify the aggregation pipeline's
     <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/match/#mongodb-pipeline-pipe.-match">
      <code class="leafygreen-ui-1l06pbn">
       $match
      </code>
     </a>
     date condition each time you run, you can define
wrap the aggregation in a helper function:
    </p>
    <script class="structured_data" type="application/ld+json">
     {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"updateSessionStats = function(startDate) {\n   db.usersessions.aggregate([\n      { $match: { ts: { $gte: startDate } } },\n      { $group: { _id: \"$userid\", total_time: { $sum: \"$length\" }, count: { $sum: 1 }, avg_time: { $avg: \"$length\" } } },\n      { $project: { value: { total_time: \"$total_time\", count: \"$count\", avg_time: \"$avg_time\" } } },\n      { $merge: {\n         into: \"session_stats_agg\",\n         whenMatched: [ { $set: {\n            \"value.total_time\": { $add: [ \"$value.total_time\", \"$$new.value.total_time\" ] },\n            \"value.count\": { $add: [ \"$value.count\", \"$$new.value.count\" ] },\n            \"value.avg_time\": { $divide: [ { $add: [ \"$value.total_time\", \"$$new.value.total_time\" ] },  { $add: [ \"$value.count\", \"$$new.value.count\" ] } ] }\n         } } ],\n         whenNotMatched: \"insert\"\n      }}\n   ]);\n};","programmingLanguage":"JavaScript"}
    </script>
    <div class="css-16lmvu9">
     <div class="leafygreen-ui-196mwvg">
      <div class="leafygreen-ui-1b1bvay">
       <pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">updateSessionStats <!-- -->= <span class="lg-highlight-keyword lg-highlight-function">function</span>(<span class="lg-highlight-params">startDate</span>)<!-- --> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->db.<span class="lg-highlight-property">usersessions</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">ts</span>: <!-- -->{ <span class="lg-highlight-attr">$gte</span>: <!-- -->startDate } } }<!-- -->,</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">"$userid"</span>, <span class="lg-highlight-attr">total_time</span>: <!-- -->{ <span class="lg-highlight-attr">$sum</span>: <span class="lg-highlight-string">"$length"</span> }<!-- -->, <span class="lg-highlight-attr">count</span>: <!-- -->{ <span class="lg-highlight-attr">$sum</span>: <span class="lg-highlight-number">1</span> }<!-- -->, <span class="lg-highlight-attr">avg_time</span>: <!-- -->{ <span class="lg-highlight-attr">$avg</span>: <span class="lg-highlight-string">"$length"</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">total_time</span>: <span class="lg-highlight-string">"$total_time"</span>, <span class="lg-highlight-attr">count</span>: <span class="lg-highlight-string">"$count"</span>, <span class="lg-highlight-attr">avg_time</span>: <span class="lg-highlight-string">"$avg_time"</span> } } }<!-- -->,</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">"session_stats_agg"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">         <span class="lg-highlight-attr">whenMatched</span>: <!-- -->[ <!-- -->{ <span class="lg-highlight-attr">$set</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">            <span class="lg-highlight-string">"value.total_time"</span>: <!-- -->{ <span class="lg-highlight-attr">$add</span>: <!-- -->[ <span class="lg-highlight-string">"$value.total_time"</span>, <span class="lg-highlight-string">"$$new.value.total_time"</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">            <span class="lg-highlight-string">"value.count"</span>: <!-- -->{ <span class="lg-highlight-attr">$add</span>: <!-- -->[ <span class="lg-highlight-string">"$value.count"</span>, <span class="lg-highlight-string">"$$new.value.count"</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">            <span class="lg-highlight-string">"value.avg_time"</span>: <!-- -->{ <span class="lg-highlight-attr">$divide</span>: <!-- -->[ <!-- -->{ <span class="lg-highlight-attr">$add</span>: <!-- -->[ <span class="lg-highlight-string">"$value.total_time"</span>, <span class="lg-highlight-string">"$$new.value.total_time"</span> ] }<!-- -->,  <!-- -->{ <span class="lg-highlight-attr">$add</span>: <!-- -->[ <span class="lg-highlight-string">"$value.count"</span>, <span class="lg-highlight-string">"$$new.value.count"</span> ] } ] }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">         } } ]<!-- -->,</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><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">
     Then, to run, you would just pass in the start date to the
     <code class="leafygreen-ui-1l06pbn">
      updateSessionStats()
     </code>
     function:
    </p>
    <script class="structured_data" type="application/ld+json">
     {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"updateSessionStats(ISODate('2020-03-05 00:00:00'))","programmingLanguage":"JavaScript"}
    </script>
    <div class="css-16lmvu9">
     <div class="leafygreen-ui-196mwvg">
      <div class="leafygreen-ui-yzcs8i">
       <pre class="leafygreen-ui-1q4bxgx" 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-title lg-highlight-function">updateSessionStats</span>(<span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">'2020-03-05 00:00:00'</span>))</td></tr></tbody></table></code></pre>
       <div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
        <button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
         <div class="leafygreen-ui-xhlipt">
          <svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
           <path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
           </path>
          </svg>
         </div>
        </button>
       </div>
      </div>
     </div>
    </div>
   </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">
    <ul class="leafygreen-ui-rioki0">
     <li class="leafygreen-ui-d7p7b6">
      <p class="leafygreen-ui-1kp3ins">
       <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/merge/#std-label-merge-examples">
        $merge Examples
       </a>
      </p>
     </li>
     <li class="leafygreen-ui-d7p7b6">
      <p class="leafygreen-ui-1kp3ins">
       <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/materialized-views/">
        On-Demand Materialized Views
       </a>
      </p>
     </li>
     <li class="leafygreen-ui-d7p7b6">
      <p class="leafygreen-ui-1kp3ins">
       <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/map-reduce-to-aggregation-pipeline/">
        Map-Reduce to Aggregation Pipeline
       </a>
      </p>
     </li>
    </ul>
   </div>
  </div>
 </section>
</section>
