<section>
 <h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
  Aggregation Commands Comparison
  <a class="headerlink leafygreen-ui-1kepx47" href="#aggregation-commands-comparison" 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-commands-comparison">
   </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="#aggregation-commands-comparison-table" target="_self">
     <span>
      Aggregation Commands Comparison Table
     </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 to Map-Reduce
   </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>
  </div>
 </div>
 <section>
  <h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
   Aggregation Commands Comparison Table
   <a class="headerlink leafygreen-ui-1kepx47" href="#aggregation-commands-comparison-table" title="Permalink to this heading">
    <svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
     <path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
     </path>
     <path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
     </path>
    </svg>
    <div class="css-1btu0w3 evvkmar0" id="aggregation-commands-comparison-table">
    </div>
   </a>
  </h2>
  <p class="leafygreen-ui-1kp3ins">
   The following table provides a brief overview of the features of the
MongoDB aggregation commands.
  </p>
  <div class="leafygreen-ui-skkc39" tabindex="0">
   <table class="leafygreen-ui-hr8lsv">
    <colgroup>
     <col style="width:30%"/>
     <col style="width:40%"/>
     <col style="width:40%"/>
    </colgroup>
    <thead class="leafygreen-ui-a9ho40">
     <tr data-testid="leafygreen-ui-header-row">
      <th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
       <div class="leafygreen-ui-ui0te9">
        <div>
        </div>
       </div>
      </th>
      <th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
       <div class="leafygreen-ui-ui0te9">
        <div>
         <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/aggregate/#mongodb-dbcommand-dbcmd.aggregate">
          <code class="leafygreen-ui-1l06pbn">
           aggregate
          </code>
         </a>
         /
         <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.aggregate/#mongodb-method-db.collection.aggregate">
          <code class="leafygreen-ui-1l06pbn">
           db.collection.aggregate()
          </code>
         </a>
        </div>
       </div>
      </th>
      <th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
       <div class="leafygreen-ui-ui0te9">
        <div>
         <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/mapReduce/#mongodb-dbcommand-dbcmd.mapReduce">
          <code class="leafygreen-ui-1l06pbn">
           mapReduce
          </code>
         </a>
         /
         <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>
        </div>
       </div>
      </th>
     </tr>
    </thead>
    <tbody>
     <tr class="leafygreen-ui-15dvrnp">
      <td class="leafygreen-ui-xejatx" role="rowheader">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         Description
        </div>
       </div>
      </td>
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         <p class="leafygreen-ui-1kp3ins">
          Designed with specific goals of improving performance and
usability for aggregation tasks.
         </p>
         <p class="leafygreen-ui-1kp3ins">
          Uses a "pipeline" approach where objects are transformed as they
pass through a series of pipeline operators such as
          <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/group/#mongodb-pipeline-pipe.-group">
           <code class="leafygreen-ui-1l06pbn">
            $group
           </code>
          </a>
          ,
          <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/match/#mongodb-pipeline-pipe.-match">
           <code class="leafygreen-ui-1l06pbn">
            $match
           </code>
          </a>
          , and
          <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/sort/#mongodb-pipeline-pipe.-sort">
           <code class="leafygreen-ui-1l06pbn">
            $sort
           </code>
           .
          </a>
         </p>
         <p class="leafygreen-ui-1kp3ins">
          See
          <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/">
           Aggregation Operators
          </a>
          for more information
on the pipeline operators.
         </p>
        </div>
       </div>
      </td>
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         Implements the Map-Reduce aggregation for processing large data sets.
        </div>
       </div>
      </td>
     </tr>
     <tr class="leafygreen-ui-15dvrnp">
      <td class="leafygreen-ui-xejatx" role="rowheader">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         Key Features
        </div>
       </div>
      </td>
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         <p class="leafygreen-ui-1kp3ins">
          Pipeline operators can be repeated as needed.
         </p>
         <p class="leafygreen-ui-1kp3ins">
          Pipeline operators need not produce one output document for every
