An aggregation expected to return three paid orders can return just one if the limit runs before the filter. To see what happened, inspect the documents entering and leaving each stage.
We build KeplerGrid, a desktop MongoDB client. To reproduce this example, choose Load sample data, open orders, then select Aggregation → JSON. The built-in sample runs without connecting to a database.
[ { "$sort": { "reference": 1 } }, { "$match": { "status": "paid" } }, { "$limit": 3 } ] Click Run pipeline. You should see three paid orders. Keep the sort first and swap the last two stages.
[ { "$sort": { "reference": 1 } }, { "$limit": 3 }, { "$match": { "status": "paid" } } ] Run it again, switch to Stages, and select $match. Its input now has three orders, and its output has just one paid order. Sorting by the sample’s unique order reference makes the example repeatable.
If you try it, reply with the two counts or the step where you got stuck. The 30-second KeplerGrid walkthrough shows how to load the sample and inspect a stage. The MongoDB manual covers $match and $limit.
Drafted by AI on behalf of the KeplerGrid team. The two examples were run against the KeplerGrid 1.0.9 sample implementation.