对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

聚合管道(Aggregation pipeline)建立器

The Data Explorer provides an aggregation pipeline builder to process your data. Aggregation pipelines transform your documents into aggregated results based on selected pipeline stages.

MongoDB Atlas 聚合管道构建器主要用于构建管道,而不是执行管道。 管道构建器提供了一种导出管道以在驱动程序中执行的简单方法。

要在Cloud Manager用户界面中与数据交互,请执行以下操作:

1
  1. 如果尚未显示,请从导航栏上的 Organizations 菜单中选择包含所需项目的组织。

  2. 如果尚未显示,请从导航栏的Projects菜单中选择所需的项目。

  3. 在侧边栏中,单击 Database 标题下的 Processes。

The Processes page displays.

2
  1. 选择所需的 MongoDB 部署。

  2. 选择 Data 标签页。

要在Data Explorer中创建和执行聚合管道,您必须至少被授予Project Data Access Read Only角色。

To utilize the $out stage in your pipeline, you must have been granted at least the Project Data Access Read/Write role.

1

主面板和左侧的 Namespaces 列出数据库中的集合。

2

主面板显示 Find、Indexes 和 Aggregation 视图。

3

首次打开Aggregation视图时, Data Explorer显示一个空的聚合管道。

聚合管道构建器的初始视图。
点击放大
1

从左下角面板的 Select(选择)下拉菜单中选择聚合阶段。

显示选择管道阶段的屏幕截图。
点击放大

下拉列表右侧的切换指示是否已启用该阶段。

2

Fill in your stage with the appropriate values. If Comment Mode is enabled, the pipeline builder provides syntactic guidelines for your selected stage.

修改阶段时, Data Explorer会根据当前阶段的结果更新右侧的预览文档。

显示管道阶段结果的屏幕截图。
点击放大
3

有两种方法可以向管道添加附加阶段:

  • 单击管道底部的 Add Stage 按钮,在管道末端添加新阶段:

  • 单击一个阶段上的 按钮可在该阶段之后直接添加新阶段。

突出显示"Add Stage"(添加阶段)按钮的屏幕截图。

要删除管道阶段,请单击图标。

4

使用排序规则来指定特定语言的字符串比较规则,如字母大小写和重音符号规则。

要指定排序规则文档,请点击管道构建器顶部的 Collation(排序规则)。

排序规则文档包含以下字段:

{
locale: <string>,
caseLevel: <boolean>,
caseFirst: <string>,
strength: <int>,
numericOrdering: <boolean>,
alternate: <string>,
maxVariable: <string>,
backwards: <boolean>
}

The locale field is mandatory; all other collation fields are optional. For descriptions of the fields, see Collation Document.

您可以将聚合管道从纯文本导入到管道构建器中,以轻松修改和验证管道。

要从纯文本导入管道,请执行以下操作:

1
  1. 单击管道构建器顶部加号图标旁边的箭头。

  2. 单击 New Pipeline from Text(连接)。

    从文本对话框打开新管道的截图。
    点击放大
2

Your pipeline must match the syntax of the pipeline parameter of the db.collection.aggregate() method.

3
4

要使管道返回到初始空白状态,单击管道构建器顶部的“+”图标。

You can use the aggregation pipeline builder to export your finished pipeline to one of the supported client library languages; Java, Node, C#, and Python 3. Use this feature to format and export pipelines for use in your applications.

要导出聚合管道,请执行以下操作:

1

For instructions on creating an aggregation pipeline, see Create an Aggregation Pipeline.

2
3

在 Export Pipeline To 下拉菜单中,选择所需的语言。

The My Pipeline pane on the left displays your pipeline in mongosh syntax.

右侧窗格将以所选语言显示您的管道。

4

(可选) :选中 Include Import Statements 选项,以包含选定语言所需的导入语句。

5

单击管道右上方的 Copy(复制)按钮,从而将所选语言的管道复制到剪贴板。现在,您可将管道集成到应用程序中。

6

修改 aggregation pipeline 构建者设置:

1
突出显示管道“设置”按钮的屏幕截图。
2

您可以修改以下设置:

设置
说明
默认

Comment Mode

启用后, Data Explorer会向每个阶段添加助手注释。

更改此设置只会影响新阶段,而不会修改已添加到管道中的阶段。

On(开启)

Number of Preview Documents

每个阶段的预览中所显示的文档数。

20

3