Data Storage

Since our application is hosted on an SSH server, much of the data also resides on the server in a MongoDB database. MongoDB is an ideal choice due to its flexibility and scalability, particularly for handling unstructured or semi-structured data. Below are the steps for setting up a MongoDB database on an SSH server, creating a database and collections, and transferring files into the database.

1. Setting Up MongoDB on an SSH Server

To set up MongoDB on a remote SSH server:


sudo apt update
sudo apt install mongodb

Ensure MongoDB runs as service:


sudo systemctl start mongodb
sudo systemctl enable mongodb

2. Creating a Database and Collections

After installing MongoDB, log in to the MongoDB shell:


mongo

Switch to or create a new database:


 use your_database_name  

Collections are created implicitly when you insert the first document:

    
db.createCollection("your_collection_name")

    

To move files into your MongoDB database, follow these steps:<br>

1. Ensure the files are in JSON or CSV format, which MongoDB supports for import.

2. Use scp to transfer files to the server: 


  scp your_file.json username@your-server-ip:/target/directory  

3. Use the mongoimport tool to load files into a collection: <br>


 mongoimport --db your_database_name --collection your_collection_name --file /target/directory/your_file.json --jsonArray   

If importing CSV files, include the –type csv and –headerline options.

MongoDB databases and collections can be accessed through Python using the PyMongo library, a powerful tool for interacting with MongoDB. After installing PyMongo, a connection to the database is established using the MongoClient class, which requires the MongoDB URI. Once connected, the desired database can be accessed as an attribute or by key indexing the MongoClient object. Similarly, collections within the database are retrieved as attributes or keys of the database object. CRUD (Create, Read, Update, Delete) operations can then be performed using methods like insert_one, find, update_one, and delete_one, among others. PyMongo also allows for the use of advanced queries, indexing, and aggregation pipelines, making it suitable for both simple and complex data operations. Proper authentication and error handling are essential for secure and efficient database access.

    
from pymongo import MongoClient

# Connect to MongoDB
client = MongoClient("mongodb://localhost:27017/")  # Replace with your MongoDB URI

# Access the database
db = client["example_database"]

# Access a collection
collection = db["example_collection"]

# Insert a document into the collection
document = {"name": "Alice", "age": 30, "city": "New York"}
insert_result = collection.insert_one(document)
print(f"Document inserted with ID: {insert_result.inserted_id}")

# Find a document in the collection
query = {"name": "Alice"}
result = collection.find_one(query)
print("Found document:", result)

# Update a document in the collection
update_query = {"name": "Alice"}
new_values = {"$set": {"age": 31}}
update_result = collection.update_one(update_query, new_values)
print(f"Documents updated: {update_result.modified_count}")

# Delete a document from the collection
delete_query = {"name": "Alice"}
delete_result = collection.delete_one(delete_query)
print(f"Documents deleted: {delete_result.deleted_count}")

# Close the connection
client.close()