Spaces:
Sleeping
Sleeping
from pymongo import MongoClient | |
from pymongo.server_api import ServerApi | |
def connect_to_mongo(db_name: str, collection_name: str): | |
uri = "mongodb+srv://kltn20133118:XEYSVzYvEwsp5Cvo@cluster0.nnsw9.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0" | |
try: | |
client = MongoClient(uri, server_api=ServerApi('1')) | |
db = client[db_name] | |
collection = db[collection_name] | |
return collection | |
except Exception as e: | |
print("Lỗi kết nối MongoDB:", e) | |
return None |