Skip to content

Aggregation editor

  • MongoDB

The aggregation editor builds a pipeline as a list of stage cards. Each card previews its own output on a sample of the collection, so you see what every stage does before you run the whole pipeline. You can switch a stage off, reorder stages, edit the pipeline as one text, save it under a name and export it as code.

The MongoDB aggregation editor with $group and $sort stage cards, each previewing its output: the average review rating per wood, then sorted. The pipeline's result table shows elm, walnut and cherry at the top.The MongoDB aggregation editor with $group and $sort stage cards, each previewing its output: the average review rating per wood, then sorted. The pipeline's result table shows elm, walnut and cherry at the top.
Every stage previews its own output, so you can see the average rating per wood take shape.
  • Choose Aggregate… from a collection’s menu in the explorer.
  • Choose Tools › Aggregate in the collection view. The pipeline starts with the view’s current filter as a $match stage.
  • Choose Open in aggregation editor in a SQL tab whose SELECT translates to an aggregate().
  1. Pick the stage’s operator in the card’s list. A short description of the stage appears beside it, with notes such as “must be first” or “writes”.
  2. Type the stage body in mongosh syntax, for example { category: 'chairs' } for a $match.
  3. Choose Add stage to add a stage at the end, or a card’s + button to add one after that card.
  4. Watch each card’s preview, for example “20 documents from a sample of 1,000”.
  5. Choose Run to run the enabled stages on the whole collection.

Each card also has:

  • Enabled: clear it to skip the stage. Its preview reads “Not previewed”, and the later stages preview without it.
  • Move up and move down buttons, a drag handle, and Alt + ↑ / Alt + ↓ on the handle to reorder.
  • A remove button.
  • Preview to preview that stage again.
Setting What it does
Sample How many documents the previews start from (1,000 by default).
Sampling first documents ($limit) or random ($sample).
Auto preview Previews again as you edit (on by default).
Preview all Previews every stage now.

A preview shows at most 20 documents per stage.

Set Edit as to Text to see the whole pipeline in mongosh syntax. Disabled stages are commented out with //:

[
{ $match: { status: 'open' } },
// { $sort: { total: -1 } },
{ $limit: 3 }
]

The cards follow the text while it parses. Set Edit as back to Stages to return to the cards.

The lower half shows the run in the Tree, Table and JSON views of the collection view, or the plan in Explain. Explain and Plan only in the toolbar work as in Explain.

Pipelines are saved per collection:

  • Save as… saves the pipeline under a name.
  • Save saves over the pipeline you opened.
  • Open saved… lists the saved pipelines; Delete removes the open one.

Export code… exports the enabled stages as a program. See Export query as code.

A pipeline whose last enabled stage is $out or $merge writes its results to a collection.

  • Previewing such a stage writes nothing: the preview shows the documents it would write.
  • Run asks first and shows the pipeline. $out replaces what the target collection holds.
  • A read-only connection does not run it.

Documents Querybara 0.1.1 · built frombc9f5aa