Spaces:
Runtime error
Runtime error
File size: 2,375 Bytes
ed0b977 | 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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | #!/usr/bin/env python3
import sys
import os
from flask import Flask
from flask_pymongo import PyMongo
from dotenv import load_dotenv
# Ensure the current directory is in the python path
sys.path.append(os.getcwd())
load_dotenv()
def extract_and_print_all():
"""
Connect to the MongoDB and print all records for all collections.
"""
app = Flask(__name__)
app.config["MONGO_URI"] = os.environ.get("MONGO_URI")
mongo = PyMongo(app)
collections = [
"admin",
"administrativeassistant",
"patient",
"doctor",
"medicalhistory",
"consultation",
"testreport",
"treatment",
"insurance",
"billing",
"chatsession",
"chatmessage"
]
print("=== START OF DATABASE DUMP (MongoDB) ===")
with app.app_context():
# Try getting db from uri or fallback to 'swiftquery'
try:
db = mongo.db
except:
db = None
if db is None:
print(" mongo.db is None. Trying mongo.cx['swiftquery']...")
db = mongo.cx['swiftquery']
for col_name in collections:
print(f"\n--- Collection: {col_name} ---")
try:
# Count
count = db[col_name].count_documents({})
print(f" Total Documents: {count}")
# Fetch all
records = list(mongo.db[col_name].find())
if not records:
print(" (No records found)")
else:
for i, record in enumerate(records):
# Convert ObjectId to str for printing if needed, though default str() works ok usually
# Handle binary data for display
display_record = record.copy()
if 'file_data' in display_record and display_record['file_data']:
display_record['file_data'] = f"<Binary Data: {len(display_record['file_data'])} bytes>"
print(f" Record {i+1}: {display_record}")
except Exception as e:
print(f" Error querying collection: {e}")
print("\n=== END OF DATABASE DUMP ===")
if __name__ == "__main__":
extract_and_print_all()
|