<section>
 <h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
  Map-Reduce
  <a class="headerlink leafygreen-ui-1kepx47" href="#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="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="#map-reduce-javascript-functions" target="_self">
     <span>
      Map-Reduce JavaScript Functions
     </span>
    </a>
   </li>
   <li class="leafygreen-ui-1jqtqlb">
    <a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#map-reduce-results" target="_self">
     <span>
      Map-Reduce Results
     </span>
    </a>
   </li>
   <li class="leafygreen-ui-1jqtqlb">
    <a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#sharded-collections" target="_self">
     <span>
      Sharded Collections
     </span>
    </a>
   </li>
   <li class="leafygreen-ui-1jqtqlb">
    <a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#views" target="_self">
     <span>
      Views
     </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="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-map-reduce" target="_self">
     <span>
      map-reduce
     </span>
    </a>
    is
deprecated:
   </p>
   <ul class="leafygreen-ui-rioki0">
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      Instead of
      <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-map-reduce" target="_self">
       <span>
        map-reduce
       </span>
      </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>
  </div>
 </div>
 <a class="css-53dzkk" href="https://www.mongodb.com/docs/atlas/atlas-ui/agg-pipeline">
  <div class="css-z0l49d">
   <div class="css-fw7ipj">
    <svg aria-label="Cloud Icon" class="leafygreen-ui-1t2jgtk" fill="none" height="16" role="img" viewbox="0 0 16 16" width="16" xmlns="http://www.w3.org/2000/svg">
     <path d="M12.571 8.143c0 1.775-.899 3.34-2.267 4.264l-.014.01a5.12 5.12 0 0 1-2.861.869H2.857a2.857 2.857 0 0 1-.545-5.663 5.144 5.144 0 0 1 10.26.52ZM13.821 8.143a6.38 6.38 0 0 1-2.358 4.96 3.429 3.429 0 1 0 2.17-6.506c.123.494.188 1.013.188 1.546Z" fill="currentColor">
     </path>
    </svg>
   </div>
   <p class="leafygreen-ui-1kp3ins">
    You can
    <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/atlas-ui/agg-pipeline/" target="_self">
     <span>
      run aggregation pipelines in the UI
     </span>
    </a>
    for deployments hosted in
    <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas?tck=docs_server" target="_self">
     <span>
      MongoDB Atlas.
     </span>
    </a>
   </p>
  </div>
 </a>
 <p class="leafygreen-ui-1kp3ins">
  Map-reduce is a data processing paradigm for condensing large volumes
of data into useful
  <em>
   aggregated
  </em>
  results. 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>
  database
command.
 </p>
 <p class="leafygreen-ui-1kp3ins">
  Consider the following map-reduce operation:
 </p>
 <div class="figure css-5vkxs0" style="width: 760px;">
  <img alt="Diagram of the annotated map-reduce operation." class="leafygreen-ui-1qnf57e" height="700" src="/docs/manual/images/map-reduce.bakedsvg.svg" style="--border-color: #C1C7C6;" width="815"/>
 </div>
 <p class="leafygreen-ui-1kp3ins">
  In this map-reduce operation, MongoDB applies the
  <em>
   map
  </em>
  phase to each
input document (i.e. the documents in the collection that match the
query condition). The map function emits key-value pairs. For those
keys that have multiple values, MongoDB applies the
  <em>
   reduce
  </em>
  phase, which
collects and condenses the aggregated data. MongoDB then stores the results
in a collection. Optionally, the output of the reduce function may
pass through a
  <em>
   finalize
  </em>
  function to further condense or process the
results of the aggregation.
 </p>
 <p class="leafygreen-ui-1kp3ins">
  All map-reduce functions in MongoDB are JavaScript and run
within the
  <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
   <code class="leafygreen-ui-1l06pbn">
    mongod
   </code>
  </a>
  process. Map-reduce operations take the
documents of a single
  <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-collection">
   collection
  </a>
  as the
  <em>
   input
  </em>
  and can perform
any arbitrary sorting and limiting before beginning the map stage.
  <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/mapReduce/#mongodb-dbcommand-dbcmd.mapReduce">
   <code class="leafygreen-ui-1l06pbn">
    mapReduce
   </code>
  </a>
  can return the results of a map-reduce operation
