<section>
 <h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
  $unionWith (aggregation)
  <a class="headerlink leafygreen-ui-1kepx47" href="#-unionwith--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="-unionwith--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="#considerations" target="_self">
     <span>
      Considerations
     </span>
    </a>
   </li>
   <li class="leafygreen-ui-1jqtqlb">
    <a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#duplicate-results" target="_self">
     <span>
      Duplicate Results
     </span>
    </a>
   </li>
   <li class="leafygreen-ui-1jqtqlb">
    <a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#-unionwith-a-sharded-collection" target="_self">
     <span>
      <code class="css-1kekw28 e1wawog0">
       $unionWith
      </code>
      a Sharded Collection
     </span>
    </a>
   </li>
   <li class="leafygreen-ui-1jqtqlb">
    <a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#collation" target="_self">
     <span>
      Collation
     </span>
    </a>
   </li>
   <li class="leafygreen-ui-1jqtqlb">
    <a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#atlas-search-support" target="_self">
     <span>
      Atlas Search Support
     </span>
    </a>
   </li>
   <li class="leafygreen-ui-1jqtqlb">
    <a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#restrictions" target="_self">
     <span>
      Restrictions
     </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>
   <li class="leafygreen-ui-1jqtqlb">
    <a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#create-sales-reports-from-the-union-of-yearly-data-collections" target="_self">
     <span>
      Create Sales Reports from the Union of Yearly Data Collections
     </span>
    </a>
   </li>
   <li class="leafygreen-ui-1jqtqlb">
    <a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#report-1--all-sales-by-year-and-stores-and-items" target="_self">
     <span>
      Report 1: All Sales by Year and Stores and Items
     </span>
    </a>
   </li>
   <li class="leafygreen-ui-1jqtqlb">
    <a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#report-2--aggregated-sales-by-items" target="_self">
     <span>
      Report 2: Aggregated Sales by Items
     </span>
    </a>
   </li>
   <li class="leafygreen-ui-1jqtqlb">
    <a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#create-a-union-with-specified-documents" target="_self">
     <span>
      Create a Union with Specified Documents
     </span>
    </a>
   </li>
   <li class="leafygreen-ui-1jqtqlb">
    <a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#namespaces-in-subpipelines" target="_self">
     <span>
      Namespaces in Subpipelines
     </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">
     $unionWith
    </code>
    <a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-pipeline-pipe.-unionWith" 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.-unionWith">
     </div>
    </a>
   </dt>
   <dd>
    <div>
     <p>
      <em>
       Changed
       <!-- -->
       in version
       <!-- -->
       8.0
      </em>
      .
     </p>
    </div>
    <p class="leafygreen-ui-1kp3ins">
     Combines two aggregations into a single result set.
     <code class="leafygreen-ui-1l06pbn">
      $unionWith
     </code>
     outputs the combined result set (including duplicates) to the next
stage.
    </p>
    <p class="leafygreen-ui-1kp3ins">
     The order in which the combined result set documents are output is
