Build an aggregation pipeline

Write a pipeline stage by stage, preview each stage's input and output, run it, explain it, export its result and turn it into code.

The Aggregation Editor builds a MongoDB aggregation pipeline one stage at a time. You can look at the documents going into and out of any stage before running the whole pipeline, which makes long pipelines easy to write and to debug.

The Aggregation Editor with a $match and a $group stage computing revenue per order status

Build and run a pipeline

  1. Select a collection in the connection tree (or open it).
  2. Press F4, click Aggregate in the toolbar, or choose Collection > Open Aggregation Editor. A new tab Aggregation: <collection> opens with Stage 1, a $match.
  3. In the stage editor, type the stage’s body, for example { status: { $ne: "cancelled" } }.
  4. Press Ctrl+Shift+N (Add New Stage) and pick an operator in Operator:, for example $group. The stage editor fills with that operator’s template.
  5. Write the body: { _id: "$status", orders: { $sum: 1 }, revenue: { $sum: "$total" } }.
  6. Press F5 (Run the Entire Pipeline). The result opens in Pipeline output.

Nothing runs while you edit: each result pane runs only when you ask.

From a Collection Tab, Open query in > Aggregation Editor starts a pipeline from the query bar’s filter, sort, skip, limit and projection.

Preview a stage

CommandShortcutShows
Show input to this stageF6The documents entering the selected stage
Show output from this stageF7The documents leaving the selected stage
Run the Entire PipelineF5The result of every included stage, in Pipeline output

Each pane shows one page of documents, with paging, Count Documents, Refresh (Ctrl+R) and the elapsed time. Documents are not counted until you click Count Documents. An empty pipeline returns the collection’s documents.

Work with stages

CommandShortcutWhat it does
Add New StageCtrl+Shift+NAppends a $match stage and selects it
Add New Stage Before Selected Stage / Add New Stage After Selected StageInserts next to the selected stage
Duplicate Selected StageAlt+KCopies the stage right after it
Move Selected Stage UpShift+F8Swaps it with the stage above
Move Selected Stage DownCtrl+F8Swaps it with the stage below
Delete Selected StageRemoves it
Exclude Stage From Pipeline / Include Stage in PipelineKeeps the stage, but skips it when running (the stage’s check box)
Rename stageGives the stage a name, shown as Stage N (name): $operator
Copy Stage, Paste StageOne stage, through the clipboard
AutocompleteCtrl+SpaceSuggests stages, operators and field names

Use the Up and Down arrows to move between stages in the list.

The Operator list

The list holds every aggregation stage, with a tooltip saying whether it must come first (such as $collStats, $geoNear, $documents), last ($out, $merge), or is for Atlas only ($search). Stages that need a newer server than the one you are on are greyed out, with the version they need as their tooltip. Choosing an operator inserts its template when the body is still empty or a template; your own text is kept. You can also type an operator that is not listed: any $name is sent to the server.

Bodies accept the relaxed shell syntax of the query bar: unquoted names, comments, ISODate(…), date tags. A body can be any value: a number for $limit and $skip, a string for $count or $out.

Options

Click Options (Aggregation Options):

OptionWhat it does
Allow disk useLets stages use temporary files beyond the server’s memory limit
Use custom collationString comparison rules (locale, strength, case…), as in Find Query Options
Index hint:An index name, or a key pattern such as { $natural: -1 }
Max time (ms):Stops the pipeline after this many milliseconds

The options apply to every run, preview and count.

Explain, code, export

CommandShortcutWhat it does
Explain pipelineF8Visual Explain of the included stages, in the Explain tab
Query CodeThe pipeline in every Query Code language
Copy Aggregate QueryThe pipeline as db.getCollection("…").aggregate([…], { … })
Paste Aggregate QueryReplaces the stages with a pasted aggregate(…) statement or [ … ] array; the tab’s collection stays
Export Documents (right-click a result pane)Exports the pane’s result as JSON, CSV, Excel, SQL, BSON or into a collection
Create ViewCreates a view with this pipeline (Create view from this aggregate query)
Save query > Save queryCtrl+SSaves the pipeline to the Query Manager
Save query > Save fileShift+Ctrl+SSaves it to a file
Load queryCtrl+OOpens a saved pipeline
Load fileShift+Ctrl+OOpens a pipeline file; Open recent lists the last ones
Query historyPipelines you ran

The connection, database and collection selectors at the top move the pipeline to another target; the result panes empty.

Pipelines that write

A pipeline with an included $out or $merge writes into a collection. It:

  • asks for confirmation first, naming the target collection;
  • is refused on a read-only connection;
  • shows The result was written to … instead of documents.

Counting and full explain leave a final $out or $merge out, so they never write.

Troubleshooting

MessageMeaning
Stage 3: expected … (line 1, column 7)The body of stage 3 does not read; nothing was sent.
Stage 2 ($group): <server message>The server refused the pipeline; the editor names the stage that fails.
A memory limit errorA stage needed more than the server allows: turn on Allow disk use.
An operator is greyed outThe server is too old for it; its tooltip says which version it needs.