MongoDB Aggregation Pipeline Builder

Build an aggregation pipeline from form fields: $match, $unwind, $lookup, $group, $sort, $project, $skip and $limit. Output as mongosh, Node.js driver, PyMongo or plain JSON.

Use the tool

Why this tool exists

Aggregation pipelines are powerful but fiddly to type: nested braces, dollar signs on field references and a stage order that matters. A missing bracket costs time, and translating a working mongosh query into Python or Node adds another chance for error.

Fill in only the stages you need and the tool assembles them in a sensible order: match first, then unwind, lookup, group, sort, project, skip and limit. Accumulators are written as name:op:field, for example total:sum:amount. You pick the output language, and get a snippet ready to paste. It builds a basic pipeline, not every operator MongoDB offers, so extend the result by hand for expressions and conditions. Nothing leaves your browser.

How to use it

  1. Enter the collection name and choose the output format.
  2. Fill in the stages you need, such as a $match JSON filter and a $group field.
  3. Write accumulators as name:op:field, separated by commas.
  4. Add sort, skip and limit, then copy the generated pipeline.

Frequently asked questions

Which stages and accumulators are supported?

Stages: $match, $unwind, $lookup, $group, $sort, $project, $skip and $limit. Accumulators: sum, avg, min, max, first, last, push, addToSet and count. Others you add by hand.

In what order are stages added?

In a fixed order: match, unwind, lookup, group, sort, project, skip, limit. If you need a different order, copy the JSON output and rearrange the stages.

How do I group all documents together?

Enter null as the group field. The _id of the group becomes null, so accumulators run over the whole collection, which is useful for totals and averages.

Does it run the query?

No. It only generates code. Test the pipeline on a copy of your data or with explain before running it on production.