Spaces:
Runtime error
Runtime error
Update db/mongo.py
Browse files- 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 |
-
#
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
|
|
|
| 12 |
|
| 13 |
-
#
|
| 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
|