- Reference >
- Operators >
- Aggregation Pipeline Stages >
- $unionWith (aggregation)
$unionWith (aggregation)¶
On this page
Definition¶
-
$unionWith
¶ New in version 4.4.
Performs a union of two collections; i.e.
$unionWith
combines pipeline results from two collections into a single result set. The stage outputs the combined result set (including duplicates) to the next stage.
Syntax¶
The $unionWith
stage has the following syntax:
To include all documents from the specified collection without any processing, you can use the simplified form:
The $unionWith
stage takes a document with the following
fields:
Field | Description |
---|---|
coll | The collection or view whose pipeline results you wish to include in the result set. |
pipeline | Optional. An aggregation pipeline to apply to the specified coll.
|
The $unionWith
operation would correspond to the following
SQL statement:
Considerations¶
Duplicates Results¶
The combined results from the previous stage and the
$unionWith
stage can include duplicates.
For example, create a suppliers collection:
The following aggregation which combines the results from the state
field projection from the suppliers
collection with the results
from the state
field projection from the warehouse
collection:
As can be seen from the returned documents, the result set contains duplicates:
To remove the duplicates, you can include a $group
stage to
group by the state
field:
The result set no longer contains duplicates:
$unionWith
a Sharded Collection¶
If the $unionWith
stage is part of the $lookup
pipeline, the $unionWith
coll cannot be sharded. For example, in the following
aggregation operation, the inventory_q1
collection cannot be
sharded:
Collation¶
If the db.collection.aggregate()
includes a collation, that collation is used for
the operation, ignoring any other collations.
If the db.collection.aggregate()
does not include a
collation, the
db.collection.aggregate()
method uses the collation for the
top-level collection/view on which the
db.collection.aggregate()
is run:
- If the $unionWith coll is a collection, its collation is ignored.
- If the $unionWith coll is a view, then its collation must match that of the top-level collection/view. Otherwise, the operation errors.
Restrictions¶
Restrictions | Description |
---|---|
Transactions | An aggregation pipeline cannot use $unionWith inside
transactions. |
Sharded Collection | If the $unionWith stage is part of the $lookup
pipeline, the $unionWith coll cannot be sharded. |
$out |
The $unionWith pipeline cannot
include the $out stage. |
$merge |
The $unionWith pipeline cannot
include the $merge stage. |
Examples¶
Create a Yearly Report from the Union of Quarterly Data Collections¶
Create a sample sales2019q1
collection with the following documents:
Create a sample sales2019q2
collection with the following documents:
Create a sample sales2019q3
collection with the following documents:
Create a sample sales2019q4
collection with the following documents:
Report 1: All Sales by Quarter and Stores and Items¶
The following aggregation uses $unionWith
to combine
documents from all four collections to create a yearly sales report
that lists all sales by quarter and stores:
Specifically, the aggregation pipeline uses:
a
$set
stage to update the_id
field to contain the quarter. That is, the documents from this stage has the form:a sequence of
$unionWith
stages to combine all documents from the four collections; each also using the$set
stage on its documents. That is, the documents are from all four collections and have the form:a
$sort
stage to sort by the_id
(i.e. the quarter), thestore
, anditem
.
Report 2: Aggregated Yearly Sales by Items¶
The following aggregation uses $unionWith
to combine
documents from all four collections to create a yearly sales report
that lists the yearly sales quantity per item:
The sequence of
$unionWith
stages retrieve documents from the specified collections into the pipeline:The
$group
stage groups by theitem
field and uses$sum
to calculate the yearly total sales quantity peritem
:The
$sort
stage orders the documents by descendingtotal
.
Alternatively, you could specify the $group
stage within
each $unionWith
stage:
The first
$group
groups the 2019q1 sales totals by items:The sequence of
$unionWith
stages groups the sales total by the items from the specified collections into the pipeline:The last
$group
stage groups these quarterly groupings:The
$sort
stage orders the documents by descendingtotal
.