Docs Home / Node.js / Node.js Driver

Aggregation Operations聚集操作

Overview概述

In this guide, you can learn how to use the MongoDB Node.js driver to perform aggregation operations.在本指南中,您可以学习如何使用MongoDB Node.js驱动程序执行聚合操作

Aggregation operations process data in your MongoDB collections and return computed results. The MongoDB Aggregation framework is modeled on the concept of data processing pipelines. Documents enter a pipeline comprised of one or more stages, and this pipeline transforms the documents into an aggregated result.聚合操作处理MongoDB集合中的数据并返回计算结果。MongoDB聚合框架是基于数据处理管道的概念建模的。文档进入由一个或多个阶段组成的管道,该管道将文档转换为聚合结果。

To learn more about the aggregation stages supported by the Node.js driver, see Aggregation Pipeline Stages.要了解Node.js驱动程序支持的聚合阶段的更多信息,请参阅聚合管道阶段

Tip

Complete Aggregation Tutorials完整的聚合教程

You can find tutorials that provide detailed explanations of common aggregation tasks in the Complete Aggregation Pipeline Tutorials section of the Server manual. 您可以在服务器手册的“完整聚合管道教程”部分找到提供常见聚合任务详细解释的教程。Select a tutorial, and then pick Node.js from the Select your language drop-down menu in the upper-right corner of the page.选择一个教程,然后从页面右上角的选择语言下拉菜单中选择Node.js。

Analogy类比

The aggregation pipeline is similar to an automobile factory assembly line. An assembly line has stations with specialized tools that are used to perform specific tasks. For example, when building a car, the assembly line begins with a frame. As the car frame moves though the assembly line, each station assembles a separate part. The result is a transformed final product, the finished car.聚合管道类似于汽车工厂的装配线。装配线有带有专用工具的工位,用于执行特定任务。例如,在制造汽车时,装配线从框架开始。当车架在装配线上移动时,每个工位都会组装一个单独的零件。结果是一个经过改造的最终产品,即成品车。

The aggregation pipeline is the assembly line, the aggregation stages are the assembly stations, the expression operators are the specialized tools, and the aggregated result is the finished product.聚合管道是装配线,聚合阶段是装配站,表达式运算符是专用工具,聚合结果是成品。

Compare Aggregation and Find Operations比较聚合和查找操作

The following table lists the different tasks you can perform with find operations compared to what you can achieve with aggregation operations. The aggregation framework provides expanded functionality that allows you to transform and manipulate your data.下表列出了与聚合操作相比,查找操作可以执行的不同任务。聚合框架提供了扩展的功能,允许您转换和操作数据。

Find Operations查找操作Aggregation Operations聚集操作
Select certain documents to return选择要返回的某些文档
Select which fields to return选择要返回的那些字段
Sort the results对结果进行排序
Limit the results限制结果
Count the results统计结果
Select certain documents to return选择要返回的特定文档
Select which fields to return选择要返回的那些字段
Sort the results对结果进行排序
Limit the results限制结果
Count the results统计结果
Group the results将结果分组
Rename fields重命名字段
Compute new fields计算新字段
Summarize data汇总数据
Connect and merge data sets连接和合并数据集

Server Limitations服务器限制

Consider the following limitations when performing aggregation operations:执行聚合操作时,请考虑以下限制

  • Returned documents must not violate the BSON document size limit of 16 megabytes.返回的文档不得违反BSON文档大小限制16 MB。
  • Pipeline stages have a memory limit of 100 megabytes by default. 默认情况下,管道阶段的内存限制为100兆字节。If required, you can exceed this limit by enabling the AllowDiskUse property of the AggregateOptions object that you pass to the aggregate() method.如果需要,您可以通过启用传递给aggregate()方法的AggregateOptions对象的AllowDiskUse属性来超过此限制。

Additional information附加信息

For a full list of aggregation stages, see Aggregation Stages in the MongoDB Server manual.有关聚合阶段的完整列表,请参阅MongoDB Server手册中的聚合阶段

To learn about explaining MongoDB aggregation operations, see Explain Results and Query Plans in the MongoDB Server manual.要了解解释MongoDB聚合操作,请参阅MongoDB服务器手册中的解释结果查询计划