input document.
         </p>
         <p class="leafygreen-ui-1kp3ins">
          Can also generate new documents or filter out documents.
         </p>
         <p class="leafygreen-ui-1kp3ins">
          Using 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, you can create on-demand materialized
views, where the content of the output collection can be updated
incrementally the pipeline is run.
          <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/merge/#mongodb-pipeline-pipe.-merge">
           <code class="leafygreen-ui-1l06pbn">
            $merge
           </code>
          </a>
          can incorporate
results (insert new documents, merge documents, replace documents, keep
existing documents, fail the operation, process documents with a custom
update pipeline) into an existing collection.
         </p>
        </div>
       </div>
      </td>
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         <p class="leafygreen-ui-1kp3ins">
          In addition to grouping operations, can perform complex
aggregation tasks as well as perform incremental aggregation on
continuously growing datasets.
         </p>
         <p class="leafygreen-ui-1kp3ins">
          See
          <a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/map-reduce-examples/">
           Map-Reduce Examples
          </a>
          and
          <a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/perform-incremental-map-reduce/">
           Perform Incremental Map-Reduce
           <!-- -->
           .
          </a>
         </p>
        </div>
       </div>
      </td>
     </tr>
     <tr class="leafygreen-ui-15dvrnp">
      <td class="leafygreen-ui-xejatx" role="rowheader">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         Flexibility
        </div>
       </div>
      </td>
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         <p class="leafygreen-ui-1kp3ins">
          You can define custom aggregation expressions with
          <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>
         </p>
         <p class="leafygreen-ui-1kp3ins">
          You can also add computed fields, create new virtual sub-objects, and
extract sub-fields into the top-level of results by using the
          <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/project/#mongodb-pipeline-pipe.-project">
           <code class="leafygreen-ui-1l06pbn">
            $project
           </code>
          </a>
          pipeline operator.
         </p>
         <p class="leafygreen-ui-1kp3ins">
          See
          <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/project/#mongodb-pipeline-pipe.-project">
           <code class="leafygreen-ui-1l06pbn">
            $project
           </code>
          </a>
          for more information as well as
          <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/">
           Aggregation Operators
          </a>
          for more information on all
the available pipeline operators.
         </p>
        </div>
       </div>
      </td>
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         <p class="leafygreen-ui-1kp3ins">
          Custom
          <code class="leafygreen-ui-1l06pbn">
           map
          </code>
          ,
          <code class="leafygreen-ui-1l06pbn">
           reduce
          </code>
          and
          <code class="leafygreen-ui-1l06pbn">
           finalize
          </code>
          JavaScript
functions offer flexibility to aggregation logic.
         </p>
         <p class="leafygreen-ui-1kp3ins">
          See
          <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/mapReduce/#mongodb-dbcommand-dbcmd.mapReduce">
           <code class="leafygreen-ui-1l06pbn">
            mapReduce
           </code>
          </a>
          for details and restrictions
on the functions.
         </p>
        </div>
       </div>
      </td>
     </tr>
     <tr class="leafygreen-ui-15dvrnp">
      <td class="leafygreen-ui-xejatx" role="rowheader">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         Output Results
        </div>
       </div>
      </td>
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         <p class="leafygreen-ui-1kp3ins">
          Returns results as a cursor. If the pipeline includes the
          <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/out/#mongodb-pipeline-pipe.-out">
           <code class="leafygreen-ui-1l06pbn">
            $out
           </code>
          </a>
          stage or
          <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, the cursor
is empty.
         </p>
         <p class="leafygreen-ui-1kp3ins">
          With
          <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/out/#mongodb-pipeline-pipe.-out">
           <code class="leafygreen-ui-1l06pbn">
            $out
           </code>
          </a>
          , you can replace an existing output
collection completely or output to a new collection. See
          <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/out/#mongodb-pipeline-pipe.-out">
           <code class="leafygreen-ui-1l06pbn">
            $out
           </code>
          </a>
          for details.
         </p>
         <p class="leafygreen-ui-1kp3ins">
          With
          <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/merge/#mongodb-pipeline-pipe.-merge">
           <code class="leafygreen-ui-1l06pbn">
            $merge
           </code>
          </a>
          , you can output to a new or existing
