For AI agents: a documentation index is available at https://www.mongodb.com/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.
Docs Menu

Create an Aggregation Pipeline

The Aggregation Pipeline Builder in Atlas helps you create aggregation pipelines to process documents from a collection or view and return computed results.

Atlas provides different modes to create aggregation pipelines:

  • 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.

  • Text View Mode, a text-based pipeline editor that accepts raw pipeline syntax.

To build an aggregation pipeline, choose a collection and click the Aggregations tab. Atlas displays a blank aggregation pipeline. The Preview of Documents in the Collection section shows 10 documents randomly sampled from the chosen collection.

Note

MongoDB Search Stages

When you connect to a MongoDB deployment hosted on Atlas, Atlas-only stages $search and $searchMeta become available in the Aggregation Pipeline Builder. Use these stages to perform full-text search on Atlas collections.

To see how to create an aggregation pipeline, select the tab corresponding to your chosen view mode:

1
  1. If it's not already displayed, select the organization that contains your project from the Organizations menu in the navigation bar.

  2. If it's not already displayed, select your project from the Projects menu in the navigation bar.

  3. In the sidebar, click Data Explorer under the Database heading.

    The Data Explorer displays.

IMPORTANT: You can also click the name of a cluster to open the Cluster sidebar, and then click Data Explorer under the Shortcuts heading.

2
  1. Select the collection.

  2. In the aggregation pipeline pane, ensure the {} Stages toggle switch is selected.

Toggle on stage view mode
click to enlarge
3

At the bottom of the aggregation pipeline pane, click the + Add Stage button.

4

On the upper-left corner of the aggregation stage card, click the Select drop-down menu and select the aggregation pipeline stage to use for the first stage of the pipeline.

5

Fill in your selected stage. You can adjust the width of the pipeline stage by dragging its border to the right.

Note

The toggle to the right of each pipeline stage name dictates whether that stage is included in the pipeline. Toggling a pipeline stage also updates the pipeline preview, which updates based on whether or not that stage is included.

For example, the following pipeline excludes the first $match stage and only includes the $project stage:

Aggregation Builder exclude stage example
6

To add an additional pipeline stage after your last aggregation stage, click Add Stage. To add an aggregation stage before your most recently added stage, click the plus + icon above the stage card.

Repeat steps 3 and 4 for each additional stage.

Note

You can change the order of pipeline stages by dragging the header of each stage card.

7

At the top-right corner of the pipeline builder, click Run. Atlas returns your results in the document view.

Warning

Some aggregation operators, like $merge and $out, can modify your collection's data.

If your aggregation pipeline contains operators that can modify your collection's data, you are prompted for confirmation before the pipeline is executed.