Database Manual / Reference / Query Language / Aggregation Stages

$out (aggregation stage)(聚合阶段)

Definition定义

$out

Takes the documents returned by the aggregation pipeline and writes them to a specified collection. You can specify the output database.获取聚合管道返回的文档并将其写入指定的集合。您可以指定输出数据库。

The $out stage must be the last stage in the pipeline. The $out operator lets the aggregation framework return result sets of any size.$out阶段必须是管道中的最后一个阶段$out运算符允许聚合框架返回任何大小的结果集。

Warning

If the collection specified by the $out operation already exists, then the $out stage atomically replaces the existing collection with the new results collection upon completion of the aggregation. 如果$out操作指定的集合已经存在,则$out阶段会在聚合完成后用新的结果集合原子性地替换现有集合。See Replace Existing Collection for details.有关详细信息,请参阅替换现有集合

Syntax语法

The $out stage has the following syntax:$out阶段具有以下语法:

  • $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
  • $out can take a document to specify the output database as well as the output collection:可以使用文档来指定输出数据库和输出集合:

    { $out: { db: "<output-db>", coll: "<output-collection>" } }
  • Starting in MongoDB 7.0.3 and 7.1, $out can take a document to output to a time series collection:从MongoDB 7.0.3和7.1开始,$out可以将文档输出到时间序列集合

    { $out:
    { db: "<output-db>", coll: "<output-collection>",
    timeseries: {
    timeField: "<field-name>",
    metaField: "<field-name>",
    granularity: "seconds" || "minutes" || "hours" ,
    }
    }
    }

    Important

    Changing Time Series Granularity改变时间序列粒度

    After creating a time series collection, you can modify its granularity using the collMod method. However, you can only increase the timespan covered by each bucket. You cannot decrease it.创建时间序列集合后,可以使用collMod方法修改其粒度。但是,您只能增加每个桶覆盖的时间跨度。你不能减少它。

    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也会创建数据库。

    coll

    The output collection name.输出集合名称。

    timeseries

    A document that specifies the configuration to use when writing to a time series collection. The timeField is required. All other fields are optional.指定写入时间序列集合时使用的配置的文档。timeField是必需的。所有其他字段都是可选的。

    timeField

    Required when writing to a time series collection. The name of the field which contains the date in each time series document. Documents in a time series collection must have a valid BSON date as the value for the timeField.写入时间序列集合时需要。每个时间序列文档中包含日期的字段的名称。时间序列集合中的文档必须具有有效的BSON日期作为timeField的值。

    metaField

    Optional. 可选。The name of the field which contains metadata in each time series document. The metadata in the specified field should be data that is used to label a unique series of documents. 每个时间序列文档中包含元数据的字段的名称。指定字段中的元数据应该是用于标记一系列唯一文档的数据。The metadata should rarely, if ever, change The name of the specified field may not be _id or the same as the timeseries.timeField. The field can be of any data type.元数据应该很少(如果有的话)更改。指定字段的名称可能不是_id或与timeseries.timeField不同。该字段可以是任何数据类型。

    Although the metaField field is optional, using metadata can improve query optimization. 虽然metaField字段是可选的,但使用元数据可以改善查询优化。For example, MongoDB automatically creates a compound index on the metaField and timeField fields for new collections. 例如,MongoDB会自动在metaFieldtimeField字段上为新集合创建复合索引If you do not provide a value for this field, the data is bucketed solely based on time.如果不为此字段提供值,则数据将仅基于时间进行分组。

    granularity

    Optional. 可选。Do not use if setting bucketRoundingSeconds and bucketMaxSpanSeconds.如果设置bucketRoundingSecondsbucketMaxSpanSeconds,则不要使用。

    Possible values are seconds (default), minutes, and hours.可能的值是seconds(默认值)、minuteshours

    Set granularity to the value that most closely matches the time between consecutive incoming timestamps. This improves performance by optimizing how MongoDB stores data in the collection.granularity设置为与连续传入时间戳之间的时间最接近的值。这通过优化MongoDB在集合中存储数据的方式来提高性能。

    For more information on granularity and bucket intervals, see Set Granularity for Time Series Data.有关粒度和桶间隔的更多信息,请参阅设置时间序列数据的粒度

    bucketMaxSpanSeconds

    Optional. 可选。Use with bucketRoundingSeconds as an alternative to granularity. 使用bucketRoundingSeconds作为granularity的替代方案。Sets the maximum time between timestamps in the same bucket.设置同一桶中时间戳之间的最大时间间隔。

    Possible values are 1-31536000.可能的值为1-31536000。

    New in version 6.3.在版本6.3中新增。

    bucketRoundingSeconds

    Optional. 可选。Use with bucketMaxSpanSeconds as an alternative to granularity. Must be equal to bucketMaxSpanSeconds.使用bucketMaxSpanSeconds作为粒度的替代方案。必须等于bucketMaxSpanSeconds

    When a document requires a new bucket, MongoDB rounds down the document's timestamp value by this interval to set the minimum time for the bucket.当文档需要新的桶时,MongoDB会按此间隔对文档的时间戳值进行四舍五入,以设置桶的最小时间。

    New in version 6.3.在版本6.3中新增。

Important

  • 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.不能将分片集合指定为输出集合。管道的输入集合可以分片。要输出到分片集合,请参阅$merge
  • The $out operator cannot write results to a capped collection.$out运算符无法将结果写入封顶集合。
  • If you modify a collection with a MongoDB Search index, you must first delete and then re-create the search index. 如果使用MongoDB搜索索引修改集合,则必须先删除,然后重新创建搜索索引。Consider using $merge instead.考虑改用$merge

Comparison with $merge$merge的比较

MongoDB provides two stages, $merge and $out, for writing the results of the aggregation pipeline to a collection. The following summarizes the capabilities of the two stages:MongoDB提供了两个阶段,$merge$out,用于将聚合管道的结果写入集合。以下总结了这两个阶段的能力:

$out$merge
  • Can output to a collection in the same or different database.可以输出到相同或不同数据库中的集合。
  • Can output to a collection in the same or different database.可以输出到相同或不同数据库中的集合。
  • Creates a new collection if the output collection does not already exist.如果输出集合不存在,则创建新集合。
  • Creates a new collection if the output collection does not already exist.如果输出集合不存在,则创建新集合。
  • Replaces the output collection completely if it already exists.如果输出集合已存在,则完全替换它。
  • Can incorporate results (insert new documents, merge documents, replace documents, keep existing documents, fail the operation, process documents with a custom update pipeline) into an existing collection.可以将结果(插入新文档、合并文档、替换文档、保留现有文档、操作失败、使用自定义更新管道处理文档)合并到现有集合中。

    Can replace the content of the collection but only if the aggregation results contain a match for all existing documents in the collection.可以替换集合的内容,但前提是聚合结果包含集合中所有现有文档的匹配项。

  • Cannot output to a sharded collection. Input collection, however, can be sharded.无法输出到分片集合。然而,输入集合可以分片。
  • Can output to a sharded collection. Input collection can also be sharded.可以输出到分片集合。输入集合也可以分片。
  • Starting in MongoDB 7.0.3 and 7.1, can output to a time series collection.从MongoDB 7.0.3和7.1开始,可以输出到时间序列集合。
  • Cannot output to a time series collection.无法输出到时间序列集合。
  • Corresponds to the SQL statements:对应于SQL语句:

    • INSERT INTO T2 SELECT * FROM T1
    • SELECT * INTO T2 FROM T1
  • Corresponds to the SQL statement:对应于SQL语句:

    • MERGE T2 AS TARGET
      USING (SELECT * FROM T1) AS SOURCE
      ON MATCH (T2.ID = SOURCE.ID)
      WHEN MATCHED THEN
      UPDATE SET TARGET.FIELDX = SOURCE.FIELDY
      WHEN NOT MATCHED THEN
      INSERT (FIELDX)
      VALUES (SOURCE.FIELDY)
    • Create/Refresh Materialized Views创建/刷新物化视图

