Docs HomeMongoDB Compass

Aggregation Pipeline Builder聚合管道生成器

New in version 1.14.0.

The Aggregation Pipeline Builder in MongoDB Compass lets you create aggregation pipelines to process documents from a collection or view and return computed results. MongoDB Compass中的聚合管道生成器允许您创建聚合管道来处理集合或视图中的文档并返回计算结果。For example, you can use aggregation pipelines to:例如,可以使用聚合管道执行以下操作:

  • Group values from multiple documents together.将多个文档中的值组合在一起。
  • Perform operations on the grouped data to return a single result.对分组数据执行操作以返回单个结果。
  • Analyze data changes over time.分析数据随时间的变化。

To build an aggregation pipeline, choose a collection and click the Aggregations tab. 若要构建聚合管道,请选择一个集合,然后单击“聚合”选项卡。Compass displays a blank aggregation pipeline. The Preview of Documents in the Collection section shows 20 documents randomly sampled from the chosen collection.Compass显示一个空白的聚合管道。“集合”部分中的“文档预览”显示从所选集合中随机抽取的20个文档。

Note

Atlas Search Stages

When you connect Compass to a MongoDB deployment hosted on Atlas, additional Atlas-only stages $search and $searchMeta are available in the Aggregation Pipeline Builder. 当您将Compass连接到Atlas上托管的MongoDB部署时,聚合管道生成器中提供了额外的仅用于Atlas的阶段$search$searchMetaUse these stages to perform full-text search on Atlas collections.使用这些阶段可以对Atlas集合执行全文搜索

Tasks任务

You can use the Compass Aggregation Pipeline Builder to:您可以使用Compass聚合管道生成器:

Limitations局限性

Create an Aggregation Pipeline创建聚合管道

MongoDB Compass provides different modes to create aggregation pipelines:MongoDB Compass提供了不同的模式来创建聚合管道:

  • Stage View Mode, a visual pipeline editor that preloads pipeline syntax based on your selected stages.阶段视图模式,一个可视化的管道编辑器,根据您选择的阶段预加载管道语法。
  • Stage Wizard, a feature of Stage View Mode that provides a set of templates for simple aggregation stage use cases. 阶段向导,阶段视图模式的一个功能,为简单的聚合阶段用例提供了一组模板。The Stage Wizard only includes simple use cases to help you get started with your aggregation pipeline.阶段向导只包括一些简单的用例,可以帮助您开始使用聚合管道。
  • Focus Mode, a feature of Stage View Mode where you edit one pipeline stage at a time. 聚焦模式,阶段视图模式的一个功能,您可以在其中一次编辑一个管道阶段。Focus Mode helps you manage complex or deeply nested aggregation pipeline stages.Focus Mode可帮助您管理复杂或深度嵌套的聚合管道阶段。
  • Text View Mode, a text-based pipeline editor that accepts raw pipeline syntax.文本视图模式,一个基于文本的管道编辑器,接受原始管道语法。

To see how to create an aggregation pipeline, select the tab corresponding to your chosen view mode:要查看如何创建聚合管道,请选择与所选视图模式相对应的选项卡:

Save a Pipeline保存管道

You can save a pipeline so that you can find it later. If you load a saved pipeline, you can change it without changing the original saved copy. 您可以保存管道,以便以后可以找到它。如果加载保存的管道,则可以在不更改原始保存副本的情况下对其进行更改。You can also create a view from your pipeline results.您还可以根据管道结果创建视图

To save your pipeline:要保存管道,请执行以下操作:

1

Click the save dropdown button单击保存下拉按钮

Click the Save dropdown button at the top of the pipeline builder. 单击管道生成器顶部的“保存”下拉按钮。Click on Save pipeline as...单击“将管道另存为…”。

Save pipeline as
2

Enter a name for your pipeline输入管道的名称

Name your pipeline
3

Click Save单击“保存”

Open a Saved Pipeline打开保存的管道

  1. Click the Folder icon at the top left of the pipeline builder.单击管道生成器左上角的“文件夹”图标。

    Saved Pipeline Icon
  2. Hover over the pipeline you want to open and click Open.将鼠标悬停在要打开的管道上,然后单击“打开”。

    Saved Pipelines List
  3. In the modal, click Open Pipeline.在模式中,单击“打开管道”。
Note

Starting in MongoDB Compass 1.31, you can view your saved queries and aggregation pipelines on the My Queries view once connected to your cluster.从MongoDB Compass 1.31开始,连接到集群后,您可以在“我的查询”视图上查看保存的查询和聚合管道。

To learn more, see viewing saved aggregations for details.要了解更多信息,请参阅查看保存的聚合以了解详细信息。

Set maxTimeMS for Aggregation Queries为聚合查询设置maxTimeMS

Use the maxTimeMS parameter on the Aggregations tab to specify an upper time limit in milliseconds on the aggregation pipelines that run in Compass.使用“聚合”选项卡上的maxTimeMS参数可以指定在Compass中运行的聚合管道的时间上限(以毫秒为单位)。

To enable maxTimeMS for aggregation operations:要为聚合操作启用maxTimeMS,请执行以下操作:

1

On the Aggregations tab, click More Options.在“聚合”选项卡上,单击“更多选项”。

Toggle more options
2

Enter a value next to Max Time MS to set a maximum amount of time in milliseconds that an aggregation pipeline can run. For a 5 second limit, enter 5000.在“最大时间MS”旁边输入一个值,以设置聚合管道可以运行的最大时间(以毫秒为单位)。输入5000作为5秒的限制。

Enter a maximum timeout value

If your aggregation operation goes over the time limit, Compass raises the following error:如果聚合操作超过时间限制,Compass将引发以下错误:

Operation timeout error

Set the Documents Limit or Auto Preview Documents设置文档限制或自动预览文档

Use the toggles at the top of the pipeline builder to set the limit on the number of sampled documents or to enable auto preview.使用管道生成器顶部的切换可以设置采样文档数的限制或启用自动预览。

Option选项Description描述
Sample Mode采样模式(Recommended) When enabled, limits input documents passed to $group, $bucket, and $bucketAuto stages. 推荐)启用时,限制传递到$group$bucket$bucketAuto阶段的输入文档。Set the document limit with the Limit setting.使用Limit设置设置文档限制。
Auto Preview自动预览When enabled, Compass automatically updates the preview documents pane to reflect the results of each active stage as the pipeline progresses.启用后,Compass会自动更新预览文档窗格,以反映管道进展过程中每个活动阶段的结果。

View or Change Pipeline Settings查看或更改管道设置

To view and change pipeline settings:要查看和更改管道设置,请执行以下操作:

  1. Click the gear icon at the upper right of the pipeline builder to open the Settings panel.单击管道生成器右上角的齿轮图标以打开“设置”面板。
  2. Change any of the following pipeline settings.更改以下任何管道设置。

    Option选项Description描述Default
    Comment Mode注释模式When enabled, adds helper comments to each stage.启用时,会向每个阶段添加辅助注释。Enabled
    Number of Preview Documents预览文档数Number of documents to show in the preview.要在预览中显示的文档数。20
    Limit限制When Sample Mode is enabled, specifies the number of documents passed to $group, $bucket, and $bucketAuto stages. 启用“示例模式”时,指定传递到$group$bucket$bucketAuto阶段的文档数。Lower limits improve pipeline running time, but may miss documents.下限可以缩短管道运行时间,但可能会错过文档。100000
  3. Click Apply to save changes and close the Settings panel.单击“应用”保存更改并关闭“设置”面板。

Explain Plan for Pipelines解释管道计划

Use the Explain button to view the explain plan that helps you understand the performance of your pipelines. 使用“解释”按钮查看解释计划,该计划可帮助您了解管道的性能。You can view the explain plan at any point while creating or editing your pipeline.在创建或编辑管道时,您可以随时查看解释计划。

To view your pipeline explain plan, click Explain in the right corner of your pipeline toolbar.要查看管道解释计划,请单击管道工具栏右角的“解释”。

Aggregation Pipeline Builder Explain Plan

Your results are presented in this format:您的结果以以下格式显示:

Aggregation Pipeline Builder Explain Results

Specify Custom Collation指定自定义排序规则

Use custom collation to specify language-specific rules for string comparison, such as rules for letter case and accent marks.使用自定义排序规则为字符串比较指定特定于语言的规则,例如字母大小写和重音标记的规则。

To specify a custom collation:要指定自定义排序规则,请执行以下操作:

  1. Click More Options in the top right corner of the pipeline builder.单击管道生成器右上角的“更多选项”。

    Collation Option
  2. Enter your collation document.输入整理文档。

Export Aggregation Results导出聚合结果

To export results from your aggregation pipeline:要从聚合管道导出结果,请执行以下操作:

  1. Connect to the deployment containing the collection you wish to export data from.连接到包含要从中导出数据的集合的展开。

    To learn how to connect to a deployment, see Connect to MongoDB.要了解如何连接到部署,请参阅连接到MongoDB

  2. Navigate to your target collection.导航到您的目标集合。

    You can either select the collection from the Collections tab or click the collection in the left-hand pane.您可以从“集合”选项卡中选择集合,也可以单击左侧窗格中的集合。

  3. Create an aggregation pipeline and run for results.创建聚合管道并运行以获取结果。

    To learn how to create an aggregation pipeline, see Create an Aggregation Pipeline.要了解如何创建聚合管道,请参阅创建聚合管道

  4. Click Export.单击“导出”。
  5. Choose the appropriate file type.选择适当的文件类型。

    Under Select Export File Type, select either JSON or CSV. 在“选择导出文件类型”下,选择“JSON”或“CSV”。If you select JSON, your data is exported to the target file as an array of JSON objects.如果选择“JSON”,则数据将作为JSON对象数组导出到目标文件。

  6. Under Output, choose where to export the file to.在“输出”下,选择将文件导出到的位置。

    Export Aggregation Data
  7. Click Export.单击“导出”。

Create a View from Pipeline Results根据管道结果创建视图

Note

Creating a view from pipeline results does not save the pipeline itself.根据管道结果创建视图不会保存管道本身。

To create a view from your pipeline results:要根据管道结果创建视图,请执行以下操作:

  1. Click the arrow next to the Save button at the top of the pipeline builder.单击管道生成器顶部“保存”按钮旁边的箭头。
  2. Click Create a View.单击“创建视图”。
  3. Enter a name for your view.输入视图的名称。
  4. Click Create.单击“创建”。

Compass creates a view from your pipeline results in the same database where the pipeline was created.Compass在创建管道的同一数据库中根据管道结果创建视图。

Refresh Document Counts刷新文档计数

When you delete or add a document, the count results value on the Aggregations tab does not automatically update to reflect the new document count.删除或添加文档时,“聚合”选项卡上的计数结果值不会自动更新以反映新的文档计数。

To update the count results:要更新“计数结果”:

1

(Optional) If the results have not been counted, click the count results link.(可选)如果尚未对结果进行计数,请单击计数结果链接。

Click the count results link
2

Click the refresh button.单击刷新按钮。

Click the refresh button

Compass updates the count results and displays the updated number of documents in the collection.Compass会更新计数结果,并显示集合中更新的文档数。

Show current document counts