unspecified.
    </p>
   </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">
    $unionWith
   </code>
   stage has the following syntax:
  </p>
  <script class="structured_data" type="application/ld+json">
   {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ $unionWith: { coll: \"&lt;collection&gt;\", pipeline: [ &lt;stage1&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">$unionWith</span>: <!-- -->{ <span class="lg-highlight-attr">coll</span>: <span class="lg-highlight-string">"&lt;collection&gt;"</span>, <span class="lg-highlight-attr">pipeline</span>: <!-- -->[ &lt;<!-- -->stage1<!-- -->&gt;<!-- -->,<!-- --> <!-- -->...<!-- --> ] } }</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">
   To include all documents from the specified collection without any
processing, you can use the simplified form:
  </p>
  <script class="structured_data" type="application/ld+json">
   {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ $unionWith: \"&lt;collection&gt;\" }  // Include all documents from the specified collection","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">$unionWith</span>: <span class="lg-highlight-string">"&lt;collection&gt;"</span> }  <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> Include all documents </span><span class="lg-highlight-comment">from the specified collection</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
   <code class="leafygreen-ui-1l06pbn">
    $unionWith
   </code>
   stage takes a document with the following fields:
  </p>
  <div class="leafygreen-ui-skkc39" tabindex="0">
   <table class="leafygreen-ui-hr8lsv">
    <colgroup>
     <col style="width:10%"/>
     <col style="width:10%"/>
     <col style="width:20%"/>
    </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>
         Necessity
        </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="">
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-unionWith-coll" target="_self">
          <span>
           coll
          </span>
         </a>
        </div>
       </div>
      </td>
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         Required if
         <code class="leafygreen-ui-1l06pbn">
          pipeline
         </code>
         is omitted. Otherwise optional.
        </div>
       </div>
      </td>
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         <span class="leafygreen-ui-1qnjd6d" id="std-label-unionWith-coll">
         </span>
         <p class="leafygreen-ui-1kp3ins">
          The collection or view whose
          <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-unionWith-pipeline" target="_self">
           <span>
            pipeline
           </span>
          </a>
          results you wish to include in the result set.
         </p>
         <p class="leafygreen-ui-1kp3ins">
          If you omit the
          <code class="leafygreen-ui-1l06pbn">
           coll
          </code>
          field, you must specify a
          <code class="leafygreen-ui-1l06pbn">
           pipeline
          </code>
          field with a first stage of
          <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/documents/#mongodb-pipeline-pipe.-documents">
           <code class="leafygreen-ui-1l06pbn">
            $documents
           </code>
           .
          </a>
         </p>
        </div>
       </div>
      </td>
     </tr>
     <tr class="">
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-unionWith-pipeline" target="_self">
          <span>
           pipeline
          </span>
         </a>
        </div>
       </div>
      </td>
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         Required if
         <code class="leafygreen-ui-1l06pbn">
          coll
         </code>
         is omitted. Otherwise optional.
        </div>
       </div>
      </td>
      <td class="leafygreen-ui-1drjk7h">
       <div class="leafygreen-ui-1n4j8xf">
        <div>
         <span class="leafygreen-ui-1qnjd6d" id="std-label-unionWith-pipeline">
         </span>
         <p class="leafygreen-ui-1kp3ins">
          An aggregation pipeline to apply to the input documents.
         </p>
         <ul class="leafygreen-ui-rioki0">
          <li class="leafygreen-ui-d7p7b6">
           <p class="leafygreen-ui-1kp3ins">
            If you specify
            <code class="leafygreen-ui-1l06pbn">
             coll
            </code>
            , the pipeline applies to the documents
in
            <code class="leafygreen-ui-1l06pbn">
             coll
            </code>
            .
           </p>
          </li>
          <li class="leafygreen-ui-d7p7b6">
           <p class="leafygreen-ui-1kp3ins">
            If you omit
            <code class="leafygreen-ui-1l06pbn">
             coll
            </code>
            , the pipeline applies to the documents in
the pipeline's
            <code class="leafygreen-ui-1l06pbn">
             $documents
            </code>
            stage. For an example, see
            <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-unionWith-example-specified-docs" target="_self">
             <span>
              Create a Union with Specified Documents
              <!-- -->
              .
             </span>
            </a>
           </p>
          </li>
         </ul>
         <p class="leafygreen-ui-1kp3ins">
          The pipeline cannot include the
          <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/out/#mongodb-pipeline-pipe.-out">
           <code class="leafygreen-ui-1l06pbn">
            $out
           </code>
          </a>
          and
          <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/merge/#mongodb-pipeline-pipe.-merge">
           <code class="leafygreen-ui-1l06pbn">
            $merge
           </code>
          </a>
          stages. Starting in v6.0, the
          <code class="leafygreen-ui-1l06pbn">
           pipeline
          </code>
          can contain the
          <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/atlas-search/" target="_self">
           <span>
            Atlas Search
           </span>
          </a>
          <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/atlas-search/aggregation-stages/search/#mongodb-pipeline-pipe.-search" target="_self">
           <span>
            <code class="leafygreen-ui-1l06pbn">
             $search
            </code>
           </span>
          </a>
          stage as the first stage inside the pipeline.
To learn more, see
          <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-fts-unionwith-behavior" target="_self">
           <span>
            Atlas Search Support
            <!-- -->
            .
           </span>
          </a>
         </p>
        </div>
       </div>
      </td>
     </tr>
    </tbody>
   </table>
  </div>
  <p class="leafygreen-ui-1kp3ins">
   The
   <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-unionWith" target="_self">
    <span>
     <code class="leafygreen-ui-1l06pbn">
      $unionWith
     </code>
    </span>
   </a>
   operation would correspond to the following
SQL statement:
  </p>
  <script class="structured_data" type="application/ld+json">
   {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"SELECT *\nFROM Collection1\nWHERE ...\nUNION ALL\nSELECT *\nFROM Collection2\nWHERE ...","programmingLanguage":"SQL"}
  </script>
  <div class="css-16lmvu9">
   <div class="leafygreen-ui-196mwvg">
    <div class="leafygreen-ui-yzcs8i">
     <pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light 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></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">FROM</span> Collection1</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">WHERE</span> ...</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">UNION</span> <span class="lg-highlight-keyword">ALL</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">SELECT</span> <span class="lg-highlight-operator">*</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">FROM</span> Collection2</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">WHERE</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>
 </section>
 <section>
  <h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
   Considerations
   <a class="headerlink leafygreen-ui-1kepx47" href="#considerations" 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="considerations">
    </div>
   </a>
  </h2>
  <span class="leafygreen-ui-1qnjd6d" id="std-label-unionWith-duplicate-results">
  </span>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    Duplicate Results
    <a class="headerlink leafygreen-ui-1kepx47" href="#duplicate-results" title="Permalink to this heading">
     <svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
      <path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
      </path>
      <path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
      </path>
     </svg>
     <div class="css-1btu0w3 evvkmar0" id="duplicate-results">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    The combined results from the previous stage and the
    <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-unionWith" target="_self">
     <span>
      <code class="leafygreen-ui-1l06pbn">
       $unionWith
      </code>
     </span>
    </a>
    stage can include duplicates.
   </p>
   <p class="leafygreen-ui-1kp3ins">
    For example, create a
    <code class="leafygreen-ui-1l06pbn">
     suppliers
    </code>
    collection and a
    <code class="leafygreen-ui-1l06pbn">
     warehouses
    </code>
    collection:
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.suppliers.insertMany([\n  { _id: 1, supplier: \"Aardvark and Sons\", state: \"Texas\" },\n  { _id: 2, supplier: \"Bears Run Amok.\", state: \"Colorado\"},\n  { _id: 3, supplier: \"Squid Mark Inc. \", state: \"Rhode Island\" },\n])","programmingLanguage":"JavaScript"}
   </script>
   <div class="css-16lmvu9">
    <div class="leafygreen-ui-196mwvg">
     <div class="leafygreen-ui-1b1bvay">
      <pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">suppliers</span>.<span class="lg-highlight-title lg-highlight-function">insertMany</span>(<!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">supplier</span>: <span class="lg-highlight-string">"Aardvark and Sons"</span>, <span class="lg-highlight-attr">state</span>: <span class="lg-highlight-string">"Texas"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-attr">supplier</span>: <span class="lg-highlight-string">"Bears Run Amok."</span>, <span class="lg-highlight-attr">state</span>: <span class="lg-highlight-string">"Colorado"</span>}<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-attr">supplier</span>: <span class="lg-highlight-string">"Squid Mark Inc. "</span>, <span class="lg-highlight-attr">state</span>: <span class="lg-highlight-string">"Rhode Island"</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>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.warehouses.insertMany([\n  { _id: 1, warehouse: \"A\", region: \"West\", state: \"California\" },\n  { _id: 2, warehouse: \"B\", region: \"Central\", state: \"Colorado\"},\n  { _id: 3, warehouse: \"C\", region: \"East\", state: \"Florida\" },\n])","programmingLanguage":"JavaScript"}
   </script>
   <div class="css-16lmvu9">
    <div class="leafygreen-ui-196mwvg">
     <div class="leafygreen-ui-1b1bvay">
      <pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">warehouses</span>.<span class="lg-highlight-title lg-highlight-function">insertMany</span>(<!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">warehouse</span>: <span class="lg-highlight-string">"A"</span>, <span class="lg-highlight-attr">region</span>: <span class="lg-highlight-string">"West"</span>, <span class="lg-highlight-attr">state</span>: <span class="lg-highlight-string">"California"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-attr">warehouse</span>: <span class="lg-highlight-string">"B"</span>, <span class="lg-highlight-attr">region</span>: <span class="lg-highlight-string">"Central"</span>, <span class="lg-highlight-attr">state</span>: <span class="lg-highlight-string">"Colorado"</span>}<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-attr">warehouse</span>: <span class="lg-highlight-string">"C"</span>, <span class="lg-highlight-attr">region</span>: <span class="lg-highlight-string">"East"</span>, <span class="lg-highlight-attr">state</span>: <span class="lg-highlight-string">"Florida"</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">
    The following aggregation combines the
    <code class="leafygreen-ui-1l06pbn">
     state
    </code>
    field projection results from
the
    <code class="leafygreen-ui-1l06pbn">
     suppliers
    </code>
    and
    <code class="leafygreen-ui-1l06pbn">
     warehouse
    </code>
    collections.
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.suppliers.aggregate([\n   { $project: { state: 1, _id: 0 } },\n   { $unionWith: { coll: \"warehouses\", pipeline: [ { $project: { state: 1, _id: 0 } } ]} }\n])","programmingLanguage":"JavaScript"}
   </script>
   <div class="css-16lmvu9">
    <div class="leafygreen-ui-196mwvg">
     <div class="leafygreen-ui-1b1bvay">
      <pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">suppliers</span>.<span class="lg-highlight-title lg-highlight-function">aggregate</span>(<!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">$project</span>: <!-- -->{ <span class="lg-highlight-attr">state</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">0</span> } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">$unionWith</span>: <!-- -->{ <span class="lg-highlight-attr">coll</span>: <span class="lg-highlight-string">"warehouses"</span>, <span class="lg-highlight-attr">pipeline</span>: <!-- -->[ <!-- -->{ <span class="lg-highlight-attr">$project</span>: <!-- -->{ <span class="lg-highlight-attr">state</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">0</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">
    The result set contains duplicates:
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"state\" : \"Texas\" }\n{ \"state\" : \"Colorado\" }\n{ \"state\" : \"Rhode Island\" }\n{ \"state\" : \"California\" }\n{ \"state\" : \"Colorado\" }\n{ \"state\" : \"Florida\" }","programmingLanguage":"JavaScript"}
   </script>
   <div class="css-1lju8ps">
    <div class="leafygreen-ui-196mwvg">
     <div class="leafygreen-ui-1jheeh3">
      <pre class="leafygreen-ui-1453az5" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"state"</span> <!-- -->: <span class="lg-highlight-string">"Texas"</span> }</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"state"</span> <!-- -->: <span class="lg-highlight-string">"Colorado"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"state"</span> <!-- -->: <span class="lg-highlight-string">"Rhode Island"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"state"</span> <!-- -->: <span class="lg-highlight-string">"California"</span> }</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"state"</span> <!-- -->: <span class="lg-highlight-string">"Colorado"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"state"</span> <!-- -->: <span class="lg-highlight-string">"Florida"</span> }</td></tr></tbody></table></code></pre>
     </div>
    </div>
   </div>
   <p class="leafygreen-ui-1kp3ins">
    To remove the duplicates, you can include a
    <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 to
group by the
    <code class="leafygreen-ui-1l06pbn">
     state
    </code>
    field:
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.suppliers.aggregate([\n   { $project: { state: 1, _id: 0 } },\n   { $unionWith: { coll: \"warehouses\", pipeline: [ { $project: { state: 1, _id: 0 } } ]} },\n   { $group: { _id: \"$state\" } }\n])","programmingLanguage":"JavaScript"}
   </script>
   <div class="css-16lmvu9">
    <div class="leafygreen-ui-196mwvg">
     <div class="leafygreen-ui-1b1bvay">
      <pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">suppliers</span>.<span class="lg-highlight-title lg-highlight-function">aggregate</span>(<!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">$project</span>: <!-- -->{ <span class="lg-highlight-attr">state</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">0</span> } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">$unionWith</span>: <!-- -->{ <span class="lg-highlight-attr">coll</span>: <span class="lg-highlight-string">"warehouses"</span>, <span class="lg-highlight-attr">pipeline</span>: <!-- -->[ <!-- -->{ <span class="lg-highlight-attr">$project</span>: <!-- -->{ <span class="lg-highlight-attr">state</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">0</span> } } ]} }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">$group</span>: <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-string">"$state"</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">
    The result set no longer contains duplicates:
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":" { \"_id\" : \"California\" }\n { \"_id\" : \"Texas\" }\n { \"_id\" : \"Florida\" }\n { \"_id\" : \"Colorado\" }\n { \"_id\" : \"Rhode Island\" }","programmingLanguage":"JavaScript"}
   </script>
   <div class="css-1lju8ps">
    <div class="leafygreen-ui-196mwvg">
     <div class="leafygreen-ui-1jheeh3">
      <pre class="leafygreen-ui-1453az5" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"California"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"Texas"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"Florida"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"Colorado"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"Rhode Island"</span> }</td></tr></tbody></table></code></pre>
     </div>
    </div>
   </div>
  </section>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    <code class="leafygreen-ui-1l06pbn">
     $unionWith
    </code>
    a Sharded Collection
    <a class="headerlink leafygreen-ui-1kepx47" href="#-unionwith-a-sharded-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="-unionwith-a-sharded-collection">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    If the
    <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-unionWith" target="_self">
     <span>
      <code class="leafygreen-ui-1l06pbn">
       $unionWith
      </code>
     </span>
    </a>
    stage is part of the
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/lookup/#std-label-lookup-join-pipeline">
     $lookup
pipeline
    </a>
    , the
    <code class="leafygreen-ui-1l06pbn">
     $unionWith
    </code>
    <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-unionWith-coll" target="_self">
     <span>
      coll
     </span>
    </a>
    cannot be sharded. For example, in the following
aggregation operation, the
    <code class="leafygreen-ui-1l06pbn">
     inventory_q1
    </code>
    collection cannot be
sharded:
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.suppliers.aggregate([\n   {\n      $lookup: {\n         from: \"warehouses\",\n         let: { order_item: \"$item\", order_qty: \"$ordered\" },\n         pipeline: [\n            ...\n            { $unionWith: { coll: \"inventory_q1\", pipeline: [ ... ] } },\n            ...\n         ],\n         as: \"stockdata\"\n      }\n   }\n])","programmingLanguage":"JavaScript"}
   </script>
   <div class="css-1lju8ps">
    <div class="leafygreen-ui-196mwvg">
     <div class="leafygreen-ui-tyg50d">
      <pre class="leafygreen-ui-1453az5" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">suppliers</span>.<span class="lg-highlight-title lg-highlight-function">aggregate</span>(<!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <span class="lg-highlight-attr">$lookup</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">         <span class="lg-highlight-attr">from</span>: <span class="lg-highlight-string">"warehouses"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">         <span class="lg-highlight-attr">let</span>: <!-- -->{ <span class="lg-highlight-attr">order_item</span>: <span class="lg-highlight-string">"$item"</span>, <span class="lg-highlight-attr">order_qty</span>: <span class="lg-highlight-string">"$ordered"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">         <span class="lg-highlight-attr">pipeline</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx">            <!-- -->...</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx">            <!-- -->{ <span class="lg-highlight-attr">$unionWith</span>: <!-- -->{ <span class="lg-highlight-attr">coll</span>: <span class="lg-highlight-string">"inventory_q1"</span>, <span class="lg-highlight-attr">pipeline</span>: <!-- -->[ <!-- -->...<!-- --> ] } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">            <!-- -->...</td></tr><tr class=""><td class="leafygreen-ui-7razhx">         ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">         <span class="lg-highlight-attr">as</span>: <span class="lg-highlight-string">"stockdata"</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>
    </div>
   </div>
  </section>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    Collation
    <a class="headerlink leafygreen-ui-1kepx47" href="#collation" 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="collation">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    If the
    <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>
    includes a
    <code class="leafygreen-ui-1l06pbn">
     collation
    </code>
    document, that collation is used for the operation, ignoring any other
collations.
   </p>
   <p class="leafygreen-ui-1kp3ins">
    If the
    <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>
    does not include a
    <code class="leafygreen-ui-1l06pbn">
     collation
    </code>
    document, the
    <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>
    method
uses the collation for the top-level collection/view on which the
    <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>
    is run:
   </p>
   <ul class="leafygreen-ui-rioki0">
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      If the
      <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-unionWith-coll" target="_self">
       <span>
        $unionWith coll
       </span>
      </a>
      is a collection, its
collation is ignored.
     </p>
    </li>
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      If the
      <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-unionWith-coll" target="_self">
       <span>
        $unionWith coll
       </span>
      </a>
      is a
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/views/">
       view
      </a>
      , then its collation must match that of the top-level
collection/view.  Otherwise, the operation errors.
     </p>
    </li>
   </ul>
   <span class="leafygreen-ui-1qnjd6d" id="std-label-fts-unionwith-behavior">
   </span>
  </section>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    Atlas Search Support
    <a class="headerlink leafygreen-ui-1kepx47" href="#atlas-search-support" 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="atlas-search-support">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    Starting in MongoDB 6.0, you can specify the
    <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/atlas-search/" target="_self">
     <span>
      Atlas Search
     </span>
    </a>
    <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/atlas-search/aggregation-stages/search/#mongodb-pipeline-pipe.-search" target="_self">
     <span>
      <code class="leafygreen-ui-1l06pbn">
       $search
      </code>
     </span>
    </a>
    or
    <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/atlas-search/aggregation-stages/searchMeta/#mongodb-pipeline-pipe.-searchMeta" target="_self">
     <span>
      <code class="leafygreen-ui-1l06pbn">
       $searchMeta
      </code>
     </span>
    </a>
    stage
in the
    <code class="leafygreen-ui-1l06pbn">
     $unionWith
    </code>
    pipeline to search collections on the Atlas
cluster. The
    <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/atlas-search/aggregation-stages/search/#mongodb-pipeline-pipe.-search" target="_self">
     <span>
      <code class="leafygreen-ui-1l06pbn">
       $search
      </code>
     </span>
    </a>
    or the
    <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/atlas-search/aggregation-stages/searchMeta/#mongodb-pipeline-pipe.-searchMeta" target="_self">
     <span>
      <code class="leafygreen-ui-1l06pbn">
       $searchMeta
      </code>
     </span>
    </a>
    stage
must be the first stage inside the
    <code class="leafygreen-ui-1l06pbn">
     $unionWith
    </code>
    pipeline.
   </p>
   <div aria-hidden="true">
   </div>
   <div class="leafygreen-ui-1b75luy" data-lgid="lg-tabs">
    <div class="leafygreen-ui-jp7pox" id="tabs-12">
     <div aria-label="Tabs to describe usage of search/searchmeta" aria-orientation="horizontal" class="leafygreen-ui-m8p1vb lg-ui-tab-list-0000" data-lgid="lg-tabs-tab_list" role="tablist">
      <button aria-controls="_yirrfrtpn" aria-selected="true" class="leafygreen-ui-1l773z1" data-text="" id="_e4w8bibwa" name="" role="tab" tabindex="0">
       <div class="leafygreen-ui-1nxuzmg">
        $search
       </div>
      </button>
      <button aria-controls="_2n53qiova" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_ig6d74xqd" name="" role="tab" tabindex="-1">
       <div class="leafygreen-ui-1nxuzmg">
        $searchMeta
       </div>
      </button>
     </div>
     <div class="leafygreen-ui-ov1ktg">
      <div class="leafygreen-ui-1sg2lsz">
      </div>
     </div>
    </div>
    <div class="lg-ui-tab-panels-0000" data-lgid="lg-tabs-tab_panels">
     <div>
      <div aria-labelledby="_h1h9yjajf" class="" id="_ddx66keh3" role="tabpanel">
       <div class="leafygreen-ui-1kdmo88">
        <script class="structured_data" type="application/ld+json">
         {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"[{\n  \"$unionWith\": {\n    \"coll\": &lt;collection-name&gt;,\n    \"pipeline\": [{\n      \"$search\": {\n        \"&lt;operator&gt;\": {\n          &lt;operator-specification&gt;\n        }\n      },\n      ...\n    }]\n  }\n}]"}
        </script>
        <div class="css-1lju8ps">
         <div class="leafygreen-ui-196mwvg">
          <div class="leafygreen-ui-1jheeh3">
           <pre class="leafygreen-ui-1453az5" tabindex="-1"><code class="lg-highlight-hljs-light none leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">[{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  "$unionWith": {</td></tr><tr class=""><td class="leafygreen-ui-7razhx">    "coll": &lt;collection-name&gt;,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">    "pipeline": [{</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx">      "$search": {</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx">        "&lt;operator&gt;": {</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx">          &lt;operator-specification&gt;</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx">        }</td></tr><tr class="leafygreen-ui-1wr1lm6"><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><tr class=""><td class="leafygreen-ui-7razhx">  }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}]</td></tr></tbody></table></code></pre>
          </div>
         </div>
        </div>
       </div>
      </div>
     </div>
     <div>
      <div aria-labelledby="_pfbo9mcpa" class="leafygreen-ui-38lglc" id="_wzsthfua1" role="tabpanel">
       <div class="leafygreen-ui-1kdmo88">
        <script class="structured_data" type="application/ld+json">
         {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"[{\n  \"$unionWith\": {\n    \"coll\": &lt;collection-name&gt;,\n    \"pipeline\": [{\n      \"$searchMeta\": {\n        \"&lt;collector&gt;\": {\n          &lt;collector-specification&gt;\n        }\n      },\n      ...\n    }]\n  }\n}]"}
        </script>
        <div class="css-1lju8ps">
         <div class="leafygreen-ui-196mwvg">
          <div class="leafygreen-ui-1jheeh3">
           <pre class="leafygreen-ui-1453az5" tabindex="-1"><code class="lg-highlight-hljs-light none leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">[{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  "$unionWith": {</td></tr><tr class=""><td class="leafygreen-ui-7razhx">    "coll": &lt;collection-name&gt;,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">    "pipeline": [{</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx">      "$searchMeta": {</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx">        "&lt;collector&gt;": {</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx">          &lt;collector-specification&gt;</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx">        }</td></tr><tr class="leafygreen-ui-1wr1lm6"><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><tr class=""><td class="leafygreen-ui-7razhx">  }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}]</td></tr></tbody></table></code></pre>
          </div>
         </div>
        </div>
       </div>
      </div>
     </div>
    </div>
   </div>
   <p class="leafygreen-ui-1kp3ins">
    To see an example of
    <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-unionWith" target="_self">
     <span>
      <code class="leafygreen-ui-1l06pbn">
       $unionWith
      </code>
     </span>
    </a>
    with
    <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/atlas-search/aggregation-stages/search/#mongodb-pipeline-pipe.-search" target="_self">
     <span>
      <code class="leafygreen-ui-1l06pbn">
       $search
      </code>
     </span>
    </a>
    ,
see the Atlas Search tutorial
    <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/atlas-search/tutorial/search-with-unionwith/" target="_self">
     <span>
      Run an Atlas Search $search Query
Using $unionWith
      <!-- -->
      .
     </span>
    </a>
   </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="">
       <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
          <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-unionWith" target="_self">
           <span>
            <code class="leafygreen-ui-1l06pbn">
             $unionWith
            </code>
           </span>
          </a>
          inside
          <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/transactions/#std-label-transactions">
           transactions
           <!-- -->
           .
          </a>
         </div>
        </div>
       </td>
      </tr>
      <tr class="">
       <td class="leafygreen-ui-1drjk7h">
        <div class="leafygreen-ui-1n4j8xf">
         <div>
          Sharded Collection
         </div>
        </div>
       </td>
       <td class="leafygreen-ui-1drjk7h">
        <div class="leafygreen-ui-1n4j8xf">
         <div>
          If the
          <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-unionWith" target="_self">
           <span>
            <code class="leafygreen-ui-1l06pbn">
             $unionWith
            </code>
           </span>
          </a>
          stage is part of the
          <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/lookup/#std-label-lookup-join-pipeline">
           $lookup
pipeline
          </a>
          , the
          <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-unionWith-coll" target="_self">
           <span>
            $unionWith coll
           </span>
          </a>
          cannot be sharded.
         </div>
        </div>
       </td>
      </tr>
      <tr class="">
       <td class="leafygreen-ui-1drjk7h">
        <div class="leafygreen-ui-1n4j8xf">
         <div>
          <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/out/#mongodb-pipeline-pipe.-out">
           <code class="leafygreen-ui-1l06pbn">
            $out
           </code>
          </a>
         </div>
        </div>
       </td>
       <td class="leafygreen-ui-1drjk7h">
        <div class="leafygreen-ui-1n4j8xf">
         <div>
          The
          <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-unionWith-pipeline" target="_self">
           <span>
            $unionWith pipeline
           </span>
          </a>
          cannot
include the
          <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/out/#mongodb-pipeline-pipe.-out">
           <code class="leafygreen-ui-1l06pbn">
            $out
           </code>
          </a>
          stage.
         </div>
        </div>
       </td>
      </tr>
      <tr class="">
       <td class="leafygreen-ui-1drjk7h">
        <div class="leafygreen-ui-1n4j8xf">
         <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>
       </td>
       <td class="leafygreen-ui-1drjk7h">
        <div class="leafygreen-ui-1n4j8xf">
         <div>
          The
          <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-unionWith-pipeline" target="_self">
           <span>
            $unionWith pipeline
           </span>
          </a>
          cannot
include the
          <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/merge/#mongodb-pipeline-pipe.-merge">
           <code class="leafygreen-ui-1l06pbn">
            $merge
           </code>
          </a>
          stage.
         </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>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    Create Sales Reports from the Union of Yearly Data Collections
    <a class="headerlink leafygreen-ui-1kepx47" href="#create-sales-reports-from-the-union-of-yearly-data-collections" title="Permalink to this heading">
     <svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
      <path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
      </path>
      <path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
      </path>
     </svg>
     <div class="css-1btu0w3 evvkmar0" id="create-sales-reports-from-the-union-of-yearly-data-collections">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    The following examples use the
    <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-unionWith" target="_self">
     <span>
      <code class="leafygreen-ui-1l06pbn">
       $unionWith
      </code>
     </span>
    </a>
    stage to combine data and
return results from multiple collections. In these examples, each collection
contains a year of sales data.
   </p>
   <section>
    <h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
     Populate Sample Data
     <a class="headerlink leafygreen-ui-1kepx47" href="#populate-sample-data" 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="populate-sample-data">
      </div>
     </a>
    </h4>
    <ol class="leafygreen-ui-rioki0" type="1">
     <li class="leafygreen-ui-d7p7b6">
      <p class="leafygreen-ui-1kp3ins">
       Create a
       <code class="leafygreen-ui-1l06pbn">
        sales_2017
       </code>
       collection with the following documents:
      </p>
      <script class="structured_data" type="application/ld+json">
       {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.sales_2017.insertMany( [\n  { store: \"General Store\", item: \"Chocolates\", quantity: 150 },\n  { store: \"ShopMart\", item: \"Chocolates\", quantity: 50 },\n  { store: \"General Store\", item: \"Cookies\", quantity: 100 },\n  { store: \"ShopMart\", item: \"Cookies\", quantity: 120 },\n  { store: \"General Store\", item: \"Pie\", quantity: 10 },\n  { store: \"ShopMart\", item: \"Pie\", quantity: 5 }\n] )","programmingLanguage":"JavaScript"}
      </script>
      <div class="css-16lmvu9">
       <div class="leafygreen-ui-196mwvg">
        <div class="leafygreen-ui-1b1bvay">
         <pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">sales_2017</span>.<span class="lg-highlight-title lg-highlight-function">insertMany</span>( <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->{ <span class="lg-highlight-attr">store</span>: <span class="lg-highlight-string">"General Store"</span>, <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-string">"Chocolates"</span>, <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-number">150</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->{ <span class="lg-highlight-attr">store</span>: <span class="lg-highlight-string">"ShopMart"</span>, <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-string">"Chocolates"</span>, <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-number">50</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->{ <span class="lg-highlight-attr">store</span>: <span class="lg-highlight-string">"General Store"</span>, <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-string">"Cookies"</span>, <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-number">100</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->{ <span class="lg-highlight-attr">store</span>: <span class="lg-highlight-string">"ShopMart"</span>, <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-string">"Cookies"</span>, <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-number">120</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->{ <span class="lg-highlight-attr">store</span>: <span class="lg-highlight-string">"General Store"</span>, <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-string">"Pie"</span>, <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-number">10</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->{ <span class="lg-highlight-attr">store</span>: <span class="lg-highlight-string">"ShopMart"</span>, <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-string">"Pie"</span>, <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-number">5</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">] )</td></tr></tbody></table></code></pre>
         <div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
          <button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
           <div class="leafygreen-ui-xhlipt">
            <svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
             <path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
             </path>
            </svg>
           </div>
          </button>
         </div>
        </div>
       </div>
      </div>
     </li>
     <li class="leafygreen-ui-d7p7b6">
      <p class="leafygreen-ui-1kp3ins">
       Create a
       <code class="leafygreen-ui-1l06pbn">
        sales_2018
       </code>
       collection with the following documents:
      </p>
      <script class="structured_data" type="application/ld+json">
       {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.sales_2018.insertMany( [\n  { store: \"General Store\", item: \"Cheese\", quantity: 30 },\n  { store: \"ShopMart\", item: \"Cheese\", quantity: 50 },\n  { store: \"General Store\", item: \"Chocolates\", quantity: 125 },\n  { store: \"ShopMart\", item: \"Chocolates\", quantity: 150 },\n  { store: \"General Store\", item: \"Cookies\", quantity: 200 },\n  { store: \"ShopMart\", item: \"Cookies\", quantity: 100 },\n  { store: \"ShopMart\", item: \"Nuts\", quantity: 100 },\n  { store: \"General Store\", item: \"Pie\", quantity: 30 },\n  { store: \"ShopMart\", item: \"Pie\", quantity: 25 }\n] )","programmingLanguage":"JavaScript"}
      </script>
      <div class="css-16lmvu9">
       <div class="leafygreen-ui-196mwvg">
        <div class="leafygreen-ui-1b1bvay">
         <pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">sales_2018</span>.<span class="lg-highlight-title lg-highlight-function">insertMany</span>( <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->{ <span class="lg-highlight-attr">store</span>: <span class="lg-highlight-string">"General Store"</span>, <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-string">"Cheese"</span>, <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-number">30</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->{ <span class="lg-highlight-attr">store</span>: <span class="lg-highlight-string">"ShopMart"</span>, <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-string">"Cheese"</span>, <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-number">50</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->{ <span class="lg-highlight-attr">store</span>: <span class="lg-highlight-string">"General Store"</span>, <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-string">"Chocolates"</span>, <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-number">125</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->{ <span class="lg-highlight-attr">store</span>: <span class="lg-highlight-string">"ShopMart"</span>, <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-string">"Chocolates"</span>, <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-number">150</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->{ <span class="lg-highlight-attr">store</span>: <span class="lg-highlight-string">"General Store"</span>, <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-string">"Cookies"</span>, <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-number">200</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->{ <span class="lg-highlight-attr">store</span>: <span class="lg-highlight-string">"ShopMart"</span>, <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-string">"Cookies"</span>, <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-number">100</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->{ <span class="lg-highlight-attr">store</span>: <span class="lg-highlight-string">"ShopMart"</span>, <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-string">"Nuts"</span>, <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-number">100</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->{ <span class="lg-highlight-attr">store</span>: <span class="lg-highlight-string">"General Store"</span>, <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-string">"Pie"</span>, <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-number">30</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->{ <span class="lg-highlight-attr">store</span>: <span class="lg-highlight-string">"ShopMart"</span>, <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-string">"Pie"</span>, <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-number">25</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">] )</td></tr></tbody></table></code></pre>
         <div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
          <button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
           <div class="leafygreen-ui-xhlipt">
            <svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
             <path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
             </path>
            </svg>
           </div>
          </button>
         </div>
        </div>
       </div>
      </div>
     </li>
     <li class="leafygreen-ui-d7p7b6">
      <p class="leafygreen-ui-1kp3ins">
       Create a
       <code class="leafygreen-ui-1l06pbn">
        sales_2019
       </code>
       collection with the following documents:
      </p>
      <script class="structured_data" type="application/ld+json">
       {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.sales_2019.insertMany( [\n  { store: \"General Store\", item: \"Cheese\", quantity: 50 },\n  { store: \"ShopMart\", item: \"Cheese\", quantity: 20 },\n  { store: \"General Store\", item: \"Chocolates\", quantity: 125 },\n  { store: \"ShopMart\", item: \"Chocolates\", quantity: 150 },\n  { store: \"General Store\", item: \"Cookies\", quantity: 200 },\n  { store: \"ShopMart\", item: \"Cookies\", quantity: 100 },\n  { store: \"General Store\", item: \"Nuts\", quantity: 80 },\n  { store: \"ShopMart\", item: \"Nuts\", quantity: 30 },\n  { store: \"General Store\", item: \"Pie\", quantity: 50 },\n  { store: \"ShopMart\", item: \"Pie\", quantity: 75 }\n] )","programmingLanguage":"JavaScript"}
      </script>
      <div class="css-16lmvu9">
       <div class="leafygreen-ui-196mwvg">
        <div class="leafygreen-ui-1b1bvay">
         <pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">sales_2019</span>.<span class="lg-highlight-title lg-highlight-function">insertMany</span>( <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->{ <span class="lg-highlight-attr">store</span>: <span class="lg-highlight-string">"General Store"</span>, <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-string">"Cheese"</span>, <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-number">50</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->{ <span class="lg-highlight-attr">store</span>: <span class="lg-highlight-string">"ShopMart"</span>, <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-string">"Cheese"</span>, <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-number">20</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->{ <span class="lg-highlight-attr">store</span>: <span class="lg-highlight-string">"General Store"</span>, <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-string">"Chocolates"</span>, <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-number">125</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->{ <span class="lg-highlight-attr">store</span>: <span class="lg-highlight-string">"ShopMart"</span>, <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-string">"Chocolates"</span>, <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-number">150</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->{ <span class="lg-highlight-attr">store</span>: <span class="lg-highlight-string">"General Store"</span>, <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-string">"Cookies"</span>, <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-number">200</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->{ <span class="lg-highlight-attr">store</span>: <span class="lg-highlight-string">"ShopMart"</span>, <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-string">"Cookies"</span>, <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-number">100</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->{ <span class="lg-highlight-attr">store</span>: <span class="lg-highlight-string">"General Store"</span>, <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-string">"Nuts"</span>, <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-number">80</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->{ <span class="lg-highlight-attr">store</span>: <span class="lg-highlight-string">"ShopMart"</span>, <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-string">"Nuts"</span>, <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-number">30</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->{ <span class="lg-highlight-attr">store</span>: <span class="lg-highlight-string">"General Store"</span>, <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-string">"Pie"</span>, <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-number">50</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->{ <span class="lg-highlight-attr">store</span>: <span class="lg-highlight-string">"ShopMart"</span>, <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-string">"Pie"</span>, <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-number">75</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">] )</td></tr></tbody></table></code></pre>
         <div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
          <button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
           <div class="leafygreen-ui-xhlipt">
            <svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
             <path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
             </path>
            </svg>
           </div>
          </button>
         </div>
        </div>
       </div>
      </div>
     </li>
     <li class="leafygreen-ui-d7p7b6">
      <p class="leafygreen-ui-1kp3ins">
       Create a
       <code class="leafygreen-ui-1l06pbn">
        sales_2020
       </code>
       collection with the following documents:
      </p>
      <script class="structured_data" type="application/ld+json">
       {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.sales_2020.insertMany( [\n  { store: \"General Store\", item: \"Cheese\", quantity: 100, },\n  { store: \"ShopMart\", item: \"Cheese\", quantity: 100},\n  { store: \"General Store\", item: \"Chocolates\", quantity: 200 },\n  { store: \"ShopMart\", item: \"Chocolates\", quantity: 300 },\n  { store: \"General Store\", item: \"Cookies\", quantity: 500 },\n  { store: \"ShopMart\", item: \"Cookies\", quantity: 400 },\n  { store: \"General Store\", item: \"Nuts\", quantity: 100 },\n  { store: \"ShopMart\", item: \"Nuts\", quantity: 200 },\n  { store: \"General Store\", item: \"Pie\", quantity: 100 },\n  { store: \"ShopMart\", item: \"Pie\", quantity: 100 }\n] )","programmingLanguage":"JavaScript"}
      </script>
      <div class="css-16lmvu9">
       <div class="leafygreen-ui-196mwvg">
        <div class="leafygreen-ui-1b1bvay">
         <pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">sales_2020</span>.<span class="lg-highlight-title lg-highlight-function">insertMany</span>( <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->{ <span class="lg-highlight-attr">store</span>: <span class="lg-highlight-string">"General Store"</span>, <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-string">"Cheese"</span>, <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-number">100</span>, <!-- -->}<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->{ <span class="lg-highlight-attr">store</span>: <span class="lg-highlight-string">"ShopMart"</span>, <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-string">"Cheese"</span>, <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-number">100</span>}<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->{ <span class="lg-highlight-attr">store</span>: <span class="lg-highlight-string">"General Store"</span>, <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-string">"Chocolates"</span>, <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-number">200</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->{ <span class="lg-highlight-attr">store</span>: <span class="lg-highlight-string">"ShopMart"</span>, <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-string">"Chocolates"</span>, <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-number">300</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->{ <span class="lg-highlight-attr">store</span>: <span class="lg-highlight-string">"General Store"</span>, <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-string">"Cookies"</span>, <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-number">500</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->{ <span class="lg-highlight-attr">store</span>: <span class="lg-highlight-string">"ShopMart"</span>, <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-string">"Cookies"</span>, <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-number">400</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->{ <span class="lg-highlight-attr">store</span>: <span class="lg-highlight-string">"General Store"</span>, <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-string">"Nuts"</span>, <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-number">100</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->{ <span class="lg-highlight-attr">store</span>: <span class="lg-highlight-string">"ShopMart"</span>, <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-string">"Nuts"</span>, <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-number">200</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->{ <span class="lg-highlight-attr">store</span>: <span class="lg-highlight-string">"General Store"</span>, <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-string">"Pie"</span>, <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-number">100</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->{ <span class="lg-highlight-attr">store</span>: <span class="lg-highlight-string">"ShopMart"</span>, <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-string">"Pie"</span>, <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-number">100</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">] )</td></tr></tbody></table></code></pre>
         <div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
          <button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
           <div class="leafygreen-ui-xhlipt">
            <svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
             <path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
             </path>
            </svg>
           </div>
          </button>
         </div>
        </div>
       </div>
      </div>
     </li>
    </ol>
   </section>
  </section>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    Report 1: All Sales by Year and Stores and Items
    <a class="headerlink leafygreen-ui-1kepx47" href="#report-1--all-sales-by-year-and-stores-and-items" 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="report-1--all-sales-by-year-and-stores-and-items">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    The following aggregation creates a yearly sales report that lists all sales by
quarter and stores. The pipeline uses
    <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-unionWith" target="_self">
     <span>
      <code class="leafygreen-ui-1l06pbn">
       $unionWith
      </code>
     </span>
    </a>
    to combine documents
from all four collections:
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.sales_2017.aggregate( [\n   { $set: { _id: \"2017\" } },\n   { $unionWith: { coll: \"sales_2018\", pipeline: [ { $set: { _id: \"2018\" } } ] } },\n   { $unionWith: { coll: \"sales_2019\", pipeline: [ { $set: { _id: \"2019\" } } ] } },\n   { $unionWith: { coll: \"sales_2020\", pipeline: [ { $set: { _id: \"2020\" } } ] } },\n   { $sort: { _id: 1, store: 1, item: 1 } }\n] )","programmingLanguage":"JavaScript"}
   </script>
   <div class="css-16lmvu9">
    <div class="leafygreen-ui-196mwvg">
     <div class="leafygreen-ui-1b1bvay">
      <pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">sales_2017</span>.<span class="lg-highlight-title lg-highlight-function">aggregate</span>( <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">$set</span>: <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-string">"2017"</span> } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">$unionWith</span>: <!-- -->{ <span class="lg-highlight-attr">coll</span>: <span class="lg-highlight-string">"sales_2018"</span>, <span class="lg-highlight-attr">pipeline</span>: <!-- -->[ <!-- -->{ <span class="lg-highlight-attr">$set</span>: <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-string">"2018"</span> } } ] } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">$unionWith</span>: <!-- -->{ <span class="lg-highlight-attr">coll</span>: <span class="lg-highlight-string">"sales_2019"</span>, <span class="lg-highlight-attr">pipeline</span>: <!-- -->[ <!-- -->{ <span class="lg-highlight-attr">$set</span>: <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-string">"2019"</span> } } ] } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">$unionWith</span>: <!-- -->{ <span class="lg-highlight-attr">coll</span>: <span class="lg-highlight-string">"sales_2020"</span>, <span class="lg-highlight-attr">pipeline</span>: <!-- -->[ <!-- -->{ <span class="lg-highlight-attr">$set</span>: <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-string">"2020"</span> } } ] } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">$sort</span>: <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">store</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-number">1</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">] )</td></tr></tbody></table></code></pre>
      <div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
       <button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
        <div class="leafygreen-ui-xhlipt">
         <svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
          <path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
          </path>
         </svg>
        </div>
       </button>
      </div>
     </div>
    </div>
   </div>
   <p class="leafygreen-ui-1kp3ins">
    Specifically, the aggregation pipeline uses:
   </p>
   <ul class="leafygreen-ui-rioki0">
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      A
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/set/#mongodb-pipeline-pipe.-set">
       <code class="leafygreen-ui-1l06pbn">
        $set
       </code>
      </a>
      stage to update the
      <code class="leafygreen-ui-1l06pbn">
       _id
      </code>
      field to contain the
year.
     </p>
    </li>
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      A sequence of
      <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-unionWith" target="_self">
       <span>
        <code class="leafygreen-ui-1l06pbn">
         $unionWith
        </code>
       </span>
      </a>
      stages to combine all documents
from the four collections, each also using the
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/set/#mongodb-pipeline-pipe.-set">
       <code class="leafygreen-ui-1l06pbn">
        $set
       </code>
      </a>
      stage
on its documents.
     </p>
    </li>
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      A
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/sort/#mongodb-pipeline-pipe.-sort">
       <code class="leafygreen-ui-1l06pbn">
        $sort
       </code>
      </a>
      stage to sort by the
      <code class="leafygreen-ui-1l06pbn">
       _id
      </code>
      (the year),
the
      <code class="leafygreen-ui-1l06pbn">
       store
      </code>
      , and
      <code class="leafygreen-ui-1l06pbn">
       item
      </code>
      .
     </p>
    </li>
   </ul>
   <p class="leafygreen-ui-1kp3ins">
    Pipeline output:
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : \"2017\", \"store\" : \"General Store\", \"item\" : \"Chocolates\", \"quantity\" : 150 }\n{ \"_id\" : \"2017\", \"store\" : \"General Store\", \"item\" : \"Cookies\", \"quantity\" : 100 }\n{ \"_id\" : \"2017\", \"store\" : \"General Store\", \"item\" : \"Pie\", \"quantity\" : 10 }\n{ \"_id\" : \"2017\", \"store\" : \"ShopMart\", \"item\" : \"Chocolates\", \"quantity\" : 50 }\n{ \"_id\" : \"2017\", \"store\" : \"ShopMart\", \"item\" : \"Cookies\", \"quantity\" : 120 }\n{ \"_id\" : \"2017\", \"store\" : \"ShopMart\", \"item\" : \"Pie\", \"quantity\" : 5 }\n{ \"_id\" : \"2018\", \"store\" : \"General Store\", \"item\" : \"Cheese\", \"quantity\" : 30 }\n{ \"_id\" : \"2018\", \"store\" : \"General Store\", \"item\" : \"Chocolates\", \"quantity\" : 125 }\n{ \"_id\" : \"2018\", \"store\" : \"General Store\", \"item\" : \"Cookies\", \"quantity\" : 200 }\n{ \"_id\" : \"2018\", \"store\" : \"General Store\", \"item\" : \"Pie\", \"quantity\" : 30 }\n{ \"_id\" : \"2018\", \"store\" : \"ShopMart\", \"item\" : \"Cheese\", \"quantity\" : 50 }\n{ \"_id\" : \"2018\", \"store\" : \"ShopMart\", \"item\" : \"Chocolates\", \"quantity\" : 150 }\n{ \"_id\" : \"2018\", \"store\" : \"ShopMart\", \"item\" : \"Cookies\", \"quantity\" : 100 }\n{ \"_id\" : \"2018\", \"store\" : \"ShopMart\", \"item\" : \"Nuts\", \"quantity\" : 100 }\n{ \"_id\" : \"2018\", \"store\" : \"ShopMart\", \"item\" : \"Pie\", \"quantity\" : 25 }\n{ \"_id\" : \"2019\", \"store\" : \"General Store\", \"item\" : \"Cheese\", \"quantity\" : 50 }\n{ \"_id\" : \"2019\", \"store\" : \"General Store\", \"item\" : \"Chocolates\", \"quantity\" : 125 }\n{ \"_id\" : \"2019\", \"store\" : \"General Store\", \"item\" : \"Cookies\", \"quantity\" : 200 }\n{ \"_id\" : \"2019\", \"store\" : \"General Store\", \"item\" : \"Nuts\", \"quantity\" : 80 }\n{ \"_id\" : \"2019\", \"store\" : \"General Store\", \"item\" : \"Pie\", \"quantity\" : 50 }\n{ \"_id\" : \"2019\", \"store\" : \"ShopMart\", \"item\" : \"Cheese\", \"quantity\" : 20 }\n{ \"_id\" : \"2019\", \"store\" : \"ShopMart\", \"item\" : \"Chocolates\", \"quantity\" : 150 }\n{ \"_id\" : \"2019\", \"store\" : \"ShopMart\", \"item\" : \"Cookies\", \"quantity\" : 100 }\n{ \"_id\" : \"2019\", \"store\" : \"ShopMart\", \"item\" : \"Nuts\", \"quantity\" : 30 }\n{ \"_id\" : \"2019\", \"store\" : \"ShopMart\", \"item\" : \"Pie\", \"quantity\" : 75 }\n{ \"_id\" : \"2020\", \"store\" : \"General Store\", \"item\" : \"Cheese\", \"quantity\" : 100 }\n{ \"_id\" : \"2020\", \"store\" : \"General Store\", \"item\" : \"Chocolates\", \"quantity\" : 200 }\n{ \"_id\" : \"2020\", \"store\" : \"General Store\", \"item\" : \"Cookies\", \"quantity\" : 500 }\n{ \"_id\" : \"2020\", \"store\" : \"General Store\", \"item\" : \"Nuts\", \"quantity\" : 100 }\n{ \"_id\" : \"2020\", \"store\" : \"General Store\", \"item\" : \"Pie\", \"quantity\" : 100 }\n{ \"_id\" : \"2020\", \"store\" : \"ShopMart\", \"item\" : \"Cheese\", \"quantity\" : 100 }\n{ \"_id\" : \"2020\", \"store\" : \"ShopMart\", \"item\" : \"Chocolates\", \"quantity\" : 300 }\n{ \"_id\" : \"2020\", \"store\" : \"ShopMart\", \"item\" : \"Cookies\", \"quantity\" : 400 }\n{ \"_id\" : \"2020\", \"store\" : \"ShopMart\", \"item\" : \"Nuts\", \"quantity\" : 200 }\n{ \"_id\" : \"2020\", \"store\" : \"ShopMart\", \"item\" : \"Pie\", \"quantity\" : 100 }","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">"2017"</span>, <span class="lg-highlight-string">"store"</span> <!-- -->: <span class="lg-highlight-string">"General Store"</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"Chocolates"</span>, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">150</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"2017"</span>, <span class="lg-highlight-string">"store"</span> <!-- -->: <span class="lg-highlight-string">"General Store"</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"Cookies"</span>, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">100</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"2017"</span>, <span class="lg-highlight-string">"store"</span> <!-- -->: <span class="lg-highlight-string">"General Store"</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"Pie"</span>, <span class="lg-highlight-string">"quantity"</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-string">"2017"</span>, <span class="lg-highlight-string">"store"</span> <!-- -->: <span class="lg-highlight-string">"ShopMart"</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"Chocolates"</span>, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">50</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"2017"</span>, <span class="lg-highlight-string">"store"</span> <!-- -->: <span class="lg-highlight-string">"ShopMart"</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"Cookies"</span>, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">120</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"2017"</span>, <span class="lg-highlight-string">"store"</span> <!-- -->: <span class="lg-highlight-string">"ShopMart"</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"Pie"</span>, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">5</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"2018"</span>, <span class="lg-highlight-string">"store"</span> <!-- -->: <span class="lg-highlight-string">"General Store"</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"Cheese"</span>, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">30</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"2018"</span>, <span class="lg-highlight-string">"store"</span> <!-- -->: <span class="lg-highlight-string">"General Store"</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"Chocolates"</span>, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">125</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"2018"</span>, <span class="lg-highlight-string">"store"</span> <!-- -->: <span class="lg-highlight-string">"General Store"</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"Cookies"</span>, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">200</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"2018"</span>, <span class="lg-highlight-string">"store"</span> <!-- -->: <span class="lg-highlight-string">"General Store"</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"Pie"</span>, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">30</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"2018"</span>, <span class="lg-highlight-string">"store"</span> <!-- -->: <span class="lg-highlight-string">"ShopMart"</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"Cheese"</span>, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">50</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"2018"</span>, <span class="lg-highlight-string">"store"</span> <!-- -->: <span class="lg-highlight-string">"ShopMart"</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"Chocolates"</span>, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">150</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"2018"</span>, <span class="lg-highlight-string">"store"</span> <!-- -->: <span class="lg-highlight-string">"ShopMart"</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"Cookies"</span>, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">100</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"2018"</span>, <span class="lg-highlight-string">"store"</span> <!-- -->: <span class="lg-highlight-string">"ShopMart"</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"Nuts"</span>, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">100</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"2018"</span>, <span class="lg-highlight-string">"store"</span> <!-- -->: <span class="lg-highlight-string">"ShopMart"</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"Pie"</span>, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">25</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"2019"</span>, <span class="lg-highlight-string">"store"</span> <!-- -->: <span class="lg-highlight-string">"General Store"</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"Cheese"</span>, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">50</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"2019"</span>, <span class="lg-highlight-string">"store"</span> <!-- -->: <span class="lg-highlight-string">"General Store"</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"Chocolates"</span>, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">125</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"2019"</span>, <span class="lg-highlight-string">"store"</span> <!-- -->: <span class="lg-highlight-string">"General Store"</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"Cookies"</span>, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">200</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"2019"</span>, <span class="lg-highlight-string">"store"</span> <!-- -->: <span class="lg-highlight-string">"General Store"</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"Nuts"</span>, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">80</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"2019"</span>, <span class="lg-highlight-string">"store"</span> <!-- -->: <span class="lg-highlight-string">"General Store"</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"Pie"</span>, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">50</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"2019"</span>, <span class="lg-highlight-string">"store"</span> <!-- -->: <span class="lg-highlight-string">"ShopMart"</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"Cheese"</span>, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">20</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"2019"</span>, <span class="lg-highlight-string">"store"</span> <!-- -->: <span class="lg-highlight-string">"ShopMart"</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"Chocolates"</span>, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">150</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"2019"</span>, <span class="lg-highlight-string">"store"</span> <!-- -->: <span class="lg-highlight-string">"ShopMart"</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"Cookies"</span>, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">100</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"2019"</span>, <span class="lg-highlight-string">"store"</span> <!-- -->: <span class="lg-highlight-string">"ShopMart"</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"Nuts"</span>, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">30</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"2019"</span>, <span class="lg-highlight-string">"store"</span> <!-- -->: <span class="lg-highlight-string">"ShopMart"</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"Pie"</span>, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">75</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"2020"</span>, <span class="lg-highlight-string">"store"</span> <!-- -->: <span class="lg-highlight-string">"General Store"</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"Cheese"</span>, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">100</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"2020"</span>, <span class="lg-highlight-string">"store"</span> <!-- -->: <span class="lg-highlight-string">"General Store"</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"Chocolates"</span>, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">200</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"2020"</span>, <span class="lg-highlight-string">"store"</span> <!-- -->: <span class="lg-highlight-string">"General Store"</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"Cookies"</span>, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">500</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"2020"</span>, <span class="lg-highlight-string">"store"</span> <!-- -->: <span class="lg-highlight-string">"General Store"</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"Nuts"</span>, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">100</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"2020"</span>, <span class="lg-highlight-string">"store"</span> <!-- -->: <span class="lg-highlight-string">"General Store"</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"Pie"</span>, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">100</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"2020"</span>, <span class="lg-highlight-string">"store"</span> <!-- -->: <span class="lg-highlight-string">"ShopMart"</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"Cheese"</span>, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">100</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"2020"</span>, <span class="lg-highlight-string">"store"</span> <!-- -->: <span class="lg-highlight-string">"ShopMart"</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"Chocolates"</span>, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">300</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"2020"</span>, <span class="lg-highlight-string">"store"</span> <!-- -->: <span class="lg-highlight-string">"ShopMart"</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"Cookies"</span>, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">400</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"2020"</span>, <span class="lg-highlight-string">"store"</span> <!-- -->: <span class="lg-highlight-string">"ShopMart"</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"Nuts"</span>, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">200</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"2020"</span>, <span class="lg-highlight-string">"store"</span> <!-- -->: <span class="lg-highlight-string">"ShopMart"</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"Pie"</span>, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">100</span> }</td></tr></tbody></table></code></pre>
     </div>
    </div>
   </div>
  </section>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    Report 2: Aggregated Sales by Items
    <a class="headerlink leafygreen-ui-1kepx47" href="#report-2--aggregated-sales-by-items" 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="report-2--aggregated-sales-by-items">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    The following aggregation creates a sales report that lists the sales quantity
per item. The pipeline uses
    <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-unionWith" target="_self">
     <span>
      <code class="leafygreen-ui-1l06pbn">
       $unionWith
      </code>
     </span>
    </a>
    to combine documents from all
four years:
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.sales_2017.aggregate( [\n   { $unionWith: \"sales_2018\" },\n   { $unionWith: \"sales_2019\" },\n   { $unionWith: \"sales_2020\" },\n   { $group: { _id: \"$item\", total: { $sum: \"$quantity\" } } },\n   { $sort: { total: -1 } }\n] )","programmingLanguage":"JavaScript"}
   </script>
   <div class="css-16lmvu9">
    <div class="leafygreen-ui-196mwvg">
     <div class="leafygreen-ui-1b1bvay">
      <pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">sales_2017</span>.<span class="lg-highlight-title lg-highlight-function">aggregate</span>( <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">$unionWith</span>: <span class="lg-highlight-string">"sales_2018"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">$unionWith</span>: <span class="lg-highlight-string">"sales_2019"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">$unionWith</span>: <span class="lg-highlight-string">"sales_2020"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">$group</span>: <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-string">"$item"</span>, <span class="lg-highlight-attr">total</span>: <!-- -->{ <span class="lg-highlight-attr">$sum</span>: <span class="lg-highlight-string">"$quantity"</span> } } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">$sort</span>: <!-- -->{ <span class="lg-highlight-attr">total</span>: <!-- -->-<span class="lg-highlight-number">1</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">] )</td></tr></tbody></table></code></pre>
      <div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
       <button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
        <div class="leafygreen-ui-xhlipt">
         <svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
          <path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
          </path>
         </svg>
        </div>
       </button>
      </div>
     </div>
    </div>
   </div>
   <ul class="leafygreen-ui-rioki0">
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      The sequence of
      <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-unionWith" target="_self">
       <span>
        <code class="leafygreen-ui-1l06pbn">
         $unionWith
        </code>
       </span>
      </a>
      stages retrieve documents
from the specified collections into the pipeline:
     </p>
    </li>
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      The
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/group/#mongodb-pipeline-pipe.-group">
       <code class="leafygreen-ui-1l06pbn">
        $group
       </code>
      </a>
      stage groups by the
      <code class="leafygreen-ui-1l06pbn">
       item
      </code>
      field and uses
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/sum/#mongodb-group-grp.-sum">
       <code class="leafygreen-ui-1l06pbn">
        $sum
       </code>
      </a>
      to calculate the total sales quantity per
      <code class="leafygreen-ui-1l06pbn">
       item
      </code>
      .
     </p>
    </li>
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      The
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/sort/#mongodb-pipeline-pipe.-sort">
       <code class="leafygreen-ui-1l06pbn">
        $sort
       </code>
      </a>
      stage orders the documents by descending
      <code class="leafygreen-ui-1l06pbn">
       total
      </code>
      .
     </p>
    </li>
   </ul>
   <p class="leafygreen-ui-1kp3ins">
    Pipeline output:
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : \"Cookies\", \"total\" : 1720 }\n{ \"_id\" : \"Chocolates\", \"total\" : 1250 }\n{ \"_id\" : \"Nuts\", \"total\" : 510 }\n{ \"_id\" : \"Pie\", \"total\" : 395 }\n{ \"_id\" : \"Cheese\", \"total\" : 350 }","programmingLanguage":"JavaScript"}
   </script>
   <div class="css-1lju8ps">
    <div class="leafygreen-ui-196mwvg">
     <div class="leafygreen-ui-1jheeh3">
      <pre class="leafygreen-ui-1453az5" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"Cookies"</span>, <span class="lg-highlight-string">"total"</span> <!-- -->: <span class="lg-highlight-number">1720</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"Chocolates"</span>, <span class="lg-highlight-string">"total"</span> <!-- -->: <span class="lg-highlight-number">1250</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"Nuts"</span>, <span class="lg-highlight-string">"total"</span> <!-- -->: <span class="lg-highlight-number">510</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"Pie"</span>, <span class="lg-highlight-string">"total"</span> <!-- -->: <span class="lg-highlight-number">395</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"Cheese"</span>, <span class="lg-highlight-string">"total"</span> <!-- -->: <span class="lg-highlight-number">350</span> }</td></tr></tbody></table></code></pre>
     </div>
    </div>
   </div>
   <span class="leafygreen-ui-1qnjd6d" id="std-label-unionWith-example-specified-docs">
   </span>
  </section>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    Create a Union with Specified Documents
    <a class="headerlink leafygreen-ui-1kepx47" href="#create-a-union-with-specified-documents" 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-a-union-with-specified-documents">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    You can use
    <code class="leafygreen-ui-1l06pbn">
     $unionWith
    </code>
    to perform a union with documents that you
specify in the
    <code class="leafygreen-ui-1l06pbn">
     pipeline
    </code>
    field. When you specify a
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/documents/#mongodb-pipeline-pipe.-documents">
     <code class="leafygreen-ui-1l06pbn">
      $documents
     </code>
    </a>
    stage in the
    <code class="leafygreen-ui-1l06pbn">
     pipeline
    </code>
    field, you perform a
union with documents that aren't stored in a separate collection.
   </p>
   <p class="leafygreen-ui-1kp3ins">
    Create a collection
    <code class="leafygreen-ui-1l06pbn">
     cakeFlavors
    </code>
    :
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.cakeFlavors.insertMany( [\n   { _id: 1, flavor: \"chocolate\" },\n   { _id: 2, flavor: \"strawberry\" },\n   { _id: 3, flavor: \"cherry\" }\n] )","programmingLanguage":"JavaScript"}
   </script>
   <div class="css-16lmvu9">
    <div class="leafygreen-ui-196mwvg">
     <div class="leafygreen-ui-yzcs8i">
      <pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">cakeFlavors</span>.<span class="lg-highlight-title lg-highlight-function">insertMany</span>( <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">flavor</span>: <span class="lg-highlight-string">"chocolate"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-attr">flavor</span>: <span class="lg-highlight-string">"strawberry"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-attr">flavor</span>: <span class="lg-highlight-string">"cherry"</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">
    The following
    <code class="leafygreen-ui-1l06pbn">
     $unionWith
    </code>
    operation performs a union with documents
specified in the
    <code class="leafygreen-ui-1l06pbn">
     pipeline
    </code>
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/documents/#mongodb-pipeline-pipe.-documents">
     <code class="leafygreen-ui-1l06pbn">
      $documents
     </code>
    </a>
    field:
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.cakeFlavors.aggregate( [\n   {\n      $unionWith: {\n         pipeline: [\n            {\n               $documents: [\n                  { _id: 4, flavor: \"orange\" },\n                  { _id: 5, flavor: \"vanilla\", price: 20 }\n               ]\n            }\n         ]\n      }\n   }\n] )","programmingLanguage":"JavaScript"}
   </script>
   <div class="css-16lmvu9">
    <div class="leafygreen-ui-196mwvg">
     <div class="leafygreen-ui-yzcs8i">
      <pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">cakeFlavors</span>.<span class="lg-highlight-title lg-highlight-function">aggregate</span>( <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <span class="lg-highlight-attr">$unionWith</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">         <span class="lg-highlight-attr">pipeline</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx">            <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">               <span class="lg-highlight-attr">$documents</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx">                  <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">4</span>, <span class="lg-highlight-attr">flavor</span>: <span class="lg-highlight-string">"orange"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">                  <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">5</span>, <span class="lg-highlight-attr">flavor</span>: <span class="lg-highlight-string">"vanilla"</span>, <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">20</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><tr class=""><td class="leafygreen-ui-7razhx">      }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">] )</td></tr></tbody></table></code></pre>
      <div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
       <button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
        <div class="leafygreen-ui-xhlipt">
         <svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
          <path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
          </path>
         </svg>
        </div>
       </button>
      </div>
     </div>
    </div>
   </div>
   <p class="leafygreen-ui-1kp3ins">
    Output:
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"[\n   { _id: 1, flavor: 'chocolate' },\n   { _id: 2, flavor: 'strawberry' },\n   { _id: 3, flavor: 'cherry' },\n   { _id: 4, flavor: 'orange' },\n   { _id: 5, flavor: 'vanilla', price: 20 }\n]","programmingLanguage":"JavaScript"}
   </script>
   <div class="css-1lju8ps">
    <div class="leafygreen-ui-196mwvg">
     <div class="leafygreen-ui-1jheeh3">
      <pre class="leafygreen-ui-1453az5" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">[</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">flavor</span>: <span class="lg-highlight-string">'chocolate'</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-attr">flavor</span>: <span class="lg-highlight-string">'strawberry'</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-attr">flavor</span>: <span class="lg-highlight-string">'cherry'</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">4</span>, <span class="lg-highlight-attr">flavor</span>: <span class="lg-highlight-string">'orange'</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">5</span>, <span class="lg-highlight-attr">flavor</span>: <span class="lg-highlight-string">'vanilla'</span>, <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">20</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">]</td></tr></tbody></table></code></pre>
     </div>
    </div>
   </div>
   <span class="leafygreen-ui-1qnjd6d" id="std-label-unionWith-example-namespaces-in-subpipelines">
   </span>
  </section>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    Namespaces in Subpipelines
    <a class="headerlink leafygreen-ui-1kepx47" href="#namespaces-in-subpipelines" 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="namespaces-in-subpipelines">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    Starting in MongoDB 8.0, namespaces in subpipelines within
    <code class="leafygreen-ui-1l06pbn">
     $lookup
    </code>
    and
    <code class="leafygreen-ui-1l06pbn">
     $unionWith
    </code>
    are validated to ensure the correct use of
    <code class="leafygreen-ui-1l06pbn">
     from
    </code>
    and
    <code class="leafygreen-ui-1l06pbn">
     coll
    </code>
    fields:
   </p>
   <ul class="leafygreen-ui-rioki0">
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      For
      <code class="leafygreen-ui-1l06pbn">
       $lookup
      </code>
      , omit the
      <code class="leafygreen-ui-1l06pbn">
       from
      </code>
      field if you use a subpipeline with
a stage which doesn't require a specified collection. For example, a
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/documents/#mongodb-pipeline-pipe.-documents">
       <code class="leafygreen-ui-1l06pbn">
        $documents
       </code>
      </a>
      stage.
     </p>
    </li>
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      Similarly, for
      <code class="leafygreen-ui-1l06pbn">
       $unionWith
      </code>
      , omit the
      <code class="leafygreen-ui-1l06pbn">
       coll
      </code>
      field.
     </p>
    </li>
   </ul>
   <p class="leafygreen-ui-1kp3ins">
    Unchanged behavior:
   </p>
   <ul class="leafygreen-ui-rioki0">
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      For a
      <code class="leafygreen-ui-1l06pbn">
       $lookup
      </code>
      that starts with a stage for a collection, for
example a
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/match/#mongodb-pipeline-pipe.-match">
       <code class="leafygreen-ui-1l06pbn">
        $match
       </code>
      </a>
      or
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/collStats/#mongodb-pipeline-pipe.-collStats">
       <code class="leafygreen-ui-1l06pbn">
        $collStats
       </code>
      </a>
      subpipeline,
you must include the
      <code class="leafygreen-ui-1l06pbn">
       from
      </code>
      field and specify the collection.
     </p>
    </li>
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      Similarly, for
      <code class="leafygreen-ui-1l06pbn">
       $unionWith
      </code>
      , include the
      <code class="leafygreen-ui-1l06pbn">
       coll
      </code>
      field and specify
the collection.
     </p>
    </li>
   </ul>
   <p class="leafygreen-ui-1kp3ins">
    The following scenario shows an example.
   </p>
   <p class="leafygreen-ui-1kp3ins">
    Create a collection
    <code class="leafygreen-ui-1l06pbn">
     cakeFlavors
    </code>
    :
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.cakeFlavors.insertMany( [\n   { _id: 1, flavor: \"chocolate\" },\n   { _id: 2, flavor: \"strawberry\" },\n   { _id: 3, flavor: \"cherry\" }\n] )","programmingLanguage":"JavaScript"}
   </script>
   <div class="css-16lmvu9">
    <div class="leafygreen-ui-196mwvg">
     <div class="leafygreen-ui-yzcs8i">
      <pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">cakeFlavors</span>.<span class="lg-highlight-title lg-highlight-function">insertMany</span>( <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">flavor</span>: <span class="lg-highlight-string">"chocolate"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-attr">flavor</span>: <span class="lg-highlight-string">"strawberry"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-attr">flavor</span>: <span class="lg-highlight-string">"cherry"</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">
    Starting in MongoDB 8.0, the following example returns an error because
it contains an invalid
    <code class="leafygreen-ui-1l06pbn">
     coll
    </code>
    field:
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.cakeFlavors.aggregate( [ {\n   $unionWith: {\n      coll: \"cakeFlavors\",\n      pipeline: [ { $documents: [] } ] }\n} ] )","programmingLanguage":"JavaScript"}
   </script>
   <div class="css-16lmvu9">
    <div class="leafygreen-ui-196mwvg">
     <div class="leafygreen-ui-yzcs8i">
      <pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">cakeFlavors</span>.<span class="lg-highlight-title lg-highlight-function">aggregate</span>( <!-- -->[ <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <span class="lg-highlight-attr">$unionWith</span>: <!-- -->{</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx">      <span class="lg-highlight-attr">coll</span>: <span class="lg-highlight-string">"cakeFlavors"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <span class="lg-highlight-attr">pipeline</span>: <!-- -->[ <!-- -->{ <span class="lg-highlight-attr">$documents</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">
    In MongoDB versions before 8.0, the previous example runs.
   </p>
   <p class="leafygreen-ui-1kp3ins">
    For an example with a valid
    <code class="leafygreen-ui-1l06pbn">
     coll
    </code>
    field, see
    <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-unionWith-duplicate-results" target="_self">
     <span>
      Duplicate Results
      <!-- -->
      .
     </span>
    </a>
   </p>
  </section>
 </section>
</section>
