This commit is contained in:
andrei-mihnea-cerbu
2024-01-12 22:19:19 +02:00
parent 37b2c068b8
commit e65b05b451
59 changed files with 349 additions and 190 deletions
@@ -0,0 +1,36 @@
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