Answer business questions with Mongo aggregation pipelines
When to use: You need counts, funnels, or top-N lists and don't want to learn $group/$lookup syntax.
Prerequisites
- Read-only connection string — Atlas: create a DB user with
readAnyDatabase. Self-hosted: user withreadrole on relevant DBs.
Flow
-
Discover collectionsList databases, then for
app_prodlist all collections and their approximate document counts.✓ Copied→ Collection catalog -
Sample and infer schemaSample 20 docs from
usersandorders. Describe the fields and types you see.✓ Copied→ Per-collection schema description -
Run the actual aggregationHow many orders were placed per country in the last 30 days? Sort desc and limit 20.✓ Copied→ Results table with the pipeline used
Outcome: Business answers with the exact pipeline preserved for re-running.
Pitfalls
- Aggregations without indexes can scan huge collections — Always check
.explain()first and make sure a supporting index exists; otherwise add$matchnarrowly on an indexed field at the top of the pipeline