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:结果集不再包含重复项:
$unionWithIf the 如果$unionWith stage is part of the $lookup pipeline, the $unionWith coll cannot be sharded. $unionWith阶段是$lookup管道的一部分,则$unionWithcoll不能被分片。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降序排列文档。