-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongodb_utils.py
58 lines (46 loc) · 1.62 KB
/
mongodb_utils.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
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
'''
Database access: You will write a few python functions organized in a
few files like mysql_utils.py, mongodb_utils.py, and neo4j_utils.py to
access the databases using their query languages via their network interfaces to support the widgets.
'''
from pymongo import MongoClient
'''
to run: python mongodb_utils.py
'''
def connect_to_mongodb():
# Connect to MongoDB server
try:
# Connect to MongoDB (adjust the URI as necessary)
client = MongoClient('localhost', 27017)
print("Connected to MongoDB")
return client
except Exception as e:
print(f"Error connecting to MongoDB: {e}")
return None
# client = MongoClient('localhost', 27017)
# # Access the 'academicworld' database
# db = client['academicworld']
# # List collections
# collections = db.list_collection_names()
# if collections:
# print("Collections in 'academicworld' database:")
# for collection in collections:
# print(collection)
# else:
# print("No collections found in 'academicworld' database.")
# return client
# def main():
# # Connect to MongoDB server
# client = MongoClient('localhost', 27017)
# # Access the 'academicworld' database
# db = client['academicworld']
# # List collections
# collections = db.list_collection_names()
# if collections:
# print("Collections in 'academicworld' database:")
# for collection in collections:
# print(collection)
# else:
# print("No collections found in 'academicworld' database.")
if __name__ == "__main__":
connect_to_mongodb()