-
Notifications
You must be signed in to change notification settings - Fork 1
/
database.py
32 lines (24 loc) · 973 Bytes
/
database.py
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
29
30
31
32
# Below code is available in Mongo-Connect
from pymongo.mongo_client import MongoClient
import os
# from os import environ as env
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
# Access the MongoDB URI using the environment variable
uname = os.getenv("MONGODB_USERNAME")
pwd = os.getenv("MONGODB_PASSWORD")
print("uname",uname, "pwd", pwd)
uri = f"mongodb+srv://{uname}:{pwd}@cluster0.5cm4d9a.mongodb.net/?retryWrites=true&w=majority"
# Create a new client to establish a connection to the MongoDB server
client = MongoClient(uri)
# Access or create a database within the MongoDB server
db = client.url_short_db
# Access or create a collection within the db database
collection_name = db["url_short_collection"]
# # Send a ping to confirm a successful connection
# try:
# client.admin.command('ping')
# print("Pinged your deployment. You successfully connected to MongoDB!")
# except Exception as e:
# print(e)