Answer business questions with Mongo aggregation pipelines
Когда использовать: You need counts, funnels, or top-N lists and don't want to learn $group/$lookup syntax.
Предварительные требования
- Read-only connection string — Atlas: create a DB user with
readAnyDatabase. Self-hosted: user withreadrole on relevant DBs.
Поток
-
Discover collectionsList databases, then for
app_prodlist all collections and their approximate document counts.✓ Скопировано→ Collection catalog -
Sample and infer schemaSample 20 docs from
usersandorders. Describe the fields and types you see.✓ Скопировано→ Per-collection schema description -
Run the actual aggregationHow many orders were placed per country in the last 30 days? Sort desc and limit 20.✓ Скопировано→ Results table with the pipeline used
Итог: Business answers with the exact pipeline preserved for re-running.
Подводные камни
- 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