Navigation

Aggregation Pipeline Builder

Aggregations Tab

New in version 1.14.0

The Aggregations tab in MongoDB Compass provides the ability to create aggregation pipelines to process data. With aggregation pipelines, documents in a collection pass through multiple stages where they are processed into a set of aggregated results. The particular stages and results can be modified depending on your application’s needs.

When navigating to the Aggregations tab, you are presented with a blank aggregation pipeline. The Preview of Documents in the Collection section of the Aggregations view displays 20 documents sampled from the current collection.

Create an Aggregation Pipeline

To create an aggregation pipeline:

  1. In the aggregation pipeline pane in the bottom-left of the view, click the Select… dropdown and select the aggregation pipeline stage to use for the first stage of the pipeline:


    Aggregation Builder select stage

    Note

    The pre-filled content of the pipeline stage changes based on the selected stage to provide syntactic guidelines.

  2. Fill in your selected stage. As you modify the pipeline stage, the sample documents shown in the pane to the right of the stage update automatically to reflect the results of your pipeline as it progresses:


    Aggregation Builder match stage example
  3. Add additional aggregation stages as desired by clicking the Add Stage button below your last aggregation stage. Repeat steps 1 and 2 for each additional stage.

Note

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.

Example

The following pipeline excludes the first $match stage and only includes the $project stage:

Aggregation Builder exclude stage example

Save a Pipeline

To save a pipeline:

  1. Name your pipeline using the Enter a pipeline name input at the top of the Aggregations view.
  2. Click Save Pipeline.

Open a Saved Pipeline

Important

Opening a saved pipeline abandons any unsaved changes to the pipeline you are currently working on. If the circle at the top-right of the Aggregations view is orange, this means you have unsaved changes to your pipeline.

To open a previously saved pipeline:

  1. Click the Folder icon to the left of the pipeline name input.
  2. Hover over the pipeline you want to open and click Open.
  3. In the ensuing dialog, click Open Pipeline.

Additional Pipeline Options

Click the button to the right of the Save Pipeline button to access additional pipeline options:

Aggregation Builder additional pipeline options

The following additional options are available:

Option Description
Copy Pipeline to Clipboard Copy the current state of the pipeline to the clipboard as JSON. Only pipeline stages enabled using their respective toggles are included in the copied pipeline.
Clone Pipeline Creates a new version of the current pipeline by appending (copy) to the current pipeline name, retaining the current pipeline state. This allows you to modify and save a new version of the pipeline.
New Pipeline Resets the pipeline to its initial state.

Important

The Clone Pipeline and New Pipeline options do not save your current pipeline state. If you wish to save your current pipeline, you must do so before using one of these options.

If the circle at the top-right of the Aggregations view is orange, this means you have unsaved changes to your pipeline.

Example

The following pipeline has two aggregation stages: $match and $group. The $match stage returns documents with carrierFsCode equal to 1I, and the $group stage groups the documents returned from the previous stage by the departureAirportFsCode property:

Aggregation Builder Full Example

Using the Copy Pipeline to Clipboard feature, the following JSON is copied to the system clipboard:

{
  $match: {
    carrierFsCode: "1I"
  }
},
{
  $group: {
    _id: "$departureAirportFsCode",
    flights: { $push: "$flightId" }
  }
}

Now that the pipeline is in the clipboard, we can execute it in the mongo shell. After launching and connecting to a mongod instance, we switch to the test database where our flightStats collection exists:

use test

The following command executes the pipeline created in MongoDB Compass:

db.flightStats.aggregate([
{
  $match: {
    carrierFsCode: "1I"
  }
},
{
  $group: {
    _id: "$departureAirportFsCode",
    flights: { $push: "$flightId" }
  }
}
])

The pipeline returns the following document:

{ "_id" : "EWR", "flights" : [ 543184347, 544589251, 544589200, 543183182, 545515483, 544595864 ] }