On this page本页内容
$unionWith
¶New in version 4.4.版本4.4中的新功能。
Performs a union of two collections; i.e. 执行两个集合的并集;亦即$unionWith
combines pipeline results from two collections into a single result set. $unionWith
将来自两个集合的管道结果合并到一个结果集中。The stage outputs the combined result set (including duplicates) to the next stage.该阶段将组合结果集(包括副本)输出到下一阶段。
The $unionWith
stage has the following syntax:$unionWith
阶段语法如下所示:
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:$unionWith
阶段接收包含以下字段的文档:
coll |
|
pipeline |
|
The $unionWith
operation would correspond to the following SQL statement:$unionWith
操作将对应于以下SQL语句:
The combined results from the previous stage and the 前一阶段和$unionWith
stage can include duplicates.$unionWith
阶段的合并结果可以包含重复结果。
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:suppliers
集合的state
字段投影的结果与来自warehouse
集合的state
字段投影的结果相结合:
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:$group
阶段以按state
字段分组:
The result set no longer contains duplicates:结果集不再包含重复项:
$unionWith
If the 如果$unionWith
stage is part of the $lookup pipeline, the $unionWith
coll cannot be sharded. $unionWith
阶段是$lookup
管道的一部分,则$unionWith
coll不能被分片。For example, in the following aggregation operation, the 例如,在以下聚合操作中,inventory_q1
collection cannot be sharded:inventory_q1
集合不能分片:
If the 如果db.collection.aggregate()
includes a collation, that collation is used for the operation, ignoring any other collations.db.collection.aggregate()
包含排序规则,则该排序规则用于操作,忽略任何其他排序规则。
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:db.collection.aggregate()
不包含排序规则,db.collection.aggregate()
方法将使用运行db.collection.aggregate()
的顶级集合/视图的排序规则:
$unionWith
coll是一个集合,则忽略其排序规则。$unionWith
coll是一个视图,那么它的排序规则必须与顶级集合/视图的排序规则相匹配。$unionWith inside transactions.$unionWith 用于事务内部。 | |
$unionWith stage is part of the $lookup pipeline, the $unionWith coll cannot be sharded.$unionWith 阶段是$lookup管道的一部分,则$unionWith coll不能被切分。 | |
$out |
$out stage.$unionWith 管道不能包括$out 阶段。 |
$merge |
$merge stage.$unionWith 管道不能包含$merge 阶段。 |
Create a sample 使用以下文档创建sales2019q1
collection with the following documents:sales2019q1
样本集合:
Create a sample 使用以下文档创建sales2019q2
collection with the following documents:sales2019q2
样本集合:
Create a sample 使用以下文档创建sales2019q3
collection with the following documents:sales2019q3
样本集合:
Create a sample 使用以下文档创建sales2019q4
collection with the following documents:sales2019q4
样本集合:
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:$unionWith
合并所有四个集合中的文档,以创建年度销售报告,按季度和门店列出所有销售额:
Specifically, the aggregation pipeline uses:具体而言,聚合管道使用:
$set
stage to update the _id
field to contain the quarter. $set
阶段更新_id
字段以包含季度。$unionWith
stages to combine all documents from the four collections; each also using the $set
stage on its documents. $unionWith
阶段的序列,用于合并四个集合中的所有文档;每个文档都在文档中使用$set
阶段。$sort
stage to sort by the _id
(i.e. the quarter), the store
, and item
.$sort
阶段,按_id
(即季度)、store
和item
进行排序。
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:$unionWith
组合来自所有四个集合的文档,以创建年度销售报告,其中列出了每个项目的年度销售数量:
$unionWith
stages retrieve documents from the specified collections into the pipeline:$unionWith
阶段将指定集合中的文档检索到管道中的顺序:
$group
stage groups by the item
field and uses $sum
to calculate the yearly total sales quantity per item
:$group
阶段按item
字段分组,并使用$sum
计算每个item
的年度总销售数量:
$sort
stage orders the documents by descending total
.$sort
阶段按total
降序排列文档。
Alternatively, you could specify the 或者,您可以在每个$group
stage within each $unionWith
stage:$unionWith
阶段中指定$group
阶段:
$group
groups the 2019q1 sales totals by items:$group
按项目对2019年第一季度的销售总额进行分组:
$unionWith
stages groups the sales total by the items from the specified collections into the pipeline:$unionWith
阶段的顺序将指定系列中的商品的销售总额分组到管道中:
$group
stage groups these quarterly groupings:$group
阶段将这些季度分组:
$sort
stage orders the documents by descending total
.$sort
阶段按total
降序排列文档。