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

    Analytics Dashboards

    Analytics dashboards — for revenue, product usage, SLOs, marketing funnels — are the third-most-common workload on MongoDB.

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

    Introduction

    Analytics dashboards — for revenue, product usage, SLOs, marketing funnels — are the third-most-common workload on MongoDB. With the aggregation framework, $facet, time-series collections and Atlas Charts, you can render rich dashboards directly off operational data.

    This lesson shows the shape of a real analytics dashboard pipeline and the operational tricks that keep it sub-second: pre-aggregation with $merge, analytics-tagged secondaries, and time-series buckets.

    Understanding the topic

    The dashboard performance stack:

    • $facet — multiple aggregations in one round-trip; one query renders the whole dashboard.
    • $merge into rollup collection — recompute every 5 min; dashboards query rollups, not raw events.
    • Time-series collections for metrics — automatic bucketing & 10×+ compression.
    • Analytics secondaries — heavy reads never touch the primary.
    • Atlas Charts — built-in BI tool, no extra license, embeds via signed URL.

    Informative example

    Dashboard pipeline — KPIs, time-series, breakdowns in one $facet:

    js
    db.events.aggregate([
    { $match: { tenantId, ts: { $gte: from, $lt: to } } },
    { $facet: {
    kpis: [
    { $group: { _id: null,
    users: { $addToSet: "$userId" },
    events: { $sum: 1 },
    revenue: { $sum: "$amount" } } }
    ],
    perDay: [
    { $group: { _id: { $dateTrunc: { date: "$ts", unit: "day" } },
    count: { $sum: 1 }, revenue: { $sum: "$amount" } } },
    { $sort: { _id: 1 } }
    ],
    topCountries: [
    { $group: { _id: "$country", n: { $sum: 1 } } },
    { $sort: { n: -1 } }, { $limit: 10 }
    ]
    } }
    ], { readPreference: { mode: "secondary", tags: [{ nodeType: "ANALYTICS" }] } });

    Real-world use

    Forbes' editorial dashboard, Bosch's IoT fleet view, and many fintech "deal of the day" panels run on this exact pattern — $facet + analytics secondaries + 5-minute rollups.

    Best practices

    • Pre-aggregate to a rollup collection on a cron — dashboards must not run heavy pipelines on every refresh.
    • Pin BI traffic to analytics-tagged secondaries with readPreference + tags.
    • Time-series collections for metrics; never store raw points in a regular collection.
    • Cap dashboard query maxTimeMS so one slow query cannot starve the cluster.
    Ready to mark this lesson complete?Track your journey across the entire course.