Behaviors行为

$out Read Operations Run on Secondary Replica Set Members在辅助副本集成员上运行$out读取操作

Starting in MongoDB 5.0, $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.从MongoDB 5.0开始,如果集群中的所有节点都将featureCompatibilityVersion设置为5.0或更高,并且读取首选项设置为secondary,则$out可以在副本集secondary节点上运行。

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. Specifically, the $out operation:如果$out操作指定的集合已经存在,那么在聚合完成后,$out阶段会用新的结果集合原子性地替换现有集合。具体来说,$out操作:

  1. Creates a temp collection.
  2. Copies the indexes from the existing collection to the temp collection.将索引从现有集合复制到临时集合。
  3. Inserts the documents into the temp collection.将文档插入到临时集合中。
  4. Calls the renameCollection command with dropTarget: true to rename the temp collection to the destination collection.使用dropTarget:true调用renameCollection命令,将临时集合重命名为目标集合。

If specified collection exists and the $out operation specifies timeseries options, then the following restrictions apply:如果指定的集合存在,并且$out操作指定了时间序列选项,则适用以下限制:

  1. The existing collection must be a time series collection.现有集合必须是时间序列集合。
  2. The existing collection must not be a view.现有集合不能是视图。
  3. The timeseries options included in the $out stage must exactly match those on the existing collection.$out阶段中包含的timeseries选项必须与现有集合中的选项完全匹配。

The $out operation does not change any indexes that existed on the previous collection. If the aggregation fails, the $out operation makes no changes to the pre-existing collection.$out操作不会更改前一个集合上存在的任何索引。如果聚合失败,$out操作不会对预先存在的集合进行任何更改。

Schema Validation Errors架构验证错误

If your coll collection uses schema validation and has validationAction set to error, inserting an invalid document with $out throws an error. 如果您的coll集合使用模式验证,并且将validationAction设置为error,则使用$out插入无效文档会抛出错误。The $out operation makes no changes to the pre-existing collection and documents returned by the aggregation pipeline are not added to the coll collection.$out操作不会对预先存在的集合进行更改,聚合管道返回的文档也不会添加到coll集合中。

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 a MongoDB Search index, you must delete and re-create the search index. Consider using $merge instead.如果$out操作修改了具有MongoDB搜索索引的集合,则必须删除并重新创建搜索索引。考虑改用$merge

majority Read Concern读取关注

You can specify read concern level "majority" for an aggregation that includes an $out stage.您可以为包含$out阶段的聚合指定读取关注级别"majority"

Interaction with mongodumpmongodump的互动

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限制

Restrictions限制Description描述
transactionsAn aggregation pipeline cannot use $out inside transactions.聚合管道不能在事务中使用$out
view definition视图定义The $out stage is not allowed as part of a view definition. $out阶段不允许作为视图定义的一部分。If the view definition includes nested pipeline (e.g. the view definition includes $lookup or $facet stage), this $out stage restriction applies to the nested pipelines as well.如果视图定义包括嵌套管道(例如,视图定义包括$lookup$facet阶段),则此$out阶段限制也适用于嵌套管道。
$lookup stage阶段You can't include the $out stage in the $lookup stage's nested pipeline.您不能将$out阶段包含在$lookup阶段的嵌套管道中。
$facet stage$facet stage's nested pipeline cannot include the $out stage.阶段的嵌套管道不能包含$out阶段。
$unionWith stage阶段$unionWith stage's nested pipeline cannot include the $out stage.阶段的嵌套管道不能包含$out阶段。
"linearizable" read concern读取关注The $out stage cannot be used in conjunction with read concern "linearizable". $out阶段不能与读取关注"linearizable"结合使用。If you specify "linearizable" read concern for db.collection.aggregate(), you cannot include the $out stage in the pipeline.如果为db.collection.aggregate()指定"linearizable"读取关注,则不能在管道中包含$out阶段。

Examples示例

MongoDB Shell

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集合中的数据进行枢轴转换,使标题按authors(作者)分组,然后将结果写入test数据库中的authors集合。

