Home Audience Developers MongoDB: The Popular Database for the Internet of Things

MongoDB: The Popular Database for the Internet of Things

0
377
MongoDB

By leveraging database for IoT, organisations can efficiently store, process, analyse, and derive insights from the massive volumes of data generated by IoT devices. We take a look at how to implement the MongoDB database in an IoT environment.

The Internet of Things (IoT) generates vast amounts of data from various sources, including sensors, devices, and applications. Databases provide a structured and organised way to store this data.

IoT applications often need to access specific subsets of data quickly and efficiently. Databases offer indexing and querying mechanisms that optimise data retrieval performance. They enable developers to define indexes on particular fields or attributes, improving the speed of data retrieval operations and reducing the overall latency in accessing IoT data.

Thus, by leveraging databases in IoT environments, organisations can effectively manage and harness the vast amounts of data generated by IoT devices. Databases provide data storage, processing, integration, security, scalability, and historical analysis capabilities that are essential for unlocking the full potential of IoT applications and deriving actionable insights from IoT data. Figure 1 shows the various sources of data generation in the IoT environment.

Data generation at different applications of IoT
Figure 1: Data generation at different applications of IoT

Comparison of MongoDB, RethinkDB, SQLite, and Apache Cassandra

Popular databases used in IoT applications are MongoDB, RethinkDB, SQLite, and Apache Cassandra. The choice between these depends on specific project requirements. MongoDB and RethinkDB are document databases suitable for handling diverse and evolving data, with MongoDB having broader industry adoption. SQLite is a lightweight embedded database for local storage, while Cassandra excels in high scalability, availability, and distributed architectures. Table 1 compares these four databases critically.

Table 1: MongoDB vs RethinkDB vs SQLite vs Apache Cassandra

Criteria MongoDB RethinkDB SQLite Apache Cassandra
Latest version MongoDB 6.0 RethinkDB 2.4.2 SQLite 3.42.0  Apache Cassandra 4.1
Released in 2009 2012 2000 2008
Supportable programming languages C++, Python, JavaScript C++, Python, JavaScript Python, Java, C#, Ruby, PHP C++, Java, Python, Go, Node.JS
Database model Supports JSON Supports JSON Follows a relational data model and supports SQL queries. Follows NoSQL database
Key features Autosharing, indexing Distributed database, automatic sharding, and replication Serverless, flexible Easy data distribution, fast linear-scale performance
Advantages of using in IoT applications Document-oriented, flexible schema Adaptable query language, plug-and-play functions Enables small memory footprint, no dependencies Fault-tolerant, scalable

Installation of MongoDB on Windows

MongoDB 6.0 is the latest community edition version and can be downloaded on the official URL of MongoDB. To install MongoDB on Windows, a 64-bit system is required. The steps are given below.

Step 1: Download the MongoDB community .msi installer zip package and unpack it in your system directory.

Step 2: Next, navigate to the download directory; there should be an installer file called .msi. From the admin user, the installation wizard will be launched by double-clicking the .msi file, as shown in Figure 2.

Setup wizard of MongoDB installation
Figure 2: Setup wizard of MongoDB installation

Step 3: After running the .msi installation file of MongoDB, choose the complete setup type. Configure and start MongoDB as a Windows service during the installation, and the MongoDB service will start on successful installation, as shown in Figure 3.Step 4: Now, the user can edit the system environment variable from the ‘program files’ of the C drive.

Step 5: The MongoDB services can now be accessed at the local host, as shown in Figure 4.

Access the MongoDB services from the local host
Figure 4: Access the MongoDB services from the local host

Popular commands used in MongoDB for IoT

MongoDB offers a rich set of commands that can be useful for IoT applications. Table 2 lists some popular MongoDB commands commonly used in IoT scenarios.

Table 2: List of MongoDB commands used in IoT

Command Description
db.createCollection (“sensors”) Creates a collection of ‘sensors’ to store data
db.insertOne Adds a document to a collection
db.insertMany Adds multiple files to a collection
db.sensors.find ({ sensorId: s }) Retrieves sensor readings where the sensor ID is ‘s’
db.aggregate Performs aggregation operations on the data
db.updateOne Updates a filter-matched document
db.deleteOne Deletes a filter-matched document
db.deleteMany Deletes multiple filter-matched documents

Programming MongoDB using Python

To work with MongoDB in Python, you can use the PyMongo library, the official MongoDB driver for Python. PyMongo provides a simple and intuitive API for interacting with MongoDB from your Python code.

Step 1: Install PyMongo. PyMongo can be set up with the help of pip, Python’s package manager. This can be done using the following command:

$ pip install pymongo

Step 2: The PyMongo package can now be imported by running the following command:

$ import pymongo

Step 3: To work with MongoClient, we need to connect to the default host and port using the following command:

$ client = pymongo.MongoClient(“mongodb://localhost:27017/”)

Step 4: MongoDB allows multiple databases to coexist on a single server. Databases in PyMongo are accessed via MongoClient instances with dictionary-style access:

$ db = client[“mydatabase”]

Step 5: Now, access a collection within the database:

$ collection = db[“yourcollection”]

A collection in MongoDB is a set of related documents that functions similar to a table in a traditional database. Python’s method for accessing collections is identical to that used for databases.

Step 6: In this step, you can insert a document into a collection. Data in MongoDB is represented (and stored) using JSON-style documents. In PyMongo, we use dictionaries to represent documents. As an example, the following dictionary inserts a document in a MongoDB collection:

$document = {“Name”: “OSFY”, “Month”: 07}
$ collection.insert_one(document)

MongoDB is a NoSQL database widely used because of its document-oriented design. Built with horizontal scalability across multiple servers, it can easily handle massive amounts of data. Industries using the Internet of Things often need to process and analyse streaming data in real-time. MongoDB’s real-time data processing, querying, and aggregation capabilities facilitate prompt analysis and decision-making.

NO COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here