collection. For existing cllections, you can specify how to
incorporate the results into the output collection (insert new
documents, merge documents, replace documents, keep existing
documents, fail the operation, process documents with a custom
update pipeline). See
          <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/merge/#mongodb-pipeline-pipe.-merge">
           <code class="leafygreen-ui-1l06pbn">
            $merge
           </code>
          </a>
          for details.
         </p>
        </div>
       </div>
      </td>
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         Returns results in various options (inline, new collection, merge,
replace, reduce). See
         <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/mapReduce/#mongodb-dbcommand-dbcmd.mapReduce">
          <code class="leafygreen-ui-1l06pbn">
           mapReduce
          </code>
         </a>
         for details on the
output options.
        </div>
       </div>
      </td>
     </tr>
     <tr class="leafygreen-ui-15dvrnp">
      <td class="leafygreen-ui-xejatx" role="rowheader">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         Sharding
        </div>
       </div>
      </td>
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         <p class="leafygreen-ui-1kp3ins">
          Supports non-sharded and sharded input collections.
         </p>
         <p class="leafygreen-ui-1kp3ins">
          <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/merge/#mongodb-pipeline-pipe.-merge">
           <code class="leafygreen-ui-1l06pbn">
            $merge
           </code>
          </a>
          can output to a non-sharded or sharded
collection.
         </p>
        </div>
       </div>
      </td>
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         Supports non-sharded and sharded input collections.
        </div>
       </div>
      </td>
     </tr>
     <tr class="leafygreen-ui-15dvrnp">
      <td class="leafygreen-ui-xejatx" role="rowheader">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         More Information
        </div>
       </div>
      </td>
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         <ul class="leafygreen-ui-rioki0">
          <li class="leafygreen-ui-d7p7b6">
           <p class="leafygreen-ui-1kp3ins">
            <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/aggregation-pipeline/#std-label-aggregation-pipeline">
             Aggregation Pipeline
            </a>
           </p>
          </li>
          <li class="leafygreen-ui-d7p7b6">
           <p class="leafygreen-ui-1kp3ins">
            <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.aggregate/#mongodb-method-db.collection.aggregate">
             <code class="leafygreen-ui-1l06pbn">
              db.collection.aggregate()
             </code>
            </a>
           </p>
          </li>
          <li class="leafygreen-ui-d7p7b6">
           <p class="leafygreen-ui-1kp3ins">
            <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/aggregate/#mongodb-dbcommand-dbcmd.aggregate">
             <code class="leafygreen-ui-1l06pbn">
              aggregate
             </code>
            </a>
           </p>
          </li>
         </ul>
        </div>
       </div>
      </td>
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         <ul class="leafygreen-ui-rioki0">
          <li class="leafygreen-ui-d7p7b6">
           <p class="leafygreen-ui-1kp3ins">
            <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/map-reduce/">
             Map-Reduce
            </a>
           </p>
          </li>
          <li class="leafygreen-ui-d7p7b6">
           <p class="leafygreen-ui-1kp3ins">
            <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>
           </p>
          </li>
          <li class="leafygreen-ui-d7p7b6">
           <p class="leafygreen-ui-1kp3ins">
            <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/mapReduce/#mongodb-dbcommand-dbcmd.mapReduce">
             <code class="leafygreen-ui-1l06pbn">
              mapReduce
             </code>
             .
            </a>
           </p>
          </li>
         </ul>
        </div>
       </div>
      </td>
     </tr>
    </tbody>
   </table>
  </div>
  <div class="leafygreen-ui-13vw3ie" role="note">
   <h2 class="leafygreen-ui-1fn4p2b">
    Tip
   </h2>
   <h3 class="leafygreen-ui-144d5kf">
    <strong>
     See also:
    </strong>
   </h3>
   <div class="leafygreen-ui-9mycqc">
    <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>
   </div>
  </div>
 </section>
</section>