as a document, or may write the results to collections.
 </p>
 <div class="leafygreen-ui-15mdk4" role="note">
  <h2 class="leafygreen-ui-6n7b7d">
   Note
  </h2>
  <div class="leafygreen-ui-9mycqc">
   <p class="leafygreen-ui-1kp3ins">
    Map-reduce is unsupported for MongoDB Atlas free clusters and
MongoDB Atlas serverless instances.
   </p>
  </div>
 </div>
 <section>
  <h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
   Map-Reduce JavaScript Functions
   <a class="headerlink leafygreen-ui-1kepx47" href="#map-reduce-javascript-functions" title="Permalink to this heading">
    <svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
     <path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
     </path>
     <path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
     </path>
    </svg>
    <div class="css-1btu0w3 evvkmar0" id="map-reduce-javascript-functions">
    </div>
   </a>
  </h2>
  <p class="leafygreen-ui-1kp3ins">
   In MongoDB, map-reduce operations use custom JavaScript functions to
   <em>
    map
   </em>
   , or associate, values to a key. If a key has multiple values
mapped to it, the operation
   <em>
    reduces
   </em>
   the values for the key to a
single object.
  </p>
  <p class="leafygreen-ui-1kp3ins">
   The use of custom JavaScript functions provide flexibility to
map-reduce operations. For instance, when processing a document, the
map function can create more than one key and value mapping or no
mapping. Map-reduce operations can also use a custom JavaScript
function to make final modifications to the results at the end of the
map and reduce operation, such as perform additional calculations.
  </p>
 </section>
 <section>
  <h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
   Map-Reduce Results
   <a class="headerlink leafygreen-ui-1kepx47" href="#map-reduce-results" title="Permalink to this heading">
    <svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
     <path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
     </path>
     <path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
     </path>
    </svg>
    <div class="css-1btu0w3 evvkmar0" id="map-reduce-results">
    </div>
   </a>
  </h2>
  <p class="leafygreen-ui-1kp3ins">
   In MongoDB, the map-reduce operation can write results to a collection
or return the results inline. If you write map-reduce output to a
collection, you can perform subsequent map-reduce operations on the
same input collection that merge replace, merge, or reduce new results
with previous results. See
   <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/mapReduce/#mongodb-dbcommand-dbcmd.mapReduce">
    <code class="leafygreen-ui-1l06pbn">
     mapReduce
    </code>
   </a>
   and
   <a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/perform-incremental-map-reduce/">
    Perform Incremental Map-Reduce
   </a>
   for details and
examples.
  </p>
  <p class="leafygreen-ui-1kp3ins">
   When returning the results of a map-reduce operation
   <em>
    inline
   </em>
   , the
result documents must be within the
   <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/limits/#mongodb-limit-BSON-Document-Size">
    BSON Document Size
   </a>
   limit,
which is currently 16 megabytes. For additional information on limits
and restrictions on map-reduce operations, see the
   <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/mapReduce/">
    mapReduce
   </a>
   reference page.
  </p>
 </section>
 <section>
  <h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
   Sharded Collections
   <a class="headerlink leafygreen-ui-1kepx47" href="#sharded-collections" title="Permalink to this heading">
    <svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
     <path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
     </path>
     <path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
     </path>
    </svg>
    <div class="css-1btu0w3 evvkmar0" id="sharded-collections">
    </div>
   </a>
  </h2>
  <p class="leafygreen-ui-1kp3ins">
   MongoDB supports map-reduce operations on
   <a class="leafygreen-ui-tqgtui" href="/docs/manual/sharding/#std-label-sharding-background">
    sharded collections.
   </a>
  </p>
 </section>
 <section>
  <h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
   Views
   <a class="headerlink leafygreen-ui-1kepx47" href="#views" 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="views">
    </div>
   </a>
  </h2>
  <p class="leafygreen-ui-1kp3ins">
   <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/views/#std-label-views-landing-page">
    Views
   </a>
   do not support map-reduce operations.
  </p>
 </section>
</section>
