Aggregation Pipeline Builder聚合管道生成器
On this page本页内容
Tasks任务Limitation限制sCreate an Aggregation Pipeline创建聚合管道Save a Pipeline保存管道- Open a Saved Pipeline
- Set
maxTimeMS
for Aggregation Queries Set the Documents Limit or Auto Preview Documents设置文档限制或自动预览文档View or Change Pipeline Settings查看或更改管道设置Explain Plan for Pipelines解释管道计划Specify Custom Collation指定自定义排序规则Export Aggregation Results导出聚合结果Create a View from Pipeline Results根据管道结果创建视图Refresh Document Counts刷新文档计数
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个文档。
Atlas Search Stages
When you connect Compass to a MongoDB deployment hosted on Atlas当您将Compass连接到Atlas, additional Atlas-only stages $search and $searchMeta are available in the Aggregation Pipeline Builder.
上托管的MongoDB部署时,聚合管道生成器中提供了额外的仅用于Atlas的阶段$search和$searchMeta。
Use these stages to perform full-text search on Atlas collections.使用这些阶段可以对Atlas集合执行全文搜索。
Tasks任务
You can use the Compass Aggregation Pipeline Builder to:您可以使用Compass聚合管道生成器:
Create a new aggregation pipeline创建新的聚合管道Save a pipeline保存管道Open a saved pipeline打开保存的管道Set maxTimeMS for Aggregation Queries为聚合查询设置maxTimeMSSet the documents limit or auto preview documents设置文档限制或自动预览文档View or change pipeline settings查看或更改管道设置View the explain plan for pipelines查看管道的解释计划Export Aggregation Results导出聚合结果Specify custom collation指定自定义排序规则Create a view from pipeline results根据管道结果创建视图Import a pipeline from plain text从纯文本导入管道Export a pipeline to Java, Node, C#, and Python 3将管道导出到Java、Node、C#和Python 3Refresh Document Counts刷新文档计数
Limitations局限性
The如果连接到数据湖,$out
stage is not available if you are connected to a Data Lake.$out
阶段不可用。You cannot run the Atlas Search您不能在时间序列集合上运行Atlas Search$search
or$searchMeta
stages on a time series collection.$Search
或$searchMeta
阶段。
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:要保存管道,请执行以下操作:
Open a Saved Pipeline打开保存的管道
Click the Folder icon at the top left of the pipeline builder.单击管道生成器左上角的“文件夹”图标。Hover over the pipeline you want to open and click Open.将鼠标悬停在要打开的管道上,然后单击“打开”。In the modal, click Open Pipeline.在模式中,单击“打开管道”。
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
maxTimeMS
for Aggregation QueriesUse 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
,请执行以下操作:
On the Aggregations tab, click More Options.在“聚合”选项卡上,单击“更多选项”。

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 在“最大时间MS”旁边输入一个值,以设置聚合管道可以运行的最大时间(以毫秒为单位)。输入5
second limit, enter 5000
.5000
作为5
秒的限制。

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

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.使用管道生成器顶部的切换可以设置采样文档数的限制或启用自动预览。
$group , $bucket , and $bucketAuto stages. $group 、$bucket 和$bucketAuto 阶段的输入文档。Limit setting.Limit 设置设置文档限制。 | |
View or Change Pipeline Settings查看或更改管道设置
To view and change pipeline settings:要查看和更改管道设置,请执行以下操作:
Click the gear icon at the upper right of the pipeline builder to open the Settings panel.单击管道生成器右上角的齿轮图标以打开“设置”面板。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 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.要查看管道解释计划,请单击管道工具栏右角的“解释”。

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

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:要指定自定义排序规则,请执行以下操作:
Click More Options in the top right corner of the pipeline builder.单击管道生成器右上角的“更多选项”。Enter your collation document.输入整理文档。
Export Aggregation Results导出聚合结果
To export results from your aggregation pipeline:要从聚合管道导出结果,请执行以下操作:
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。Navigate to your target collection.导航到您的目标集合。You can either select the collection from the Collections tab or click the collection in the left-hand pane.您可以从“集合”选项卡中选择集合,也可以单击左侧窗格中的集合。Create an aggregation pipeline and run for results.创建聚合管道并运行以获取结果。To learn how to create an aggregation pipeline, see Create an Aggregation Pipeline.要了解如何创建聚合管道,请参阅创建聚合管道。Click Export.单击“导出”。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对象数组导出到目标文件。Under Output, choose where to export the file to.在“输出”下,选择将文件导出到的位置。Click Export.单击“导出”。
Create a View from Pipeline Results根据管道结果创建视图
Creating a view from pipeline results does not save the pipeline itself.根据管道结果创建视图不会保存管道本身。
To create a view from your pipeline results:要根据管道结果创建视图,请执行以下操作:
Click the arrow next to the Save button at the top of the pipeline builder.单击管道生成器顶部“保存”按钮旁边的箭头。Click Create a View.单击“创建视图”。Enter a name for your view.输入视图的名称。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:要更新“计数结果”: