theexoticman / web3_project_scrapper

0 stars 0 forks source link

Create a python class interface for mongodb #2

Open theexoticman opened 3 months ago

theexoticman commented 3 months ago

create a python class boiler plate that is responsible for the create and delete a table, create an entry, update an entry, delete an entry.

It should consider that the mongodb service is hosted in another docker image in local network.

theexoticman commented 3 months ago

Sure! Here's a Python class boilerplate that handles creating and deleting a table, creating an entry, updating an entry, and deleting an entry in MongoDB. This code assumes you have the pymongo library installed and have a running MongoDB service hosted in a Docker container on the local network.

from pymongo import MongoClient

class MongoDBManager:
    def __init__(self, host, port, database_name, collection_name):
        self.client = MongoClient(host, port)
        self.database = self.client[database_name]
        self.collection = self.database[collection_name]

    def create_table(self):
        # Creates the collection (table) if it doesn't exist
        self.database.create_collection(self.collection.name)

    def delete_table(self):
        # Drops the collection (table)
        self.database.drop_collection(self.collection.name)

    def create_entry(self, entry):
        # Inserts a new entry into the collection
        self.collection.insert_one(entry)

    def update_entry(self, query, update):
        # Updates an existing entry in the collection based on a query
        self.collection.update_one(query, {"$set": update})

    def delete_entry(self, query):
        # Deletes an entry from the collection based on a query
        self.collection.delete_one(query)

To use this class, you need to provide the MongoDB connection details and the names of the database and collection you want to work with. Here's an example:

host = 'mongodb'  # Your MongoDB service hostname or IP address
port = 27017  # Your MongoDB service port number
database_name = 'mydatabase'  # Name of the database
collection_name = 'mycollection'  # Name of the collection

# Create an instance of MongoDBManager
manager = MongoDBManager(host, port, database_name, collection_name)

# Create the table (collection)
manager.create_table()

# Create an entry
entry = {"name": "John", "age": 30}  # Example entry data
manager.create_entry(entry)

# Update an entry
query = {"name": "John"}  # Example query
update = {"age": 31}  # Example update data
manager.update_entry(query, update)

# Delete an entry
query = {"name": "John"}  # Example query
manager.delete_entry(query)

# Delete the table (collection)
manager.delete_table()

Remember to replace the placeholder values with your actual MongoDB connection details, database name, and collection name.

Please let me know if you need any further assistance!

theexoticman commented 3 months ago

Sure! Here's a Python class boilerplate that performs CRUD operations on a MongoDB database, assuming it is hosted in another Docker image in the local network:

from pymongo import MongoClient

class CRUDService:
    def __init__(self, host='mongodb', port=27017, db_name='mydatabase'):
        self.client = MongoClient(host, port)
        self.db = self.client[db_name]

    def create_table(self, table_name):
        self.db.create_collection(table_name)

    def delete_table(self, table_name):
        self.db.drop_collection(table_name)

    def create_entry(self, table_name, data):
        collection = self.db[table_name]
        collection.insert_one(data)

    def update_entry(self, table_name, query, new_data):
        collection = self.db[table_name]
        collection.update_one(query, {"$set": new_data})

    def delete_entry(self, table_name, query):
        collection = self.db[table_name]
        collection.delete_one(query)

# Usage Example
if __name__ == "__main__":
    service = CRUDService()
    service.create_table("users")
    service.create_entry("users", {"name": "John Doe", "age": 25})
    service.update_entry("users", {"name": "John Doe"}, {"age": 30})
    service.delete_entry("users", {"name": "John Doe"})
    service.delete_table("users")

This boilerplate uses the PyMongo library to connect to the MongoDB server. The constructor of the CRUDService class initializes the connection with the MongoDB server, and the class methods perform the required CRUD operations.

You will need to install the pymongo package before running this code. You can install it using pip install pymongo.

Don't forget to update the host, port, and db_name parameters in the CRUDService constructor to match your MongoDB server configuration.

Let me know if you need any further assistance!

theexoticman commented 3 months ago

done