db.getSiblingDB("test").books.aggregate( [
{ $group : { _id : "$author", books: { $push: "$title" } } },
{ $out : "authors" }
] )
First Stage ($group):第一阶段($group):

The $group stage groups by the authors and uses $push to add the titles to a books 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 the authors collection in the test database.$out阶段将文档输出到测试数据库中的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输出到其他数据库

Note

For a replica set or a standalone, if the output database does not exist, $out also creates the database.对于副本集或独立副本,如果输出数据库不存在,$out也会创建数据库。

$out can output to a collection in a database different from where the aggregation is run.可以输出到与聚合运行位置不同的数据库中的集合。

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集合中的数据进行枢轴转换,使标题按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 the authors and uses $push to add the titles to a books 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 the authors collection in the reporting 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" ] }
C#

The C# examples on this page use the sample_mflix database from the Atlas sample datasets. 本页上的C#示例使用Atlas示例数据集中的sample_mflix数据库。To learn how to create a free MongoDB Atlas cluster and load the sample datasets, see Get Started in the MongoDB .NET/C# Driver documentation.要了解如何创建免费的MongoDB Atlas集群并加载示例数据集,请参阅MongoDB .NET/C#驱动程序文档中的入门

The following Movie class models the documents in the sample_mflix.movies collection:以下Movie类对sample_mflix.movies集合中的文档进行建模:

public class Movie
{
public ObjectId Id { get; set; }

public int Runtime { get; set; }

public string Title { get; set; }

public string Rated { get; set; }

public List<string> Genres { get; set; }

public string Plot { get; set; }

public ImdbData Imdb { get; set; }

public int Year { get; set; }

public int Index { get; set; }

public string[] Comments { get; set; }

[BsonElement("lastupdated")]
public DateTime LastUpdated { get; set; }
}

Note

ConventionPack for Pascal CasePascal大小写的约定包

The C# classes on this page use Pascal case for their property names, but the field names in the MongoDB collection use camel case. To account for this difference, you can use the following code to register a ConventionPack when your application starts:此页面上的C#类使用Pascal大小写作为其属性名,但MongoDB集合中的字段名使用驼峰大小写。为了解释这种差异,您可以在应用程序启动时使用以下代码注册ConventionPack

var camelCaseConvention = new ConventionPack { new CamelCaseElementNameConvention() };
ConventionRegistry.Register("CamelCase", camelCaseConvention, type => true);

To use the MongoDB .NET/C# driver to add a $out stage to an aggregation pipeline, call the Out() method on a PipelineDefinition object.要使用MongoDB .NET/C#驱动程序向聚合管道添加$out阶段,请在PipelineDefinition对象上调用Out()方法。

The following example creates a pipeline stage that writes the results of the pipeline into the movies collection:以下示例创建了一个管道阶段,将管道的结果写入movies集合:

var movieCollection = client
.GetDatabase("sample_mflix")
.GetCollection<Movie>("movies");

var pipeline = new EmptyPipelineDefinition<Movie>()
.Out(movieCollection);
Node.js

The Node.js examples on this page use the sample_mflix database from the Atlas sample datasets. 本页上的Node.js示例使用Atlas示例数据集中的sample_mflix数据库。To learn how to create a free MongoDB Atlas cluster and load the sample datasets, see Get Started in the MongoDB Node.js driver documentation.要了解如何创建免费的MongoDB Atlas集群并加载示例数据集,请参阅MongoDB Node.js驱动程序文档中的入门

To use the MongoDB Node.js driver to add a $out stage to an aggregation pipeline, use the $out operator in a pipeline object.要使用MongoDB Node.js驱动程序向聚合管道添加$out阶段,请在管道对象中使用$out运算符。

The following example creates a pipeline stage that writes the results of the pipeline into the movies collection. The example then runs the aggregation pipeline:以下示例创建了一个管道阶段,将管道的结果写入movies集合。然后,该示例运行聚合管道:

const pipeline = [{ $out: { db: "sample_mflix", coll: "movies" } }];

const cursor = collection.aggregate(pipeline);
return cursor;