Aggregation Pipeline Builder
On this page
New in version 1.14.0
The Aggregation Pipeline Builder in MongoDB Compass provides the ability to create aggregation pipelines to process data. In aggregation pipelines, documents in a collection or view pass through stages where MongoDB Compass processes them into a set of aggregated results. You can change the stages and results to suit your needs.
To start building an aggregation pipeline for a collection or view, choose the collection and click the Aggregations tab. A blank aggregation pipeline displays. The Preview of Documents in the Collection section of the Aggregations view displays 20 documents sampled from the current collection.
You can:
- Create a new aggregation pipeline
- Save a pipeline
- Open a saved pipeline
- Set the documents limit or auto preview documents
- View or change pipeline settings
- 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
Limitations
The $out
stage is not available if you are connected to a
Data Lake.
Create an Aggregation Pipeline
Add an aggregation pipeline stage.
In the aggregation pipeline pane in the lower-left corner, click the Select... dropdown and select the aggregation pipeline stage to use for the first stage of the pipeline:

Add additional pipeline stages.
Click Add Stage to add additional aggregation stages below your last aggregation stage. Repeat steps 1 and 2 for each additional stage.
The toggle to the right of the name of each pipeline stage dictates whether that stage is included in the pipeline. Toggling a pipeline stage also updates the pipeline preview, which reflects whether or not that stage is included.
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.
To learn more, see viewing saved aggregations for details.
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 |
---|---|
(Recommended) When enabled, limits input documents passed to
$group , $bucket , and
$bucketAuto stages. Set the document limit with the
Limit setting. | |
When enabled, Compass automatically updates the
preview documents pane to reflect the results of each active
stage as the pipeline progresses. |
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.
OptionDescriptionDefaultWhen enabled, adds helper comments to each stage.EnabledNumber of documents to show in the preview.20Cumulative time limit in milliseconds for processing the pipeline. Use this option to limit the waiting time if the pipeline stops responding.5000When Sample Mode is enabled, specifies the number of documents passed to$group
,$bucket
, and$bucketAuto
stages. Lower limits improve pipeline running time, but may miss documents.100000- Click Apply to save changes and close the Settings panel.
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 the Collation button at the top of the pipeline builder.
- Enter your collation document.
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.
Example
In this example, you create and run a pipeline for a collection with airline data. You can download this dataset from the following link: air_airlines.json.
For instructions on importing JSON data into your cluster, see
mongoimport. This
example assumes you have the data in the
example.air_airlines
namespace.
Create the Pipeline
The following pipeline has two aggregation stages:
$group
and $match
.
- The
$group
stage groups documents by theiractive
status andcountry
. The stage also adds a newflightCount
field containing the number of documents in each group. - The
$match
stage filters documents to return documents with aflightCount
value greater than or equal to5
.

Copy the Pipeline to the Clipboard
Click the Export to Language button at the top of the pipeline builder. The following page displays:
- Click the Copy button in the My Pipeline
panel on the left. Compass copies the pipeline to your
clipboard in
mongo
shell syntax. You will use the pipeline in the following section.
Run the Pipeline
- Launch and connect to a
mongod instance which contains the imported
air_airlines.json
dataset. Switch to the
example
database where theair_airlines
collection exists:use example Run the following command to run the pipeline created in Compass:
db.air_airlines.aggregate([{ $group: { _id: { active: '$active', country: '$country' }, flightCount: { $sum: 1 } } }, { $match: { flightCount: { $gte: 5 } } }])
The pipeline returns documents, as shown in the following abbreviated example:
{ "_id" : { "active" : "Y", "country" : "Nigeria" }, "flightCount" : 5 } { "_id" : { "active" : "N", "country" : "Switzerland" }, "flightCount" : 46 } { "_id" : { "active" : "N", "country" : "Bahrain" }, "flightCount" : 8 } { "_id" : { "active" : "N", "country" : "Guinea-Bissau" }, "flightCount" : 8 } { "_id" : { "active" : "N", "country" : "Argentina" }, "flightCount" : 14 } { "_id" : { "active" : "N", "country" : "Moldova" }, "flightCount" : 17 } { "_id" : { "active" : "Y", "country" : "Israel" }, "flightCount" : 6 } { "_id" : { "active" : "N", "country" : "Finland" }, "flightCount" : 7 }