forked from yuwang1028/694_Team15_DBMS_2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connect_nonrelational.py
33 lines (30 loc) · 1.09 KB
/
connect_nonrelational.py
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
import warnings
from utils import connect_to_mongodb
warnings.filterwarnings('ignore')
def test_mongodb_connection():
print("Testing MongoDB connection...")
client = connect_to_mongodb()
if client:
print("Successfully connected to MongoDB.")
return client
else:
print("Failed to connect to MongoDB.")
return None
if __name__ == "__main__":
client = test_mongodb_connection()
if client:
try:
# Access the database and collection
db = client['twitter']
collection = db['nonrelational']
# Fetch the first 2 documents from the collection
documents = collection.find().limit(2)
print("First few documents from the collection 'nonrelational':")
for doc in documents:
print(doc)
except Exception as e:
print("Error fetching documents: ", e)
finally:
client.close() # It's important to close the connection after testing
else:
print("Unable to perform database operations due to failed connection.")