<section>
 <h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
  $out (aggregation)
  <a class="headerlink leafygreen-ui-1kepx47" href="#-out--aggregation-" 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="-out--aggregation-">
   </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="#definition" target="_self">
     <span>
      Definition
     </span>
    </a>
   </li>
   <li class="leafygreen-ui-1jqtqlb">
    <a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#syntax" target="_self">
     <span>
      Syntax
     </span>
    </a>
   </li>
   <li class="leafygreen-ui-1jqtqlb">
    <a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#behaviors" target="_self">
     <span>
      Behaviors
     </span>
    </a>
   </li>
   <li class="leafygreen-ui-1jqtqlb">
    <a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#examples" target="_self">
     <span>
      Examples
     </span>
    </a>
   </li>
  </ul>
 </div>
 <section>
  <h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
   Definition
   <a class="headerlink leafygreen-ui-1kepx47" href="#definition" 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="definition">
    </div>
   </a>
  </h2>
  <dl class="pipeline">
   <dt>
    <code class="leafygreen-ui-1l06pbn">
     $out
    </code>
    <a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-pipeline-pipe.-out" title="Permalink to this definition">
     <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="mongodb-pipeline-pipe.-out">
     </div>
    </a>
   </dt>
   <dd>
    <p class="leafygreen-ui-1kp3ins">
     Takes the documents returned by the aggregation pipeline and writes
them to a specified collection. You can specify the output database.
    </p>
    <p class="leafygreen-ui-1kp3ins">
     The
     <code class="leafygreen-ui-1l06pbn">
      $out
     </code>
     stage must be
     <em>
      the last stage
     </em>
     in the
pipeline. The
     <code class="leafygreen-ui-1l06pbn">
      $out
     </code>
     operator lets the aggregation
framework return result sets of any size.
    </p>
    <div class="leafygreen-ui-1sjx7ru" role="note">
     <h2 class="leafygreen-ui-29m78u">
      Warning
     </h2>
     <div class="leafygreen-ui-9mycqc">
      <p class="leafygreen-ui-1kp3ins">
       If the collection specified by the
       <code class="leafygreen-ui-1l06pbn">
        $out
       </code>
       operation already
exists, then the
       <code class="leafygreen-ui-1l06pbn">
        $out
       </code>
       stage atomically replaces the existing
collection with the new results collection upon completion of the
aggregation. See
       <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-replace-existing-collection" target="_self">
        <span>
         Replace Existing Collection
        </span>
       </a>
       for details.
      </p>
     </div>
    </div>
   </dd>
  </dl>
 </section>
 <section>
  <h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
   Syntax
   <a class="headerlink leafygreen-ui-1kepx47" href="#syntax" 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="syntax">
    </div>
   </a>
  </h2>
  <p class="leafygreen-ui-1kp3ins">
   The
   <code class="leafygreen-ui-1l06pbn">
    $out
   </code>
   stage has the following syntax:
  </p>
  <ul class="leafygreen-ui-rioki0">
   <li class="leafygreen-ui-d7p7b6">
    <p class="leafygreen-ui-1kp3ins">
     <code class="leafygreen-ui-1l06pbn">
      $out
     </code>
     can take a string to specify only the output
collection (i.e. output to a collection in the same database):
    </p>
    <script class="structured_data" type="application/ld+json">
     {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ $out: \"&lt;output-collection&gt;\" } // Output collection is in the same database","programmingLanguage":"JavaScript"}
    </script>
    <div class="css-16lmvu9">
     <div class="leafygreen-ui-196mwvg">
      <div class="leafygreen-ui-1b1bvay">
       <pre class="leafygreen-ui-1q4bxgx" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">$out</span>: <span class="lg-highlight-string">"&lt;output-collection&gt;"</span> } <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> Output collection is </span><span class="lg-highlight-comment">in the same database</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>
   <li class="leafygreen-ui-d7p7b6">
    <p class="leafygreen-ui-1kp3ins">
     <code class="leafygreen-ui-1l06pbn">
      $out
     </code>
     can take a document to specify the output database as well as the
