$out (aggregation)
On this page本页内容
Definition定义
$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
运算符允许聚合框架返回任何大小的结果集。Warning$out
replaces the specified collection if it exists.替换指定的集合(如果存在)。See Replace Existing Collection for details.有关详细信息,请参阅替换现有集合。
Syntax语法
The $out
stage has the following syntax:$out
阶段具有以下语法:
Starting in MongoDB 4.4,从MongoDB 4.4开始,$out
can take a document to specify the output database as well as the output collection:$out
可以使用一个文档来指定输出数据库和输出集合:{ $out: { db: "<output-db>", coll: "<output-collection>" } }
Field字段Description描述db
The output database name.输出数据库名称。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.对于分片集群,指定的输出数据库必须已经存在。
coll
The output collection name.输出集合名称。$out
can take a string to specify only the output collection (i.e. output to a collection in the same database):可以使用字符串仅指定输出集合(即输出到同一数据库中的集合):{ $out: "<output-collection>" } // Output collection is in the same database
You cannot specify a sharded collection as the output collection.不能将分片集合指定为输出集合。The input collection for a pipeline can be sharded.可以对管道的输入集合进行分片。To output to a sharded collection, see要输出到分片集合,请参阅$merge
(Available starting in MongoDB 4.2).$merge
(从MongoDB 4.2开始提供)。The$out
operator cannot write results to a capped collection.$out
运算符无法将结果写入封顶集合。If you modify a collection with an Atlas Search index, you must first delete and then re-create the search index. Consider using如果使用Atlas Search索引修改集合,则必须先删除搜索索引,然后重新创建。请考虑改用$merge
instead.$merge
。
Comparison with $merge
与$merge
的比较
$merge
With the introduction of 随着$merge
in version 4.2, MongoDB provides two stages, $merge
and $out
, for writing the results of the aggregation pipeline to a collection. $merge
在4.2版本中的引入,MongoDB提供了两个阶段,$merge
和$out
,用于将聚合管道的结果写入集合。The following summarizes the capabilities of the two stages:以下总结了两个阶段的功能:
$out | $merge |
---|---|
|
|
|
|
|
|
|
|
|
|
Behaviors行为
$out Read Operations Run on Secondary Replica Set Members在辅助副本集成员上运行$out
读取操作
Starting in MongoDB 5.0, 从MongoDB 5.0开始,如果集群中的所有节点都将featureCompatibilityVersion设置为$out
can run on replica set secondary nodes if all the nodes in cluster have featureCompatibilityVersion set to 5.0
or higher and the Read Preference is set to secondary.5.0
或更高版本,并且读取首选项设置为次要,$out
可以在副本集次要节点上运行。
Read operations of the $out
statement occur on the secondary nodes, while the write operations occur only on the primary nodes.$out
语句的读取操作发生在辅助节点上,而写入操作仅发生在主节点上。
Not all driver versions support targeting of 并非所有驱动程序版本都支持将$out
operations to replica set secondary nodes. $out
操作定向到副本集辅助节点。Check your driver documentation to see when your driver added support for 查看驱动程序文档,了解驱动程序何时添加了对在辅助设备上运行$out
running on a secondary.$out
的支持。
Create New Collection创建新集合
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不会创建集合。
Replace Existing Collection替换现有集合
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
操作:
Creates a temp collection.创建临时集合。Copies the indexes from the existing collection to the temp collection.将索引从现有集合复制到临时集合。Inserts the documents into the temp collection.将文档插入临时集合中。Calls the使用renameCollection
command withdropTarget: true
to rename the temp collection to the destination collection.dropTarget:true
调用renameCollection
命令,将临时集合重命名为目标集合。
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
操作不会对预先存在的集合进行任何更改。
Index Constraints索引约束
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
字段上的索引,则管道将无法完成。
If the 如果$out
operation modifies a collection with an Atlas Search index, you must delete and re-create the search index. Consider using $merge
instead.$out
操作使用Atlas Search索引修改集合,则必须删除并重新创建搜索索引。请考虑改用$merge
。
majority
Read Concern读取关注
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"
。
Interaction with mongodump
与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
。
Restrictions限制
$out inside transactions.$out 。 | |
$out to output to a time series collection.$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 stage限制也适用于嵌套管道。 | |
$lookup | $out stage in the $lookup stage's nested pipeline.$out 阶段包括在$lookup 阶段的嵌套管道中。 |
$facet | $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" | $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 阶段。 |
Examples实例
In the 在test
database, create a collection books
with the following documents:test
数据库中,创建一个包含以下文档的集合books
:
db.getSiblingDB("test").books.insertMany([
{ "_id" : 8751, "title" : "The Banquet", "author" : "Dante", "copies" : 2 },
{ "_id" : 8752, "title" : "Divine Comedy", "author" : "Dante", "copies" : 1 },
{ "_id" : 8645, "title" : "Eclogues", "author" : "Dante", "copies" : 2 },
{ "_id" : 7000, "title" : "The Odyssey", "author" : "Homer", "copies" : 10 },
{ "_id" : 7020, "title" : "Iliad", "author" : "Homer", "copies" : 10 }
])
If the 如果test
database does not already exist, the insert operation creates the database as well as the books
collection.test
数据库还不存在,则插入操作将创建数据库以及books
集合。
Output to Same Database输出到同一数据库
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.test
数据库中books
集合中的数据调整为按作者分组的标题,然后将结果写入test
数据库中的authors
集合。
db.getSiblingDB("test").books.aggregate( [
{ $group : { _id : "$author", books: { $push: "$title" } } },
{ $out : "authors" }
] )
First Stage (第一阶段 ($group
):$group
):-
The$group
stage groups by theauthors
and uses$push
to add the titles to abooks
array field:$group
阶段按authors
(作者)分组,并使用$push
将标题添加到books
数组字段:{ "_id" : "Dante", "books" : [ "The Banquet", "Divine Comedy", "Eclogues" ] }
{ "_id" : "Homer", "books" : [ "The Odyssey", "Iliad" ] } Second Stage (第二阶段 ($out
):$out
):The$out
stage outputs the documents to theauthors
collection in thetest
database.$out
阶段将文档输出到test
数据库中的authors
集合。
To view the documents in the output collection, run the following operation:若要查看输出集合中的文档,请运行以下操作:
db.getSiblingDB("test").authors.find()
The collection contains the following documents:该集合包含以下文档:
{ "_id" : "Homer", "books" : [ "The Odyssey", "Iliad" ] }
{ "_id" : "Dante", "books" : [ "The Banquet", "Divine Comedy", "Eclogues" ] }
Output to a Different Database输出到其他数据库
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
集合:
db.getSiblingDB("test").books.aggregate( [
{ $group : { _id : "$author", books: { $push: "$title" } } },
{ $out : { db: "reporting", coll: "authors" } }
] )
First Stage (第一阶段 ($group
):$group
):-
The$group
stage groups by theauthors
and uses$push
to add the titles to abooks
array field:$group
阶段按authors
分组,并使用$push
将标题添加到books
数组字段:{ "_id" : "Dante", "books" : [ "The Banquet", "Divine Comedy", "Eclogues" ] }
{ "_id" : "Homer", "books" : [ "The Odyssey", "Iliad" ] } Second Stage (第二阶段 ($out
):$out
):The$out
stage outputs the documents to theauthors
collection in thereporting
database.$out
阶段将文档输出到reporting
数据库中的authors
集合。
To view the documents in the output collection, run the following operation:若要查看输出集合中的文档,请运行以下操作:
db.getSiblingDB("reporting").authors.find()
The collection contains the following documents:该集合包含以下文档:
{ "_id" : "Homer", "books" : [ "The Odyssey", "Iliad" ] }
{ "_id" : "Dante", "books" : [ "The Banquet", "Divine Comedy", "Eclogues" ] }