There is UNION command in relational databases, e.g. in Microsoft SQL.
For Microsoft SQL, UNION states
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.
Is there any analog of UNION in MongoDB.
The task I'd like to solve is to union documents from two collections - Accounts and Packages.
Accounts
{ "_id": 1, "accountId": 1, "accOld": "oldValue1", "accNew": "newValue1" },
{ "_id": 2, "accountId": 1, "accOld": "oldValue2", "accNew": "newValue2" },
{ "_id": 3, "accountId": 2, "accOld": "oldValue1", "accNew": "newValue1" }
Packages
{ "_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" }
As the result "row-set" I'd like to have the following one sorted by logically grouped by accountId
field
{ "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" }
P.S.: I've read the following questions, but haven't found it helpful
MongoDB: Combine data from multiple collections into one..how?
Merging two collections in MongoDB
MongoDB and “joins” [duplicate]