MongoDB query compiled from plain English against the ecommerce sample schema, then verified by running it.
{
"type": "aggregation",
"collection": "orders",
"pipeline": [
{},
{},
{},
{},
{},
{}
]
}
$group — groups by "$customerId" and aggregates totalSpend, orderCount.$sort — orders by totalSpend.$limit — caps the result at 3 documents.$lookup — joins the customers collection on _id → _id.$unwind — flattens $customer into one document per element.$project — returns only _id, name, email, totalSpend, orderCount.totalCents stores money as an integer number of cents — divide by 100 before displaying it.$lookup runs *after* $limit, so it only joins customers for the 3 documents that survive — not for the whole collection. Doing the join first is the common mistake, and on a large collection it is dramatically slower for an identical result.$group, totalSpend totals $totalCents, orderCount counts documents in each group — one output document per distinct "$customerId".Live output from running this query against the sample dataset.
| totalSpend | orderCount | name | |
|---|---|---|---|
| 32900 | 1 | Priya Nair | priya@example.com |
| 28800 | 2 | Ava Chen | ava@example.com |
| 10300 | 1 | Marcus Reid | marcus@example.com |
Write this in plain English instead. Mask Databases compiles the sentence above into exactly this query at build time — no AI at runtime, and you can read the output in a diff.
Try it in the playground → No signup, runs in your browser.