<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Akash Singh</title>
    <description>The latest articles on DEV Community by Akash Singh (@akashsingh-dev).</description>
    <link>https://dev.to/akashsingh-dev</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3981716%2F722b57c0-2068-444a-a6ca-3c1fd78cc830.jpg</url>
      <title>DEV Community: Akash Singh</title>
      <link>https://dev.to/akashsingh-dev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/akashsingh-dev"/>
    <language>en</language>
    <item>
      <title>How I Merged Two MongoDB Collections Using $unionWith</title>
      <dc:creator>Akash Singh</dc:creator>
      <pubDate>Sat, 13 Jun 2026 06:04:16 +0000</pubDate>
      <link>https://dev.to/akashsingh-dev/how-i-merged-two-mongodb-collections-using-unionwith-2mj9</link>
      <guid>https://dev.to/akashsingh-dev/how-i-merged-two-mongodb-collections-using-unionwith-2mj9</guid>
      <description>&lt;h1&gt;
  
  
  How I Merged Two MongoDB Collections Using &lt;code&gt;$unionWith&lt;/code&gt;
&lt;/h1&gt;

&lt;p&gt;While working on a doctor listing feature, I encountered an issue where some doctors were missing from the All Doctors page.&lt;/p&gt;

&lt;p&gt;After investigating the issue, I found that data was being fetched from two separate collections, and pagination was being applied independently. This resulted in inconsistent results and unnecessary complexity.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;I had two collections:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;ReferralUser&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;user_managements&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Fetching data separately caused:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Multiple API calls&lt;/li&gt;
&lt;li&gt;Missing records&lt;/li&gt;
&lt;li&gt;Inconsistent pagination&lt;/li&gt;
&lt;li&gt;Additional frontend logic&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The Solution
&lt;/h2&gt;

&lt;p&gt;MongoDB's &lt;code&gt;$unionWith&lt;/code&gt; aggregation stage allowed me to combine both collections into a single pipeline.&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
js
`db.ReferralUser.aggregate([
  {
    $unionWith: {
      coll: "user_managements"
    }
  }
]);`
---

Benefits
Reduced multiple API calls into one
Improved pagination consistency
Simplified frontend logic
Easier to maintain
Key Takeaways

When working with similar data across multiple collections, $unionWith can help simplify the API and provide more consistent results.

Thanks for reading! 🚀
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>mongodb</category>
      <category>node</category>
      <category>backend</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
