-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_mongo.py
More file actions
67 lines (55 loc) · 1.79 KB
/
test_mongo.py
File metadata and controls
67 lines (55 loc) · 1.79 KB
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
#!/usr/bin/env python3
import os
import sys
from dotenv import load_dotenv
print("Loading environment...")
# Load environment variables
load_dotenv()
print("Importing pymongo...")
try:
from pymongo import MongoClient
print("✅ PyMongo imported successfully")
except ImportError as e:
print(f"❌ Failed to import pymongo: {e}")
sys.exit(1)
# Test MongoDB connection
def test_mongo_connection():
mongodb_uri = os.getenv("MONGODB_URI")
if not mongodb_uri:
print("ERROR: MONGODB_URI not found")
return False
print(f"Testing connection to: {mongodb_uri[:50]}...")
try:
# Test with direct pymongo client with timeout
print("Creating MongoClient...")
client = MongoClient(mongodb_uri, serverSelectionTimeoutMS=5000)
print("Getting default database...")
# Test connection
db = client.get_default_database()
print(f"Default database name: {db.name}")
print("Testing ping...")
# Test ping
result = db.command("ping")
print(f"Ping result: {result}")
print("Listing collections...")
# List collections
collections = db.list_collection_names()
print(f"Existing collections: {collections}")
print("✅ MongoDB connection successful!")
return True
except Exception as e:
print(f"❌ MongoDB connection failed: {e}")
print(f"Error type: {type(e)}")
import traceback
traceback.print_exc()
return False
finally:
try:
print("Closing client...")
client.close()
except:
pass
if __name__ == "__main__":
print("Starting MongoDB connection test...")
test_mongo_connection()
print("Test completed.")