MongoDB Tutorial 0/120 lessons ~6 min read Lesson 58

    MongoDB Analytics

    MongoDB Analytics means running aggregations on operational data to power dashboards, BI tools, and ML pipelines — without ETL'ing into a separate warehouse first.

    Course progress0%
    Focus
    5 guided sections
    Practice signal
    Examples included
    Career prep
    Foundation builder

    Introduction

    MongoDB Analytics means running aggregations on operational data to power dashboards, BI tools, and ML pipelines — without ETL'ing into a separate warehouse first. With $group, $bucket, $facet and time-series collections, MongoDB can serve sub-second analytics on hundreds of millions of documents.

    For larger workloads, MongoDB Atlas offers Online Archive (cold tier on S3), Atlas Data Federation (query S3 + cluster in one pipeline) and Atlas SQL (BI tool connectivity). Most teams start with aggregations against the operational cluster and graduate to dedicated analytics nodes only when read volume threatens transactional latency.

    Understanding the topic

    Three tiers of MongoDB analytics:

    • Tier 1 — In-cluster aggregations. Direct $group / $facet pipelines on live data. Best for dashboards under 50ms.
    • Tier 2 — Analytics nodes. Hidden secondary members tagged nodeType: ANALYTICS; heavy reports run there without touching primaries.
    • Tier 3 — Atlas Data Lake / Federation. Federated SQL over Atlas + S3 cold storage; warehouse-class workloads.
    • Materialized views$merge stage writes pre-aggregated rollups every hour for instant dashboards.
    • Time-series collections — purpose-built storage (10×+ compression) for metrics, IoT and logs.

    Informative example

    Daily revenue dashboard pipeline — typical analytics workload:

    js
    db.orders.aggregate([
    { $match: { status: "paid", createdAt: { $gte: ISODate("2025-01-01") } } },
    { $group: {
    _id: { day: { $dateTrunc: { date: "$createdAt", unit: "day" } }, country: "$country" },
    revenue: { $sum: "$total" },
    orders: { $sum: 1 },
    avgBasket: { $avg: "$total" }
    } },
    { $sort: { "_id.day": 1 } },
    // Persist into a rollup collection — dashboard queries this instead of orders
    { $merge: { into: "rev_daily", on: "_id", whenMatched: "replace" } }
    ]);

    Real-world use

    Bosch streams 1M+ IoT events per second into time-series collections and powers their fleet dashboard with $merge rollups. Forbes uses analytics nodes so editorial dashboards never slow down the public CMS reads.

    Best practices

    • Push analytics reads to analytics-tagged secondaries via readPreference: { mode: "secondary", tags: [{ nodeType: "ANALYTICS" }] }.
    • Pre-aggregate with $merge every hour — dashboards should query rollups, not raw events.
    • Use time-series collections for any append-only metric stream — automatic bucketing saves 70%+ storage.
    • Add a covering index for every $match + $sort in your pipeline.
    Ready to mark this lesson complete?Track your journey across the entire course.