Language: EN

como-instalar-mongodb-en-raspberry-pi

How to Install MongoDB on Raspberry Pi

MongoDB is a document-oriented NoSQL database that stores data in BSON (Binary JSON) format. This allows for great flexibility in data schema design, unlike traditional relational databases.

Some key features of MongoDB include:

  • Horizontal Scalability: Easily scales by adding more nodes to a cluster.
  • Dynamic Schema: Does not require a fixed schema for data, allowing each document in a collection to have a different structure.
  • Ad-Hoc Queries: Supports complex queries and data aggregations.
  • High Availability: Through replicas and cluster configurations, MongoDB offers high availability and fault tolerance.

These features make MongoDB an excellent choice for modern applications that handle large volumes of unstructured or semi-structured data.

How to Install MongoDB on Raspberry Pi

Setting Up the Repository

First, we need to ensure that our Raspberry Pi is up to date. We run the following commands to update the packages and the operating system:

sudo apt update
sudo apt upgrade

Next, to install MongoDB, which is not included in the standard Raspberry Pi OS repositories, we need to add the appropriate repository.

# Install necessary dependencies
sudo apt install -y gnupg

# Download and install the public key for the MongoDB repository
wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -

# Create a list file for MongoDB
echo "deb [arch=armhf] http://repo.mongodb.org/apt/debian buster 10gen" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list

Note: Version 5.0 is one of the versions compatible with Raspberry Pi. You can adjust according to the desired version.

Installing MongoDB

Once we have added the repository, we install MongoDB with the following steps:

First, we update the package list with the following command:

sudo apt update

Then, we proceed to install MongoDB by running:

sudo apt install -y mongodb

After completing the installation, we need to configure and enable MongoDB to run as a service.

We start the MongoDB service with:

# Ensure mongod config is picked up
sudo systemctl daemon-reload

# Tell systemd to run mongod on reboot
sudo systemctl enable mongod

# Start up mongod!
sudo systemctl start mongod

Finally, we check the status of the service to confirm that MongoDB is running correctly with:

sudo systemctl status mongod

We should see output indicating that the service is active and running.

Using MongoDB

Once installed and running, you can start using MongoDB to manage your databases. To access the MongoDB shell and begin interacting with the database, use the following command:

mongo

In the MongoDB shell, you can start creating databases, collections, and documents. Here are some basic commands to get started:

Create a Database

use myDatabase

Create a Collection and Insert a Document

db.myCollection.insertOne({ name: "Raspberry Pi", type: "Device" })

Query Documents

db.myCollection.find()

Using MongoDB with Python

Here is a basic example of how to use MongoDB in Python to insert and query data:

from pymongo import MongoClient

# Connect to the local MongoDB database
client = MongoClient('localhost', 27017)

# Access the "my_db" database
db = client.my_db

# Access the "my_collection" collection
collection = db.my_collection

# Insert a document
document = {"name": "Raspberry Pi", "category": "IoT"}
inserted_id = collection.insert_one(document).inserted_id

# Query all documents in the collection
for document in collection.find():
    print(document)

In this example,

  • We connect to MongoDB
  • Insert a document into a collection
  • Query all documents in that collection.