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

    IoT Applications

    IoT workloads — connected cars, smart meters, factory sensors, wearables — generate billions of small, time-ordered events per day.

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

    Introduction

    IoT workloads — connected cars, smart meters, factory sensors, wearables — generate billions of small, time-ordered events per day. MongoDB 5.0+ introduced time-series collections specifically for this pattern: automatic bucketing, columnar-like storage, 10×+ compression and purpose-built indexes.

    Bosch, Toyota, Iron Mountain and dozens of utilities run IoT platforms on MongoDB Atlas time-series for exactly this reason — one technology spans ingest, query and analytics.

    Understanding the topic

    IoT-shaped MongoDB design:

    • Time-series collection with timeField, metaField, granularity.
    • metaField = device id / tenant — drives bucketing locality.
    • Automatic bucketing — measurements grouped into compact buckets per device per time window.
    • Window stages ($setWindowFields) for moving averages and anomaly detection.
    • Online Archive moves cold data to S3 after N days — keeps hot working set tiny.
    • Atlas Stream Processing for real-time alerts off Kafka/MQTT streams.

    Informative example

    Create a time-series collection and ingest sensor events:

    js
    db.createCollection("readings", {
    timeseries: {
    timeField: "ts",
    metaField: "device", // { id, fleet, model }
    granularity: "seconds"
    },
    expireAfterSeconds: 60 * 60 * 24 * 90 // keep 90 days hot
    });
    db.readings.insertMany([
    { ts: new Date(), device: { id: "veh-42", fleet: "EU-1" }, speed: 88, temp: 22.4 },
    { ts: new Date(), device: { id: "veh-42", fleet: "EU-1" }, speed: 90, temp: 22.5 }
    ]);
    // 1-minute moving average per device using window stages
    db.readings.aggregate([
    { $match: { "device.fleet": "EU-1", ts: { $gte: from } } },
    { $setWindowFields: {
    partitionBy: "$device.id", sortBy: { ts: 1 },
    output: { tempAvg: { $avg: "$temp",
    window: { range: [-60, 0], unit: "second" } } } } }
    ]);

    Real-world use

    Bosch streams 1M+ events/sec from connected appliances into Atlas time-series; Toyota powers fleet telemetry; Vaillant runs predictive maintenance on heating systems — all on MongoDB time-series + Atlas Stream Processing.

    Best practices

    • Always pick the right granularity at creation — it cannot be changed later.
    • Use the metaField for the dimension you filter on most (device id, tenant).
    • Combine with Online Archive to keep hot data lean while preserving history on S3.
    • Apply $setWindowFields rather than self-joins for moving averages and rates.
    Ready to mark this lesson complete?Track your journey across the entire course.