I don't think it's a server issue but I tried fixing server timing, I don't know where else to turn to at this point. I tried every option and stack overflow is my last option. Point out what I am doing wrong or any advice that could potentially resolve this issue. Below is my py file and the jupyter code below that is a ipynb file.
from pymongo import MongoClient
from bson.objectid import ObjectId
class AnimalShelter(object):
def __init__(self):
USER = 'private'
PASS = 'private'
HOST = 'private'
PORT = private
DB = 'aac'
COL = 'animals'
self.client = MongoClient('mongodb://%s:%s@%s:%d' % (USER, PASS, HOST, PORT), server_selection_timeout=60)
if not self.client:
raise Exception("Failed to connect to MongoDB database")
self.database = self.client['%s' % (DB)]
self.collection = self.database['%s' % (COL)]
def create(self, data):
if data is not None:
self.collection.insert_one(data)
return True
else:
raise Exception("Nothing to save because the data parameter is empty")
def read(self, query):
try:
cursor = self.collection.find(query)
return list(cursor)
except Exception as e:
print(f"Error reading documents: {e}")
return []
#def update(self, query, update_data):
#try:
#result = self.collection.update_many(query, {"$set": update_data})
#return result.modified_count
#except Exception as e:
#print(f"Error updating documents: {e}")
#return 0
#def delete(self, query):
#try:
#result = self.collection.delete_many(query)
#return result.deleted_count
#except Exception as e:
#print(f"Error deleting documents: {e}")
#return 0
For jupyter:
from CRUD_Modules import AnimalShelter
USER = 'private'
PASS = 'private'
HOST = 'private'
PORT = private
DB = 'aac'
COL = 'animals'
# Instantiate the AnimalShelter class
animal_shelter = AnimalShelter()
# Example data to insert
data_to_insert = {
'name': 'Shadow',
'species': 'Dog',
'age': 3
}
# Test create method
create_result = animal_shelter.create(data_to_insert)
print(f"create Result: {create_result}")
# Test read method
query_to_read = {'name': 'Shadow'}
read_result = animal_shelter.read(query_to_read)
print(f"Read Result: {read_result}")