File size: 555 Bytes
c85c606
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
from pymongo import MongoClient
from pymongo.server_api import ServerApi

def connect_to_mongo(db_name: str, collection_name: str):

    uri = "mongodb+srv://kltn20133118:[email protected]/?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