from pymongo import MongoClient MONGODB_CONFIG = { 'host': 'localhost', 'port': 27017, 'dbname': 'local', 'collection': 'messenger' } class MongoDbHandler: def __init__(self, dbname, collection, host, port): self.host = host self.port = port self.dbname = dbname self.collection_name = collection self.client = None self.collection = None def connect(self): try: self.client = MongoClient(self.host, self.port) db = self.client[self.dbname] self.collection = db[self.collection_name] print("Connected to MongoDB") except Exception as e: self.collection = None def close_client(self): if self.client: self.client.close() self.collection = None print("MongoDB client closed.") def get_collection(self): return self.collection