title: UNION analogue in MongoDB
body: <p>There is UNION command in relational databases, e.g. in Microsoft SQL.<br>
  For Microsoft SQL, UNION states  </p>
  
  <blockquote>
    <p>Combines the results of two or more queries into a single result set
    that includes all the rows that belong to all queries in the union.
    The UNION operation is different from using joins that combine columns
    from two tables.</p>
  </blockquote>
  
  <p>Is there any analog of UNION in MongoDB.</p>
  
  <p>The task I'd like to solve is to union documents from two collections - Accounts and Packages.  </p>
  
  <p><strong>Accounts</strong></p>
  
  <pre><code>{ "_id": 1, "accountId": 1, "accOld": "oldValue1", "accNew": "newValue1" },  
  { "_id": 2, "accountId": 1, "accOld": "oldValue2", "accNew": "newValue2" },  
  { "_id": 3, "accountId": 2, "accOld": "oldValue1", "accNew": "newValue1" }
  </code></pre>
  
  <p><strong>Packages</strong></p>
  
  <pre><code>{ "_id": 1, "accountId": 1, "pckgOld": "packageOldValue1", "pckgNew": "packageNewValue1" },  
  { "_id": 2, "accountId": 1, "pckgOld": "packageOldValue2", "pckgOld": "packageNewValue2" },  
  { "_id": 3, "accountId": 2, "pckgOld": "packageOldValue3", "pckgOld": "packageNewValue4" },  
  { "_id": 4, "accountId": 3, "pckgOld": "packageOldValue4", "pckgOld": "packageNewValue4" }
  </code></pre>
  
  <p>As the result "row-set" I'd like to have the following one sorted by logically grouped by <code>accountId</code> field  </p>
  
  <pre><code>{ "accountId": 1, "accOld": "oldValue1", "accNew": "newValue1" },  
  { "accountId": 1, "accOld": "oldValue2", "accNew": "newValue2" },  
  { "accountId": 1, "pckgOld": "packageOldValue1", "pckgNew": "packageNewValue1" },  
  { "accountId": 1, "pckgOld": "packageOldValue2", "pckgOld": "packageNewValue2" },  
  { "accountId": 2, "accOld": "oldValue1", "accNew": "newValue1" }
  { "accountId": 2, "pckgOld": "packageOldValue3", "pckgOld": "packageNewValue4" },  
  { "accountId": 3, "pckgOld": "packageOldValue4", "pckgOld": "packageNewValue4" }
  </code></pre>
  
  <p>P.S.: I've read the following questions, but haven't found it helpful<br>
  <a href="https://stackoverflow.com/questions/5681851/mongodb-combine-data-from-multiple-collections-into-one-how">MongoDB: Combine data from multiple collections into one..how?</a><br>
  <a href="https://stackoverflow.com/questions/9696940/merging-two-collections-in-mongodb">Merging two collections in MongoDB
  </a><br>
  <a href="https://stackoverflow.com/questions/4067197/mongodb-and-joins">MongoDB and “joins” [duplicate]</a></p>