File size: 729 Bytes
420fa8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import certifi
import pymongo
import os

from dotenv import load_dotenv

load_dotenv()


def get_mongo_client(mongo_url):
    """Establish connection to the MongoDB."""
    if not mongo_url:
        print("MONGO_URI not set in environment variables")
    try:
        client = pymongo.MongoClient(mongo_url, tlsCAFile=certifi.where())
        print("Connection to MongoDB successful")
        return client
    except pymongo.errors.ConnectionFailure as e:
        print(f"Connection failed: {e}")
        return None


def get_mongo_url():
    username = os.environ["MONGO_USERNAME"]
    password = os.environ["MONGO_PW"]
    mongo_url = f"mongodb+srv://{username}:{password}@cluster0.62unmco.mongodb.net/"
    return mongo_url