Ali2206 commited on
Commit
5110903
·
verified ·
1 Parent(s): 1b22be4

Update db/mongo.py

Browse files
Files changed (1) hide show
  1. db/mongo.py +16 -6
db/mongo.py CHANGED
@@ -1,15 +1,25 @@
1
  import os
2
  from motor.motor_asyncio import AsyncIOMotorClient
3
 
4
- # Get the MongoDB URI from Hugging Face secrets
5
  MONGO_URI = os.environ.get("MONGODB_URI")
6
 
7
- # Initialize the async MongoDB client
8
- client = AsyncIOMotorClient(MONGO_URI)
 
 
 
 
 
 
9
 
10
- # Automatically get the default database (from the URI)
11
- db = client.get_default_database()
 
12
 
13
- # Expose collections
14
  patients_collection = db["patients"]
15
  results_collection = db["patient_analysis_results"]
 
 
 
 
 
1
  import os
2
  from motor.motor_asyncio import AsyncIOMotorClient
3
 
 
4
  MONGO_URI = os.environ.get("MONGODB_URI")
5
 
6
+ # Extract database name from the URI manually
7
+ if not MONGO_URI:
8
+ raise ValueError("MONGODB_URI environment variable not set")
9
+
10
+ # Example: mongodb+srv://user:pass@host/mydatabase
11
+ db_name = MONGO_URI.rsplit("/", 1)[-1].split("?")[0]
12
+ if not db_name:
13
+ raise ValueError("No default database found in MONGODB_URI")
14
 
15
+ # Initialize client
16
+ client = AsyncIOMotorClient(MONGO_URI)
17
+ db = client[db_name] # use the extracted database
18
 
19
+ # Collections
20
  patients_collection = db["patients"]
21
  results_collection = db["patient_analysis_results"]
22
+
23
+ # Optional: export client or db if needed elsewhere
24
+ def get_mongo_client():
25
+ return client