output collection:
    </p>
    <script class="structured_data" type="application/ld+json">
     {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ $out: { db: \"&lt;output-db&gt;\", coll: \"&lt;output-collection&gt;\" } }","programmingLanguage":"JavaScript"}
    </script>
    <div class="css-16lmvu9">
     <div class="leafygreen-ui-196mwvg">
      <div class="leafygreen-ui-1b1bvay">
       <pre class="leafygreen-ui-1q4bxgx" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">$out</span>: <!-- -->{ <span class="lg-highlight-attr">db</span>: <span class="lg-highlight-string">"&lt;output-db&gt;"</span>, <span class="lg-highlight-attr">coll</span>: <span class="lg-highlight-string">"&lt;output-collection&gt;"</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>
   <li class="leafygreen-ui-d7p7b6">
    <p class="leafygreen-ui-1kp3ins">
     Starting in MongoDB 7.0.3 and 7.1,
     <code class="leafygreen-ui-1l06pbn">
      $out
     </code>
     can take a document to
output to a
     <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/timeseries-collections/#std-label-manual-timeseries-landing">
      time series collection
      <!-- -->
      :
     </a>
    </p>
    <script class="structured_data" type="application/ld+json">
     {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ $out:\n  { db: \"&lt;output-db&gt;\", coll: \"&lt;output-collection&gt;\",\n    timeseries: {\n      timeField: \"&lt;field-name&gt;\",\n      metaField: \"&lt;field-name&gt;\",\n      granularity:  \"seconds\" || \"minutes\" || \"hours\" ,\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">{ <span class="lg-highlight-attr">$out</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->{ <span class="lg-highlight-attr">db</span>: <span class="lg-highlight-string">"&lt;output-db&gt;"</span>, <span class="lg-highlight-attr">coll</span>: <span class="lg-highlight-string">"&lt;output-collection&gt;"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">    <span class="lg-highlight-attr">timeseries</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <span class="lg-highlight-attr">timeField</span>: <span class="lg-highlight-string">"&lt;field-name&gt;"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <span class="lg-highlight-attr">metaField</span>: <span class="lg-highlight-string">"&lt;field-name&gt;"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <span class="lg-highlight-attr">granularity</span>:  <span class="lg-highlight-string">"seconds"</span> <!-- -->|<!-- -->| <span class="lg-highlight-string">"minutes"</span> <!-- -->|<!-- -->| <span class="lg-highlight-string">"hours"</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>
    <div class="leafygreen-ui-85da11" role="note">
     <h2 class="leafygreen-ui-1ac4g5h">
      Important
     </h2>
     <h3 class="leafygreen-ui-144d5kf">
      <strong>
       Changing Time Series Granularity
      </strong>
     </h3>
     <div class="leafygreen-ui-9mycqc">
      <p class="leafygreen-ui-1kp3ins">
       After creating a time series collection, you can modify its
granularity using the
       <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/collMod/#mongodb-dbcommand-dbcmd.collMod">
        <code class="leafygreen-ui-1l06pbn">
         collMod
        </code>
       </a>
       method. However,
you can only increase the timespan covered by each bucket. You
cannot decrease it.
      </p>
     </div>
    </div>
    <div class="leafygreen-ui-skkc39" tabindex="0">
     <table class="leafygreen-ui-hr8lsv">
      <colgroup>
       <col style="width:15%"/>
       <col style="width:85%"/>
      </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>
           Field
          </div>
         </div>
        </th>
        <th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
         <div class="leafygreen-ui-ui0te9">
          <div>
           Description
          </div>
         </div>
        </th>
       </tr>
      </thead>
      <tbody>
       <tr class="leafygreen-ui-15dvrnp">
        <td class="leafygreen-ui-1drjk7h">
         <div class="leafygreen-ui-1n4j8xf">
          <div>
           <code class="leafygreen-ui-1l06pbn">
            db
           </code>
          </div>
         </div>
        </td>
        <td class="leafygreen-ui-1drjk7h">
         <div class="leafygreen-ui-1n4j8xf">
          <div>
           <span class="leafygreen-ui-1qnjd6d" id="std-label-out-db">
           </span>
           <p class="leafygreen-ui-1kp3ins">
            The output database name.
           </p>
           <ul class="leafygreen-ui-rioki0">
            <li class="leafygreen-ui-d7p7b6">
             <p class="leafygreen-ui-1kp3ins">
              For a
              <a class="leafygreen-ui-tqgtui" href="/docs/manual/replication/#std-label-replica-set">
               replica set
              </a>
              or a standalone, if the
output database does not exist,
              <code class="leafygreen-ui-1l06pbn">
               $out
              </code>
              also creates
the database.
             </p>
            </li>
           </ul>
          </div>
         </div>
        </td>
       </tr>
       <tr class="leafygreen-ui-15dvrnp">
        <td class="leafygreen-ui-1drjk7h">
         <div class="leafygreen-ui-1n4j8xf">
          <div>
           <code class="leafygreen-ui-1l06pbn">
            coll
           </code>
          </div>
         </div>
        </td>
        <td class="leafygreen-ui-1drjk7h">
         <div class="leafygreen-ui-1n4j8xf">
          <div>
           <span class="leafygreen-ui-1qnjd6d" id="std-label-out-collection">
           </span>
           <p class="leafygreen-ui-1kp3ins">
            The output collection name.
           </p>
          </div>
         </div>
        </td>
       </tr>
       <tr class="leafygreen-ui-15dvrnp">
        <td class="leafygreen-ui-1drjk7h">
         <div class="leafygreen-ui-1n4j8xf">
          <div>
           <code class="leafygreen-ui-1l06pbn">
            timeseries
           </code>
          </div>
         </div>
        </td>
        <td class="leafygreen-ui-1drjk7h">
         <div class="leafygreen-ui-1n4j8xf">
          <div>
           <span class="leafygreen-ui-1qnjd6d" id="std-label-out-timeseries">
           </span>
           <p class="leafygreen-ui-1kp3ins">
            A document that specifies the configuration to use when writing
to a time series collection. The
            <code class="leafygreen-ui-1l06pbn">
             timeField
            </code>
            is required. All
other fields are optional.
           </p>
          </div>
         </div>
        </td>
       </tr>
       <tr class="leafygreen-ui-15dvrnp">
        <td class="leafygreen-ui-1drjk7h">
         <div class="leafygreen-ui-1n4j8xf">
          <div>
           <code class="leafygreen-ui-1l06pbn">
            timeField
           </code>
          </div>
         </div>
        </td>
        <td class="leafygreen-ui-1drjk7h">
         <div class="leafygreen-ui-1n4j8xf">
          <div>
           <span class="leafygreen-ui-1qnjd6d" id="std-label-out-timefield">
           </span>
           <p class="leafygreen-ui-1kp3ins">
            Required when writing to a time series collection.
.. include::
/includes/time-series/fact-time-field-description.rst
           </p>
          </div>
         </div>
        </td>
       </tr>
       <tr class="leafygreen-ui-15dvrnp">
        <td class="leafygreen-ui-1drjk7h">
         <div class="leafygreen-ui-1n4j8xf">
          <div>
           <code class="leafygreen-ui-1l06pbn">
            metaField
           </code>
          </div>
         </div>
        </td>
        <td class="leafygreen-ui-1drjk7h">
         <div class="leafygreen-ui-1n4j8xf">
          <div>
           <span class="leafygreen-ui-1qnjd6d" id="std-label-out-metafield">
           </span>
           <p class="leafygreen-ui-1kp3ins">
            Optional. The name of the field which contains metadata in
each time series document. The metadata in the specified field
should be data that is used to label a unique series of
documents. The metadata should rarely, if ever, change
The name of the specified field may not be
            <code class="leafygreen-ui-1l06pbn">
             _id
            </code>
            or the same
as the
            <code class="leafygreen-ui-1l06pbn">
             timeseries.timeField
            </code>
            . The field can be of any data type.
           </p>
           <p class="leafygreen-ui-1kp3ins">
            Although the
            <code class="leafygreen-ui-1l06pbn">
             metaField
            </code>
            field is optional, using metadata can improve
query optimization. For example, MongoDB automatically
            <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/timeseries/timeseries-secondary-index/#std-label-timeseries-add-secondary-index">
             creates a compound index
            </a>
            on the
            <code class="leafygreen-ui-1l06pbn">
             metaField
            </code>
            and
            <code class="leafygreen-ui-1l06pbn">
             timeField
            </code>
            fields for new collections.
If you do not provide a value for this field, the data is bucketed solely
based on time.
           </p>
          </div>
         </div>
        </td>
       </tr>
       <tr class="leafygreen-ui-15dvrnp">
        <td class="leafygreen-ui-1drjk7h">
         <div class="leafygreen-ui-1n4j8xf">
          <div>
           <code class="leafygreen-ui-1l06pbn">
            granularity
           </code>
          </div>
         </div>
        </td>
        <td class="leafygreen-ui-1drjk7h">
         <div class="leafygreen-ui-1n4j8xf">
          <div>
           <span class="leafygreen-ui-1qnjd6d" id="std-label-out-granularity">
           </span>
           <p class="leafygreen-ui-1kp3ins">
            Optional. Do not use if setting
            <code class="leafygreen-ui-1l06pbn">
             bucketRoundingSeconds
            </code>
            and
            <code class="leafygreen-ui-1l06pbn">
             bucketMaxSpanSeconds
            </code>
            .
           </p>
           <p class="leafygreen-ui-1kp3ins">
            Possible values are
            <code class="leafygreen-ui-1l06pbn">
             seconds
            </code>
            (default),
            <code class="leafygreen-ui-1l06pbn">
             minutes
            </code>
            , and
            <code class="leafygreen-ui-1l06pbn">
             hours
            </code>
            .
           </p>
           <p class="leafygreen-ui-1kp3ins">
            Set
            <code class="leafygreen-ui-1l06pbn">
             granularity
            </code>
            to the value that most closely matches
the time between consecutive incoming timestamps. This
improves performance by optimizing how MongoDB stores data in the
collection.
           </p>
           <p class="leafygreen-ui-1kp3ins">
            For more information on granularity and bucket intervals, see
            <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/timeseries/timeseries-granularity/#std-label-timeseries-granularity">
             Set Granularity for Time Series Data
             <!-- -->
             .
            </a>
           </p>
          </div>
         </div>
        </td>
       </tr>
       <tr class="leafygreen-ui-15dvrnp">
        <td class="leafygreen-ui-1drjk7h">
         <div class="leafygreen-ui-1n4j8xf">
          <div>
           <code class="leafygreen-ui-1l06pbn">
            bucketMaxSpanSeconds
           </code>
          </div>
         </div>
        </td>
        <td class="leafygreen-ui-1drjk7h">
         <div class="leafygreen-ui-1n4j8xf">
          <div>
           <span class="leafygreen-ui-1qnjd6d" id="std-label-out-bucketmaxspanseconds">
           </span>
           <p class="leafygreen-ui-1kp3ins">
            Optional. Use with
            <code class="leafygreen-ui-1l06pbn">
             bucketRoundingSeconds
            </code>
            as an alternative
to
            <code class="leafygreen-ui-1l06pbn">
             granularity
            </code>
            . Sets the maximum time between timestamps
in the same bucket.
           </p>
           <p class="leafygreen-ui-1kp3ins">
            Possible values are 1-31536000.
           </p>
           <div>
            <p>
             <em>
              New
              <!-- -->
              in version
              <!-- -->
              6.3
             </em>
             .
            </p>
           </div>
          </div>
         </div>
        </td>
       </tr>
       <tr class="leafygreen-ui-15dvrnp">
        <td class="leafygreen-ui-1drjk7h">
         <div class="leafygreen-ui-1n4j8xf">
          <div>
           <code class="leafygreen-ui-1l06pbn">
            bucketRoundingSeconds
           </code>
          </div>
         </div>
        </td>
        <td class="leafygreen-ui-1drjk7h">
         <div class="leafygreen-ui-1n4j8xf">
          <div>
           <span class="leafygreen-ui-1qnjd6d" id="std-label-out-bucketroundingseconds">
           </span>
           <p class="leafygreen-ui-1kp3ins">
            Optional. Use with
            <code class="leafygreen-ui-1l06pbn">
             bucketMaxSpanSeconds
            </code>
            as an alternative
to
            <code class="leafygreen-ui-1l06pbn">
             granularity
            </code>
            . Must be equal to
            <code class="leafygreen-ui-1l06pbn">
             bucketMaxSpanSeconds
            </code>
            .
           </p>
           <p class="leafygreen-ui-1kp3ins">
            When a document requires a new bucket, MongoDB rounds down the
document's timestamp value by this interval to set the minimum
time for the bucket.
           </p>
           <div>
            <p>
             <em>
              New
              <!-- -->
              in version
              <!-- -->
              6.3
             </em>
             .
            </p>
           </div>
          </div>
         </div>
        </td>
       </tr>
      </tbody>
     </table>
    </div>
   </li>
  </ul>
  <div class="leafygreen-ui-85da11" role="note">
   <h2 class="leafygreen-ui-1ac4g5h">
    Important
   </h2>
   <div class="leafygreen-ui-9mycqc">
    <ul class="leafygreen-ui-rioki0">
     <li class="leafygreen-ui-d7p7b6">
      <p class="leafygreen-ui-1kp3ins">
       You cannot specify a sharded collection as the output
collection. The input collection for a pipeline can be sharded.
To output to a sharded collection, 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>
      </p>
     </li>
     <li class="leafygreen-ui-d7p7b6">
      <p class="leafygreen-ui-1kp3ins">
       The
       <code class="leafygreen-ui-1l06pbn">
        $out
       </code>
       operator cannot write results to a
       <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/capped-collections/#std-label-manual-capped-collection">
        capped collection
        <!-- -->
        .
       </a>
      </p>
     </li>
     <li class="leafygreen-ui-d7p7b6">
      <p class="leafygreen-ui-1kp3ins">
       If you modify a collection with an
       <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/atlas-search/" target="_self">
        <span>
         Atlas Search
        </span>
       </a>
       index, you must first delete and then re-create
the search index. Consider using
       <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/merge/#mongodb-pipeline-pipe.-merge">
        <code class="leafygreen-ui-1l06pbn">
         $merge
        </code>
       </a>
       instead.
      </p>
     </li>
    </ul>
   </div>
  </div>
  <span class="leafygreen-ui-1qnjd6d" id="std-label-out-merge-comparison">
  </span>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    Comparison with
    <code class="leafygreen-ui-1l06pbn">
     $merge
    </code>
    <a class="headerlink leafygreen-ui-1kepx47" href="#comparison-with--merge" 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="comparison-with--merge">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    MongoDB provides two stages,
    <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
    <code class="leafygreen-ui-1l06pbn">
     $out
    </code>
    , for
writing the results of the aggregation pipeline to a collection. The
following summarizes the capabilities of the two stages:
   </p>
   <div class="leafygreen-ui-skkc39" tabindex="0">
    <table class="leafygreen-ui-hr8lsv">
     <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>
          <code class="leafygreen-ui-1l06pbn">
           $out
          </code>
         </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/operator/aggregation/merge/#mongodb-pipeline-pipe.-merge">
           <code class="leafygreen-ui-1l06pbn">
            $merge
           </code>
          </a>
         </div>
        </div>
       </th>
      </tr>
     </thead>
     <tbody>
      <tr class="leafygreen-ui-15dvrnp">
       <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">
             Can output to a collection in the same or different database.
            </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">
             Can output to a collection in the same or different database.
            </p>
           </li>
          </ul>
         </div>
        </div>
       </td>
      </tr>
      <tr class="leafygreen-ui-15dvrnp">
       <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">
             Creates a new collection if the output collection does not
already exist.
            </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">
             Creates a new collection if the output collection does not
already exist.
            </p>
           </li>
          </ul>
         </div>
        </div>
       </td>
      </tr>
      <tr class="leafygreen-ui-15dvrnp">
       <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">
             Replaces the output collection completely if it already exists.
            </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">
             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>
            <p class="leafygreen-ui-1kp3ins">
             Can replace the content of the collection but only if the
aggregation results contain a match for all existing
documents in the collection.
            </p>
           </li>
          </ul>
         </div>
        </div>
       </td>
      </tr>
      <tr class="leafygreen-ui-15dvrnp">
       <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">
             Cannot output to a sharded collection. Input collection,
however, can be sharded.
            </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">
             Can output to a sharded collection. Input collection can
also be sharded.
            </p>
           </li>
          </ul>
         </div>
        </div>
       </td>
      </tr>
      <tr class="leafygreen-ui-15dvrnp">
       <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">
             Starting in MongoDB 7.0.3 and 7.1, can output to a time series
collection.
            </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">
             Cannot output to a time series collection.
            </p>
           </li>
          </ul>
         </div>
        </div>
       </td>
      </tr>
      <tr class="leafygreen-ui-15dvrnp">
       <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">
             Corresponds to the SQL statements:
            </p>
            <ul class="leafygreen-ui-rioki0">
             <li class="leafygreen-ui-d7p7b6">
              <script class="structured_data" type="application/ld+json">
               {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"INSERT INTO T2 SELECT * FROM T1","programmingLanguage":"SQL"}
              </script>
              <div class="css-1lju8ps">
               <div class="leafygreen-ui-196mwvg">
                <div class="leafygreen-ui-tyg50d">
                 <pre class="leafygreen-ui-1agdxuk" tabindex="0"><code class="lg-highlight-hljs-light sql leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">INSERT</span> <span class="lg-highlight-keyword">INTO</span> T2 <span class="lg-highlight-keyword">SELECT</span> <span class="lg-highlight-operator">*</span> <span class="lg-highlight-keyword">FROM</span> T1</td></tr></tbody></table></code></pre>
                </div>
               </div>
              </div>
             </li>
             <li class="leafygreen-ui-d7p7b6">
              <script class="structured_data" type="application/ld+json">
               {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"SELECT * INTO T2 FROM T1","programmingLanguage":"SQL"}
              </script>
              <div class="css-1lju8ps">
               <div class="leafygreen-ui-196mwvg">
                <div class="leafygreen-ui-tyg50d">
                 <pre class="leafygreen-ui-1agdxuk" tabindex="0"><code class="lg-highlight-hljs-light sql leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">SELECT</span> <span class="lg-highlight-operator">*</span> <span class="lg-highlight-keyword">INTO</span> T2 <span class="lg-highlight-keyword">FROM</span> T1</td></tr></tbody></table></code></pre>
                </div>
               </div>
              </div>
             </li>
            </ul>
           </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">
             Corresponds to the SQL statement:
            </p>
            <ul class="leafygreen-ui-rioki0">
             <li class="leafygreen-ui-d7p7b6">
              <script class="structured_data" type="application/ld+json">
               {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"MERGE T2 AS TARGET\nUSING (SELECT * FROM T1) AS SOURCE\nON MATCH (T2.ID = SOURCE.ID)\nWHEN MATCHED THEN\n  UPDATE SET TARGET.FIELDX = SOURCE.FIELDY\nWHEN NOT MATCHED THEN\n  INSERT (FIELDX)\n  VALUES (SOURCE.FIELDY)","programmingLanguage":"SQL"}
              </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 sql leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">MERGE</span> T2 <span class="lg-highlight-keyword">AS</span> TARGET</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">USING</span> (<span class="lg-highlight-keyword">SELECT</span> <span class="lg-highlight-operator">*</span> <span class="lg-highlight-keyword">FROM</span> T1) <span class="lg-highlight-keyword">AS</span> SOURCE</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">ON</span> <span class="lg-highlight-keyword">MATCH</span> (T2.ID <span class="lg-highlight-operator">=</span> SOURCE.ID)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">WHEN</span> MATCHED <span class="lg-highlight-keyword">THEN</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <span class="lg-highlight-keyword">UPDATE</span> <span class="lg-highlight-keyword">SET</span> TARGET.FIELDX <span class="lg-highlight-operator">=</span> SOURCE.FIELDY</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">WHEN</span> <span class="lg-highlight-keyword">NOT</span> MATCHED <span class="lg-highlight-keyword">THEN</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <span class="lg-highlight-keyword">INSERT</span> (FIELDX)</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <span class="lg-highlight-keyword">VALUES</span> (SOURCE.FIELDY)</td></tr></tbody></table></code></pre>
                </div>
               </div>
              </div>
             </li>
             <li class="leafygreen-ui-d7p7b6">
              <p class="leafygreen-ui-1kp3ins">
               Create/Refresh Materialized Views
              </p>
             </li>
            </ul>
           </li>
          </ul>
         </div>
        </div>
       </td>
      </tr>
     </tbody>
    </table>
   </div>
  </section>
 </section>
 <section>
  <h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
   Behaviors
   <a class="headerlink leafygreen-ui-1kepx47" href="#behaviors" 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="behaviors">
    </div>
   </a>
  </h2>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    $out Read Operations Run on Secondary Replica Set Members
    <a class="headerlink leafygreen-ui-1kepx47" href="#-out-read-operations-run-on-secondary-replica-set-members" 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="-out-read-operations-run-on-secondary-replica-set-members">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    Starting in MongoDB 5.0,
    <code class="leafygreen-ui-1l06pbn">
     $out
    </code>
    can run on
replica set secondary nodes if all the nodes in
cluster have
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/setFeatureCompatibilityVersion/#std-label-view-fcv">
     featureCompatibilityVersion
    </a>
    set
to
    <code class="leafygreen-ui-1l06pbn">
     5.0
    </code>
    or higher and the
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/read-preference/">
     Read Preference
    </a>
    is set to
secondary.
   </p>
   <p class="leafygreen-ui-1kp3ins">
    Read operations of the
    <code class="leafygreen-ui-1l06pbn">
     $out
    </code>
    statement occur on the
secondary nodes, while the write operations occur only on the
primary nodes.
   </p>
   <p class="leafygreen-ui-1kp3ins">
    Not all driver versions support targeting of
    <code class="leafygreen-ui-1l06pbn">
     $out
    </code>
    operations to replica set secondary nodes. Check your
    <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/drivers/" target="_self">
     <span>
      driver
     </span>
    </a>
    documentation to see when your driver added
support for
    <code class="leafygreen-ui-1l06pbn">
     $out
    </code>
    running on a secondary.
   </p>
  </section>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    Create New Collection
    <a class="headerlink leafygreen-ui-1kepx47" href="#create-new-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="create-new-collection">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    The
    <code class="leafygreen-ui-1l06pbn">
     $out
    </code>
    operation creates a new collection if one does not
already exist.
   </p>
   <p class="leafygreen-ui-1kp3ins">
    The collection is not visible until the aggregation completes. If the
aggregation fails, MongoDB does not create the collection.
   </p>
   <span class="leafygreen-ui-1qnjd6d" id="std-label-replace-existing-collection">
   </span>
  </section>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    Replace Existing Collection
    <a class="headerlink leafygreen-ui-1kepx47" href="#replace-existing-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="replace-existing-collection">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    If the collection specified by the
    <code class="leafygreen-ui-1l06pbn">
     $out
    </code>
    operation already
exists, then upon completion of the aggregation, the
    <code class="leafygreen-ui-1l06pbn">
     $out
    </code>
    stage atomically replaces the existing collection with the new results
collection. Specifically, the
    <code class="leafygreen-ui-1l06pbn">
     $out
    </code>
    operation:
   </p>
   <ol class="leafygreen-ui-rioki0" type="1">
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      Creates a temp collection.
     </p>
    </li>
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      Copies the indexes from the existing collection to the temp collection.
     </p>
    </li>
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      Inserts the documents into the temp collection.
     </p>
    </li>
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      Calls the
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/renameCollection/#mongodb-dbcommand-dbcmd.renameCollection">
       <code class="leafygreen-ui-1l06pbn">
        renameCollection
       </code>
      </a>
      command with
      <code class="leafygreen-ui-1l06pbn">
       dropTarget: true
      </code>
      to rename the temp collection to the destination collection.
     </p>
    </li>
   </ol>
   <p class="leafygreen-ui-1kp3ins">
    If specified collection exists and the
    <code class="leafygreen-ui-1l06pbn">
     $out
    </code>
    operation specifies
    <code class="leafygreen-ui-1l06pbn">
     timeseries
    </code>
    options, then the following restrictions apply:
   </p>
   <ol class="leafygreen-ui-rioki0" type="1">
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      The existing collection must be a time series collection.
     </p>
    </li>
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      The existing collection must not be a view.
     </p>
    </li>
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      The
      <code class="leafygreen-ui-1l06pbn">
       timeseries
      </code>
      options included in the
      <code class="leafygreen-ui-1l06pbn">
       $out
      </code>
      stage must
exactly match those on the existing collection.
     </p>
    </li>
   </ol>
   <p class="leafygreen-ui-1kp3ins">
    The
    <code class="leafygreen-ui-1l06pbn">
     $out
    </code>
    operation does not change any indexes that existed on the
previous collection. If the aggregation fails, the
    <code class="leafygreen-ui-1l06pbn">
     $out
    </code>
    operation
makes no changes to the pre-existing collection.
   </p>
   <section>
    <h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
     Schema Validation Errors
     <a class="headerlink leafygreen-ui-1kepx47" href="#schema-validation-errors" 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="schema-validation-errors">
      </div>
     </a>
    </h4>
    <p class="leafygreen-ui-1kp3ins">
     If your
     <code class="leafygreen-ui-1l06pbn">
      coll
     </code>
     collection uses
     <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/schema-validation/#std-label-schema-validation-overview">
      schema validation
     </a>
     and has
     <code class="leafygreen-ui-1l06pbn">
      validationAction
     </code>
     set to
     <code class="leafygreen-ui-1l06pbn">
      error
     </code>
     , inserting an invalid document with
     <code class="leafygreen-ui-1l06pbn">
      $out
     </code>
     throws an
error. The
     <code class="leafygreen-ui-1l06pbn">
      $out
     </code>
     operation makes no changes to the pre-existing
collection and documents returned by the aggregation pipeline are not
added to the
     <code class="leafygreen-ui-1l06pbn">
      coll
     </code>
     collection.
    </p>
   </section>
  </section>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    Index Constraints
    <a class="headerlink leafygreen-ui-1kepx47" href="#index-constraints" 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="index-constraints">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    The pipeline will fail to complete if the documents produced by the
pipeline would violate any unique indexes, including the index on the
    <code class="leafygreen-ui-1l06pbn">
     _id
    </code>
    field of the original output collection.
   </p>
   <p class="leafygreen-ui-1kp3ins">
    If the
    <code class="leafygreen-ui-1l06pbn">
     $out
    </code>
    operation modifies a collection with an
    <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/atlas-search/" target="_self">
     <span>
      Atlas Search
     </span>
    </a>
    index, you must delete and
re-create the search index. Consider using
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/merge/#mongodb-pipeline-pipe.-merge">
     <code class="leafygreen-ui-1l06pbn">
      $merge
     </code>
    </a>
    instead.
   </p>
  </section>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    <code class="leafygreen-ui-1l06pbn">
     majority
    </code>
    Read Concern
    <a class="headerlink leafygreen-ui-1kepx47" href="#majority-read-concern" 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="majority-read-concern">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    You can specify
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/read-concern/#std-label-read-concern">
     read concern
    </a>
    level
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/read-concern-majority/#mongodb-readconcern-readconcern.-majority-">
     <code class="leafygreen-ui-1l06pbn">
      "majority"
     </code>
    </a>
    for an aggregation that includes an
    <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-out" target="_self">
     <span>
      <code class="leafygreen-ui-1l06pbn">
       $out
      </code>
     </span>
    </a>
    stage.
   </p>
  </section>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    Interaction with
    <code class="leafygreen-ui-1l06pbn">
     mongodump
    </code>
    <a class="headerlink leafygreen-ui-1kepx47" href="#interaction-with-mongodump" 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="interaction-with-mongodump">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    A
    <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/database-tools/mongodump/#mongodb-binary-bin.mongodump" target="_self">
     <span>
      <code class="leafygreen-ui-1l06pbn">
       mongodump
      </code>
     </span>
    </a>
    started with
    <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/database-tools/mongodump/#std-option-mongodump.--oplog" target="_self">
     <span>
      <code class="leafygreen-ui-1l06pbn">
       --oplog
      </code>
     </span>
    </a>
    fails if a client issues an aggregation pipeline
that includes
    <code class="leafygreen-ui-1l06pbn">
     $out
    </code>
    during the dump process. See
    <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/database-tools/mongodump/#std-option-mongodump.--oplog" target="_self">
     <span>
      <code class="leafygreen-ui-1l06pbn">
       mongodump --oplog
      </code>
     </span>
    </a>
    for more information.
   </p>
  </section>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    Restrictions
    <a class="headerlink leafygreen-ui-1kepx47" href="#restrictions" 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="restrictions">
     </div>
    </a>
   </h3>
   <div class="leafygreen-ui-skkc39" tabindex="0">
    <table class="leafygreen-ui-hr8lsv">
     <colgroup>
      <col style="width:30%"/>
      <col style="width:70%"/>
     </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>
          Restrictions
         </div>
        </div>
       </th>
       <th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
        <div class="leafygreen-ui-ui0te9">
         <div>
          Description
         </div>
        </div>
       </th>
      </tr>
     </thead>
     <tbody>
      <tr class="leafygreen-ui-15dvrnp">
       <td class="leafygreen-ui-1drjk7h">
        <div class="leafygreen-ui-1n4j8xf">
         <div>
          <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/transactions/#std-label-transactions">
           transactions
          </a>
         </div>
        </div>
       </td>
       <td class="leafygreen-ui-1drjk7h">
        <div class="leafygreen-ui-1n4j8xf">
         <div>
          An aggregation pipeline cannot use
          <code class="leafygreen-ui-1l06pbn">
           $out
          </code>
          inside
          <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/transactions/#std-label-transactions">
           transactions
           <!-- -->
           .
          </a>
         </div>
        </div>
       </td>
      </tr>
      <tr class="leafygreen-ui-15dvrnp">
       <td class="leafygreen-ui-1drjk7h">
        <div class="leafygreen-ui-1n4j8xf">
         <div>
          <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/views/#std-label-views-landing-page">
           view definition
          </a>
         </div>
        </div>
       </td>
       <td class="leafygreen-ui-1drjk7h">
        <div class="leafygreen-ui-1n4j8xf">
         <div>
          The
          <code class="leafygreen-ui-1l06pbn">
           $out
          </code>
          stage is not allowed as part of a
view definition. If the view definition includes nested pipeline
(e.g. the view definition includes
          <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/lookup/#mongodb-pipeline-pipe.-lookup">
           <code class="leafygreen-ui-1l06pbn">
            $lookup
           </code>
          </a>
          or
          <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/facet/#mongodb-pipeline-pipe.-facet">
           <code class="leafygreen-ui-1l06pbn">
            $facet
           </code>
          </a>
          stage), this
          <code class="leafygreen-ui-1l06pbn">
           $out
          </code>
          stage
restriction applies to the nested pipelines as well.
         </div>
        </div>
       </td>
      </tr>
      <tr class="leafygreen-ui-15dvrnp">
       <td class="leafygreen-ui-1drjk7h">
        <div class="leafygreen-ui-1n4j8xf">
         <div>
          <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/lookup/#mongodb-pipeline-pipe.-lookup">
           <code class="leafygreen-ui-1l06pbn">
            $lookup
           </code>
          </a>
          stage
         </div>
        </div>
       </td>
       <td class="leafygreen-ui-1drjk7h">
        <div class="leafygreen-ui-1n4j8xf">
         <div>
          You can't include the
          <code class="leafygreen-ui-1l06pbn">
           $out
          </code>
          stage in the
          <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/lookup/#mongodb-pipeline-pipe.-lookup">
           <code class="leafygreen-ui-1l06pbn">
            $lookup
           </code>
          </a>
          stage's
          <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/lookup/#std-label-lookup-syntax-let-pipeline">
           nested pipeline
           <!-- -->
           .
          </a>
         </div>
        </div>
       </td>
      </tr>
      <tr class="leafygreen-ui-15dvrnp">
       <td class="leafygreen-ui-1drjk7h">
        <div class="leafygreen-ui-1n4j8xf">
         <div>
          <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/facet/#mongodb-pipeline-pipe.-facet">
           <code class="leafygreen-ui-1l06pbn">
            $facet
           </code>
          </a>
          stage
         </div>
        </div>
       </td>
       <td class="leafygreen-ui-1drjk7h">
        <div class="leafygreen-ui-1n4j8xf">
         <div>
          <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/facet/#mongodb-pipeline-pipe.-facet">
           <code class="leafygreen-ui-1l06pbn">
            $facet
           </code>
          </a>
          stage's
          <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/lookup/#std-label-lookup-syntax-let-pipeline">
           nested pipeline
          </a>
          cannot include the
          <code class="leafygreen-ui-1l06pbn">
           $out
          </code>
          stage.
         </div>
        </div>
       </td>
      </tr>
      <tr class="leafygreen-ui-15dvrnp">
       <td class="leafygreen-ui-1drjk7h">
        <div class="leafygreen-ui-1n4j8xf">
         <div>
          <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/unionWith/#mongodb-pipeline-pipe.-unionWith">
           <code class="leafygreen-ui-1l06pbn">
            $unionWith
           </code>
          </a>
          stage
         </div>
        </div>
       </td>
       <td class="leafygreen-ui-1drjk7h">
        <div class="leafygreen-ui-1n4j8xf">
         <div>
          <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/unionWith/#mongodb-pipeline-pipe.-unionWith">
           <code class="leafygreen-ui-1l06pbn">
            $unionWith
           </code>
          </a>
          stage's
          <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/unionWith/#std-label-unionWith-pipeline">
           nested pipeline
          </a>
          cannot include the
          <code class="leafygreen-ui-1l06pbn">
           $out
          </code>
          stage.
         </div>
        </div>
       </td>
      </tr>
      <tr class="leafygreen-ui-15dvrnp">
       <td class="leafygreen-ui-1drjk7h">
        <div class="leafygreen-ui-1n4j8xf">
         <div>
          <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/read-concern-linearizable/#mongodb-readconcern-readconcern.-linearizable-">
           <code class="leafygreen-ui-1l06pbn">
            "linearizable"
           </code>
          </a>
          read concern
         </div>
        </div>
       </td>
       <td class="leafygreen-ui-1drjk7h">
        <div class="leafygreen-ui-1n4j8xf">
         <div>
          <p class="leafygreen-ui-1kp3ins">
           The
           <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-out" target="_self">
            <span>
             <code class="leafygreen-ui-1l06pbn">
              $out
             </code>
            </span>
           </a>
           stage cannot be used in conjunction with read concern
           <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/read-concern-linearizable/#mongodb-readconcern-readconcern.-linearizable-">
            <code class="leafygreen-ui-1l06pbn">
             "linearizable"
            </code>
           </a>
           . If you specify
           <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/read-concern-linearizable/#mongodb-readconcern-readconcern.-linearizable-">
            <code class="leafygreen-ui-1l06pbn">
             "linearizable"
            </code>
           </a>
           read concern for
           <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>
           , you cannot include the
           <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-out" target="_self">
            <span>
             <code class="leafygreen-ui-1l06pbn">
              $out
             </code>
            </span>
           </a>
           stage in the pipeline.
          </p>
         </div>
        </div>
       </td>
      </tr>
     </tbody>
    </table>
   </div>
  </section>
 </section>
 <section>
  <h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
   Examples
   <a class="headerlink leafygreen-ui-1kepx47" href="#examples" title="Permalink to this heading">
    <svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
     <path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
     </path>
     <path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
     </path>
    </svg>
    <div class="css-1btu0w3 evvkmar0" id="examples">
    </div>
   </a>
  </h2>
  <p class="leafygreen-ui-1kp3ins">
   In the
   <code class="leafygreen-ui-1l06pbn">
    test
   </code>
   database, create a collection
   <code class="leafygreen-ui-1l06pbn">
    books
   </code>
   with the
following documents:
  </p>
  <script class="structured_data" type="application/ld+json">
   {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.getSiblingDB(\"test\").books.insertMany([\n   { \"_id\" : 8751, \"title\" : \"The Banquet\", \"author\" : \"Dante\", \"copies\" : 2 },\n   { \"_id\" : 8752, \"title\" : \"Divine Comedy\", \"author\" : \"Dante\", \"copies\" : 1 },\n   { \"_id\" : 8645, \"title\" : \"Eclogues\", \"author\" : \"Dante\", \"copies\" : 2 },\n   { \"_id\" : 7000, \"title\" : \"The Odyssey\", \"author\" : \"Homer\", \"copies\" : 10 },\n   { \"_id\" : 7020, \"title\" : \"Iliad\", \"author\" : \"Homer\", \"copies\" : 10 }\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-title lg-highlight-function">getSiblingDB</span>(<span class="lg-highlight-string">"test"</span>).<span class="lg-highlight-property">books</span>.<span class="lg-highlight-title lg-highlight-function">insertMany</span>(<!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">8751</span>, <span class="lg-highlight-string">"title"</span> <!-- -->: <span class="lg-highlight-string">"The Banquet"</span>, <span class="lg-highlight-string">"author"</span> <!-- -->: <span class="lg-highlight-string">"Dante"</span>, <span class="lg-highlight-string">"copies"</span> <!-- -->: <span class="lg-highlight-number">2</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">8752</span>, <span class="lg-highlight-string">"title"</span> <!-- -->: <span class="lg-highlight-string">"Divine Comedy"</span>, <span class="lg-highlight-string">"author"</span> <!-- -->: <span class="lg-highlight-string">"Dante"</span>, <span class="lg-highlight-string">"copies"</span> <!-- -->: <span class="lg-highlight-number">1</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">8645</span>, <span class="lg-highlight-string">"title"</span> <!-- -->: <span class="lg-highlight-string">"Eclogues"</span>, <span class="lg-highlight-string">"author"</span> <!-- -->: <span class="lg-highlight-string">"Dante"</span>, <span class="lg-highlight-string">"copies"</span> <!-- -->: <span class="lg-highlight-number">2</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">7000</span>, <span class="lg-highlight-string">"title"</span> <!-- -->: <span class="lg-highlight-string">"The Odyssey"</span>, <span class="lg-highlight-string">"author"</span> <!-- -->: <span class="lg-highlight-string">"Homer"</span>, <span class="lg-highlight-string">"copies"</span> <!-- -->: <span class="lg-highlight-number">10</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">7020</span>, <span class="lg-highlight-string">"title"</span> <!-- -->: <span class="lg-highlight-string">"Iliad"</span>, <span class="lg-highlight-string">"author"</span> <!-- -->: <span class="lg-highlight-string">"Homer"</span>, <span class="lg-highlight-string">"copies"</span> <!-- -->: <span class="lg-highlight-number">10</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">
   If the
   <code class="leafygreen-ui-1l06pbn">
    test
   </code>
   database does not already exist, the insert operation
creates the database as well as the
   <code class="leafygreen-ui-1l06pbn">
    books
   </code>
   collection.
  </p>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    Output to Same Database
    <a class="headerlink leafygreen-ui-1kepx47" href="#output-to-same-database" 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="output-to-same-database">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    The following aggregation operation pivots the data in the
    <code class="leafygreen-ui-1l06pbn">
     books
    </code>
    collection in the
    <code class="leafygreen-ui-1l06pbn">
     test
    </code>
    database to have titles grouped by authors and then writes
the results to the
    <code class="leafygreen-ui-1l06pbn">
     authors
    </code>
    collection, also in the
    <code class="leafygreen-ui-1l06pbn">
     test
    </code>
    database.
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.getSiblingDB(\"test\").books.aggregate( [\n    { $group : { _id : \"$author\", books: { $push: \"$title\" } } },\n    { $out : \"authors\" }\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-title lg-highlight-function">getSiblingDB</span>(<span class="lg-highlight-string">"test"</span>).<span class="lg-highlight-property">books</span>.<span class="lg-highlight-title lg-highlight-function">aggregate</span>( <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx">    <!-- -->{ <!-- -->$group<!-- --> <!-- -->: <!-- -->{ <!-- -->_id <!-- -->: <span class="lg-highlight-string">"$author"</span>, <span class="lg-highlight-attr">books</span>: <!-- -->{ <span class="lg-highlight-attr">$push</span>: <span class="lg-highlight-string">"$title"</span> } } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">    <!-- -->{ <!-- -->$out<!-- --> <!-- -->: <span class="lg-highlight-string">"authors"</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>
   <dl>
    <dt>
     First Stage (
     <code class="leafygreen-ui-1l06pbn">
      $group
     </code>
     ):
    </dt>
    <dd class="css-1ufxuu7">
     <p class="leafygreen-ui-1kp3ins">
      The
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/group/#mongodb-pipeline-pipe.-group">
       <code class="leafygreen-ui-1l06pbn">
        $group
       </code>
      </a>
      stage groups by the
      <code class="leafygreen-ui-1l06pbn">
       authors
      </code>
      and uses
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/push/#mongodb-group-grp.-push">
       <code class="leafygreen-ui-1l06pbn">
        $push
       </code>
      </a>
      to add the titles to a
      <code class="leafygreen-ui-1l06pbn">
       books
      </code>
      array field:
     </p>
     <script class="structured_data" type="application/ld+json">
      {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : \"Dante\", \"books\" : [ \"The Banquet\", \"Divine Comedy\", \"Eclogues\" ] }\n{ \"_id\" : \"Homer\", \"books\" : [ \"The Odyssey\", \"Iliad\" ] }","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">"Dante"</span>, <span class="lg-highlight-string">"books"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"The Banquet"</span>, <span class="lg-highlight-string">"Divine Comedy"</span>, <span class="lg-highlight-string">"Eclogues"</span> ] }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"Homer"</span>, <span class="lg-highlight-string">"books"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"The Odyssey"</span>, <span class="lg-highlight-string">"Iliad"</span> ] }</td></tr></tbody></table></code></pre>
       </div>
      </div>
     </div>
    </dd>
    <dt>
     Second Stage (
     <code class="leafygreen-ui-1l06pbn">
      $out
     </code>
     ):
    </dt>
    <dd class="css-1ufxuu7">
     The
     <code class="leafygreen-ui-1l06pbn">
      $out
     </code>
     stage outputs the documents to the
     <code class="leafygreen-ui-1l06pbn">
      authors
     </code>
     collection in the
     <code class="leafygreen-ui-1l06pbn">
      test
     </code>
     database.
    </dd>
   </dl>
   <p class="leafygreen-ui-1kp3ins">
    To view the documents in the output collection, run the following
operation:
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.getSiblingDB(\"test\").authors.find()","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-title lg-highlight-function">getSiblingDB</span>(<span class="lg-highlight-string">"test"</span>).<span class="lg-highlight-property">authors</span>.<span class="lg-highlight-title lg-highlight-function">find</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 collection contains the following documents:
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : \"Homer\", \"books\" : [ \"The Odyssey\", \"Iliad\" ] }\n{ \"_id\" : \"Dante\", \"books\" : [ \"The Banquet\", \"Divine Comedy\", \"Eclogues\" ] }","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">"Homer"</span>, <span class="lg-highlight-string">"books"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"The Odyssey"</span>, <span class="lg-highlight-string">"Iliad"</span> ] }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"Dante"</span>, <span class="lg-highlight-string">"books"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"The Banquet"</span>, <span class="lg-highlight-string">"Divine Comedy"</span>, <span class="lg-highlight-string">"Eclogues"</span> ] }</td></tr></tbody></table></code></pre>
     </div>
    </div>
   </div>
  </section>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    Output to a Different Database
    <a class="headerlink leafygreen-ui-1kepx47" href="#output-to-a-different-database" 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="output-to-a-different-database">
     </div>
    </a>
   </h3>
   <div class="leafygreen-ui-15mdk4" role="note">
    <h2 class="leafygreen-ui-6n7b7d">
     Note
    </h2>
    <div class="leafygreen-ui-9mycqc">
     <p class="leafygreen-ui-1kp3ins">
      For a
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/replication/#std-label-replica-set">
       replica set
      </a>
      or a standalone, if the
output database does not exist,
      <code class="leafygreen-ui-1l06pbn">
       $out
      </code>
      also creates
the database.
     </p>
    </div>
   </div>
   <p class="leafygreen-ui-1kp3ins">
    <code class="leafygreen-ui-1l06pbn">
     $out
    </code>
    can output to a collection in a database different from where the
aggregation is run.
   </p>
   <p class="leafygreen-ui-1kp3ins">
    The following aggregation operation pivots the data in the
    <code class="leafygreen-ui-1l06pbn">
     books
    </code>
    collection to have titles grouped by authors and then writes the
results to the
    <code class="leafygreen-ui-1l06pbn">
     authors
    </code>
    collection in the
    <code class="leafygreen-ui-1l06pbn">
     reporting
    </code>
    database:
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.getSiblingDB(\"test\").books.aggregate( [\n    { $group : { _id : \"$author\", books: { $push: \"$title\" } } },\n    { $out : { db: \"reporting\", coll: \"authors\" } }\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-title lg-highlight-function">getSiblingDB</span>(<span class="lg-highlight-string">"test"</span>).<span class="lg-highlight-property">books</span>.<span class="lg-highlight-title lg-highlight-function">aggregate</span>( <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx">    <!-- -->{ <!-- -->$group<!-- --> <!-- -->: <!-- -->{ <!-- -->_id <!-- -->: <span class="lg-highlight-string">"$author"</span>, <span class="lg-highlight-attr">books</span>: <!-- -->{ <span class="lg-highlight-attr">$push</span>: <span class="lg-highlight-string">"$title"</span> } } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">    <!-- -->{ <!-- -->$out<!-- --> <!-- -->: <!-- -->{ <span class="lg-highlight-attr">db</span>: <span class="lg-highlight-string">"reporting"</span>, <span class="lg-highlight-attr">coll</span>: <span class="lg-highlight-string">"authors"</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>
   <dl>
    <dt>
     First Stage (
     <code class="leafygreen-ui-1l06pbn">
      $group
     </code>
     ):
    </dt>
    <dd class="css-1ufxuu7">
     <p class="leafygreen-ui-1kp3ins">
      The
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/group/#mongodb-pipeline-pipe.-group">
       <code class="leafygreen-ui-1l06pbn">
        $group
       </code>
      </a>
      stage groups by the
      <code class="leafygreen-ui-1l06pbn">
       authors
      </code>
      and uses
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/push/#mongodb-group-grp.-push">
       <code class="leafygreen-ui-1l06pbn">
        $push
       </code>
      </a>
      to add the titles to a
      <code class="leafygreen-ui-1l06pbn">
       books
      </code>
      array field:
     </p>
     <script class="structured_data" type="application/ld+json">
      {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : \"Dante\", \"books\" : [ \"The Banquet\", \"Divine Comedy\", \"Eclogues\" ] }\n{ \"_id\" : \"Homer\", \"books\" : [ \"The Odyssey\", \"Iliad\" ] }","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">"Dante"</span>, <span class="lg-highlight-string">"books"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"The Banquet"</span>, <span class="lg-highlight-string">"Divine Comedy"</span>, <span class="lg-highlight-string">"Eclogues"</span> ] }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"Homer"</span>, <span class="lg-highlight-string">"books"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"The Odyssey"</span>, <span class="lg-highlight-string">"Iliad"</span> ] }</td></tr></tbody></table></code></pre>
       </div>
      </div>
     </div>
    </dd>
    <dt>
     Second Stage (
     <code class="leafygreen-ui-1l06pbn">
      $out
     </code>
     ):
    </dt>
    <dd class="css-1ufxuu7">
     The
     <code class="leafygreen-ui-1l06pbn">
      $out
     </code>
     stage outputs the documents to the
     <code class="leafygreen-ui-1l06pbn">
      authors
     </code>
     collection in the
     <code class="leafygreen-ui-1l06pbn">
      reporting
     </code>
     database.
    </dd>
   </dl>
   <p class="leafygreen-ui-1kp3ins">
    To view the documents in the output collection, run the following
operation:
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.getSiblingDB(\"reporting\").authors.find()","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-title lg-highlight-function">getSiblingDB</span>(<span class="lg-highlight-string">"reporting"</span>).<span class="lg-highlight-property">authors</span>.<span class="lg-highlight-title lg-highlight-function">find</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 collection contains the following documents:
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : \"Homer\", \"books\" : [ \"The Odyssey\", \"Iliad\" ] }\n{ \"_id\" : \"Dante\", \"books\" : [ \"The Banquet\", \"Divine Comedy\", \"Eclogues\" ] }","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">"Homer"</span>, <span class="lg-highlight-string">"books"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"The Odyssey"</span>, <span class="lg-highlight-string">"Iliad"</span> ] }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"Dante"</span>, <span class="lg-highlight-string">"books"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"The Banquet"</span>, <span class="lg-highlight-string">"Divine Comedy"</span>, <span class="lg-highlight-string">"Eclogues"</span> ] }</td></tr></tbody></table></code></pre>
     </div>
    </div>
   </div>
  </section>
 </section>
</section>
