Swift-Query / tests /extract_data.py
Darshan Kumar R
Deploy to Hugging Face Spaces (Clean)
ed0b977
Raw History Blame Contribute Delete
2.38 kB
#!/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()