On this page本页内容
$out
¶Takes the documents returned by the aggregation pipeline and writes them to a specified collection. 获取聚合管道返回的文档并将其写入指定集合。Starting in MongoDB 4.4, you can specify the output database.从MongoDB 4.4开始,可以指定输出数据库。
The $out
stage must be the last stage in the pipeline. $out
阶段必须是管道中的最后一个阶段。The $out
operator lets the aggregation framework return result sets of any size.$out
运算符允许聚合框架返回任意大小的结果集。
The $out
stage has the following syntax:$out
阶段语法如下所示:
$out
can take a document to specify the output database as well as the output collection:$out
可以获取一个文档来指定输出数据库和输出集合:
db |
|
coll |
|
$out
can take a string to specify only the output collection (i.e. output to a collection in the same database):$out
可以使用字符串仅指定输出集合(即输出到同一数据库中的集合):
Important重要
$merge
(Available starting in MongoDB 4.2).$merge
(从MongoDB 4.2开始提供)。$out
operator cannot write results to a capped collection.$out
运算符无法将结果写入封顶集合。$merge
$merge
的比较¶$merge
and $out
Comparison$merge
和$out
比较
With the introduction of 在版本4.2中引入$merge
in version 4.2, MongoDB provides two stages, $merge
and $out
, for writing the results of the aggregation pipeline to a collection. $merge
后,MongoDB提供了两个阶段,$merge
和$out
,用于将聚合管道的结果写入集合。The following summarizes the capabilities of the two stages:以下总结了两个阶段的功能:
$out | $merge |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
The $out
operation creates a new collection if one does not already exist.$out
操作将创建一个新集合(如果尚未存在)。
The collection is not visible until the aggregation completes. 在聚合完成之前,集合不可见。If the aggregation fails, MongoDB does not create the collection.如果聚合失败,MongoDB不会创建集合。
If the collection specified by the 如果$out
operation already exists, then upon completion of the aggregation, the $out
stage atomically replaces the existing collection with the new results collection. $out
操作指定的集合已经存在,则在聚合完成后,$out
阶段会以原子方式将现有集合替换为新的结果集合。Specifically, the 具体而言,$out
operation:$out
操作:
renameCollection
command with dropTarget: true
to rename the temp collection to the destination collection.true
调用renameCollection
命令以及dropTarget: true
,将临时集合重命名为目标集合。The $out
operation does not change any indexes that existed on the previous collection. $out
操作不会更改上一个集合上存在的任何索引。If the aggregation fails, the 如果聚合失败,$out
operation makes no changes to the pre-existing collection.$out
操作不会对预先存在的集合进行任何更改。
The pipeline will fail to complete if the documents produced by the pipeline would violate any unique indexes, including the index on the 如果管道生成的文档违反任何唯一索引,包括原始输出集合的_id
field of the original output collection._id
字段上的索引,管道将无法完成。
majority
Starting in MongoDB 4.2, you can specify read concern level 从MongoDB 4.2开始,您可以为包含"majority"
for an aggregation that includes an $out
stage.$out
阶段的聚合指定读取关注级别"majority"
。
In MongoDB 4.0 and earlier, you cannot include the 在MongoDB 4.0及更早版本中,您不能包含$out
stage to use "majority"
read concern for the aggregation.$out
阶段来使用聚合的"majority"
读取关注点。
mongodump
mongodump
的交互¶A 如果客户端在转储过程中发出包含mongodump
started with --oplog
fails if a client issues an aggregation pipeline that includes $out
during the dump process. $out
的聚合管道,则以--oplog
开头的mongodump
将失败。See 有关更多信息,请参阅mongodump --oplog
for more information.mongodump --oplog
。
$out inside transactions.$out 。 | |
$out stage is not allowed as part of a view definition. $out 阶段不允许作为视图定义的一部分。$lookup or $facet stage), this $out stage restriction applies to the nested pipelines as well.$lookup 或$facet 阶段),则此$out 阶段限制也适用于嵌套管道。 | |
$lookup stage |
$out stage in the $lookup stage’s nested pipeline.$lookup 阶段的嵌套管道中包含$out 阶段。 |
$facet stage |
$facet stage’s nested pipeline cannot include the $out stage.$facet 阶段的嵌套管道不能包括$out 阶段。 |
$unionWith |
$unionWith stage’s nested pipeline cannot include the $out stage.$unionWith 阶段的嵌套管道不能包括$out 阶段。 |
"linearizable" read concern |
$out stage cannot be used in conjunction with read concern "linearizable" . $out 阶段不能与读关注点"linearizable" 结合使用。"linearizable" read concern for db.collection.aggregate() , you cannot include the $out stage in the pipeline.db.collection.aggregate() 的"linearizable" 读取关注点,就不能在管道中包含$out 阶段。 |
In the 在test
database, create a collection books
with the following documents:test
数据库中,创建包含以下文档的books
集合:
If the 如果test
database does not already exist, the insert operation creates the database as well as the books
collection.test
数据库不存在,则insert
操作将创建数据库以及books
集合。
The following aggregation operation pivots the data in the 下面的聚合操作将测试数据库中books
collection in the test
database to have titles grouped by authors and then writes the results to the authors
collection, also in the test
database.books
集合中的数据转为按作者分组的标题,然后将结果写入test
数据库中的authors
集合。
$group
):The $group
stage groups by the authors
and uses $push
to add the titles to a books
array field:$group
阶段由author
分组,并使用$push
将标题添加到books
数组字段:
$out
):$out
stage outputs the documents to the authors
collection in the test
database.$out
阶段将文档输出到test
数据库中的authors
集合。To view the documents in the output collection, run the following operation:要查看输出集合中的文档,请运行以下操作:
The collection contains the following documents:该集合包含以下文档:
Note
For a replica set or a standalone, if the output database does not exist, 对于副本集或单机版,如果输出数据库不存在,$out
also creates the database.$out
也会创建数据库。
For a sharded cluster, the specified output database must already exist.对于分片群集,指定的输出数据库必须已经存在。
Starting in MongoDB 4.4, 从MongoDB 4.4开始,$out
can output to a collection in a database different from where the aggregation is run.$out
可以输出到不同于运行聚合的数据库中的集合。
The following aggregation operation pivots the data in the 以下聚合操作将books
collection to have titles grouped by authors and then writes the results to the authors
collection in the reporting
database:books
集合中的数据转为按作者分组的标题,然后将结果写入reporting
数据库中的authors
集合:
$group
):The $group
stage groups by the authors
and uses $push
to add the titles to a books
array field:$group
阶段按author
分组,并使用$push
将标题添加到authors
数组字段:
$out
):$out
stage outputs the documents to the authors
collection in the reporting
database.$out
阶段将文档输出到reporting
数据库中的authors
集合。To view the documents in the output collection, run the following operation:要查看输出集合中的文档,请运行以下操作:
The collection contains the following documents:该集合包含以下文档: