KeplerGrid guide

How to debug a MongoDB aggregation pipeline stage by stage

When an aggregation returns an unexpected result, start at the first stage where the documents change. Inspect the input and output to see which fields or documents that stage passes along.

Inspect the input and output of a stage

  1. In KeplerGrid, choose Load sample data, open orders, and select Aggregation. The sample workspace runs without a MongoDB connection.
  2. Choose $limit and click Add stage. Set Rows to 3 and click Run pipeline.
  3. Select the $limit stage. Compare the sample documents under Input with the three documents under Output. The full pipeline stays visible above the preview.
KeplerGrid’s sample orders pipeline with a limit of three and side-by-side stage input and output
The sample $limit stage receives an input preview and passes through three documents.

See why stage order matters

Switch to JSON and run this pipeline. The first stage selects paid orders. The second stage passes through up to three of those orders.

[
  { "$match": { "status": "paid" } },
  { "$limit": 3 }
]

In the built-in sample, the pipeline returns three paid orders. Switch to Stages and select $limit. Its input should contain only orders with status: "paid".

Swap the stages and run again. With $limit first, $match receives at most three orders to inspect, so the result can contain fewer than three paid orders. Add a $sort before $limit if you need a particular order.

Export MongoDB aggregation results to CSV or JSON

How to read a MongoDB aggregation Explain plan

MongoDB query filters for ObjectId, dates, and text

Free for personal, noncommercial use. Download KeplerGrid or open the getting-started guide.

Try grouping orders by day, finding the latest order per customer, or saving and reopening the pipeline.

Try multiple summaries with $facet or compute a field in a pipeline.

Watch a sample query in 30 seconds

Follow along in the built-in sample workspace. No MongoDB connection is needed.

Read the transcript

Only three orders came back. Where did the rest go?

In KeplerGrid, open the sample orders. Set the limit to three and run the pipeline.

Now click the stage. Input shows what arrived. Output shows what the stage returned.

There’s the answer. This stage kept three orders. In a longer pipeline, check the next stage the same way.

Try the built-in sample, or bring a query from your own development database.