Explore Developer Center's New Chatbot! MongoDB AI Chatbot can be accessed at the top of your navigation to answer all your MongoDB questions.

Join us at AWS re:Invent 2024! Learn how to use MongoDB for AI use cases.
MongoDB Developer
MongoDB
plus
Sign in to follow topics
MongoDB Developer Centerchevron-right
Developer Topicschevron-right
Productschevron-right
MongoDBchevron-right

Capture IoT Data With MongoDB in 5 Minutes

Maxime Beugnet2 min read • Published Dec 21, 2021 • Updated Sep 09, 2024
MongoDBJavaScript
Facebook Icontwitter iconlinkedin icon
Rate this article
star-empty
star-empty
star-empty
star-empty
star-empty
Some features mentioned below will be deprecated on Sep. 30, 2025. Learn more.
Please note: This article discusses Stitch. Stitch is now MongoDB Realm. All the same features and functionality, now with a new name. Learn more here. We will be updating this article in due course.
Capturing IoT (Internet of Things) data is a complex task for 2 main reasons:
  • We have to deal with a huge amount of data so we need a rock solid architecture.
  • While keeping a bulletproof security level.
First, let's have a look at a standard IoT capture architecture:
standard IoT capture architecture.
standard IoT capture architecture.
On the left, we have our sensors. Let's assume they can push data every second over TCP using a POST and let's suppose we have a million of them. We need an architecture capable to handle a million queries per seconds and able to resist any kind of network or hardware failure. TCP queries need to be distributed evenly to the application servers using load balancers and finally, the application servers are able to push the data to our multiple Mongos routers from our MongoDB Sharded Cluster.
As you can see, this architecture is relatively complex to install. We need to:
  • buy and maintain a lot of servers,
  • make security updates on a regular basis of the Operating Systems and applications,
  • have an auto-scaling capability (reduce maintenance cost & enable automatic failover).
This kind of architecture is expensive and maintenance cost can be quite high as well.
Now let's solve this same problem with MongoDB Stitch!
MongoDB Stitch architecture
MongoDB Stitch architecture
Once you have created a MongoDB Atlas cluster, you can attach a MongoDB Stitch application to it and then create an HTTP Service containing the following code:
1exports = function(payload, response) {
2 const mongodb = context.services.get("mongodb-atlas");
3 const sensors = mongodb.db("stitch").collection("sensors");
4 var body = EJSON.parse(payload.body.text());
5 body.createdAt = new Date();
6 sensors.insertOne(body)
7 .then(result => {
8 response.setStatusCode(201);
9 });
10};
And that's it! That's all we need! Our HTTP POST service can be reached directly by the sensors from the webhook provided by MongoDB Stitch like so:
1curl -H "Content-Type: application/json" -d '{"temp":22.4}' https://webhooks.mongodb-stitch.com/api/client/v2.0/app/stitchtapp-abcde/service/sensors/incoming_webhook/post_sensor?secret=test
Because MongoDB Stitch is capable of scaling automatically according to demand, you no longer have to take care of infrastructure or handling failovers.

Next Step

Thanks for taking the time to read my post. I hope you found it useful and interesting.
If you are looking for a very simple way to get started with MongoDB, you can do that in just 5 clicks on our MongoDB Atlas database service in the cloud.
You can also try MongoDB Stitch for free and discover how the billing works.
If you want to query your data sitting in MongoDB Atlas using MongoDB Stitch, I recommend this article from Michael Lynn.

Facebook Icontwitter iconlinkedin icon
Rate this article
star-empty
star-empty
star-empty
star-empty
star-empty
Related
Tutorial

Currency Analysis with Time Series Collections #1 — Generating Candlestick Charts Data


May 31, 2023 | 6 min read
Article

Real-Time Card Fraud Solution Accelerator with MongoDB and Databricks


Jul 11, 2023 | 7 min read
Quickstart

Getting Started with MongoDB and Java - CRUD Operations Tutorial


Mar 01, 2024 | 24 min read
Article

8 Best Practices for Building FastAPI and MongoDB Applications


Apr 23, 2024 | 8 min read
Table of Contents
  • Next Step