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
    12 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    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.

    Learning objective: Apply IoT Applications correctly in a TechLearningPRO MongoDB workload — with clear data-model choices, safe writes, and failure modes you can explain in review.

    Treat every example as production-shaped MongoDB: explicit projections, indexes that match the access path, and Atlas-safe defaults. Prefer mongosh / Node driver shapes used in SaaS backends.

    Purpose of this lesson

    Apply IoT Applications correctly in a TechLearningPRO MongoDB workload — with clear data-model choices, safe writes, and failure modes you can explain in review.

    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.

    Step-by-step explanation

    1. State when IoT Applications is the right MongoDB tool versus an alternative.
    2. Map the diagram boxes to driver, mongod, WiredTiger, and your collection.
    3. Run the smallest correct mongosh / driver example against a TechLearningPRO learners collection.
    4. Name two failure modes (missing index, bad shard key, unbounded scan) and how you detect them.

    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.

    Security implications

    MongoDB is a trust boundary. Drivers must not interpolate untrusted input into query operators.

    • Never build queries by concatenating user strings into operator objects.
    • Use least-privilege Atlas database users; apps should not have root.
    • Enable TLS and avoid exposing mongod on public networks.

    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.

    Common mistakes

    • Unbounded find({}) / aggregation without an early $match.
    • Missing indexes on hot equality/range predicates.
    • Embedding unbounded arrays that approach the 16MB document limit.

    Advanced interview questions

    Interview Prep

    Practice concise answers, then expand each card for the explanation.

    3 questions
    1BeginnerQuestionWhat is IoT Applications and when do you use it?+

    Answer

    Time-series collections turn MongoDB into a first-class IoT database.
    2IntermediateQuestionHow would you verify IoT Applications is working in production?+

    Answer

    Bucketing + compression yields ~10× storage savings vs. naive collections.
    3AdvancedQuestionWhat breaks if you misuse IoT Applications?+

    Answer

    Atlas Stream Processing + Online Archive complete the end-to-end pipeline.

    Hands-on exercise

    Hands-on drill:

    • Implement the smallest correct example for IoT Applications on a learners/orders-like collection.
    • Capture explain('executionStats') and confirm an index was used when expected.

    Summary

    IoT Applications is a production MongoDB skill: model for your read path, prove it with explain(), and ship with indexes and least-privilege roles.

    Ready to mark this lesson complete?Track your journey across the entire course.