Answer business questions with Mongo aggregation pipelines
Quand l'utiliser : You need counts, funnels, or top-N lists and don't want to learn $group/$lookup syntax.
Prérequis
- Read-only connection string — Atlas: create a DB user with
readAnyDatabase. Self-hosted: user withreadrole on relevant DBs.
Déroulement
-
Discover collectionsList databases, then for
app_prodlist all collections and their approximate document counts.✓ Copié→ Collection catalog -
Sample and infer schemaSample 20 docs from
usersandorders. Describe the fields and types you see.✓ Copié→ Per-collection schema description -
Run the actual aggregationHow many orders were placed per country in the last 30 days? Sort desc and limit 20.✓ Copié→ Results table with the pipeline used
Résultat : Business answers with the exact pipeline preserved for re-running.
Pièges
- 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