demo_obsei / connect_mongo.py
kltn20133118's picture
Upload 4 files
c85c606 verified
raw
history blame contribute delete
555 Bytes
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