forked from Code-Institute-Org/gitpod-full-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo_project.py
152 lines (120 loc) · 3.41 KB
/
mongo_project.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import os
import pymongo
if os.path.exists("env.py"):
import env
MONGO_URI = os.environ.get("MONGO_URI")
DATABASE = "myFirstDB"
COLLECTION = "celebrities"
def mongo_connect(url):
try:
conn = pymongo.MongoClient(url)
print("Mongo is connected")
return conn
except pymongo.errors.ConnectionFailure as e:
print("Could not connect to MongoDB: %s") % e
def show_menu():
print("")
print("1. Add a record")
print("2. Find a record by name")
print("3. Edit a record")
print("4. Delete a record")
print("5. Exit")
option = input("Enter option: ")
return option
def get_record():
print("")
first = input("Enter first name > ")
last = input("Enter last name > ")
try:
doc = coll.find_one({"first": first.lower(), "last": last.lower()})
except:
print("Error accessing the database")
if not doc:
print("")
print("Error! No results found.")
return doc
def add_record():
print("")
first = input("Enter first name > ")
last = input("Enter last name > ")
dob = input("Enter date of birth > ")
gender = input("Enter gender > ")
hair_color = input("Enter hair color > ")
occupation = input("Enter occupation > ")
nationality = input("Enter nationality > ")
new_doc = {
"first": first.lower(),
"last": last.lower(),
"dob": dob,
"gender": gender,
"hair_color": hair_color,
"occupation": occupation,
"nationality": nationality
}
try:
coll.insert(new_doc)
print("")
print("Document inserted")
except:
print("Error accessing the database")
def find_record():
doc = get_record()
if doc:
print("")
for k,v in doc.items():
if k != "_id":
print(k.capitalize() + ": " + v.capitalize())
def edit_record():
doc = get_record()
if doc:
update_doc = {}
print("")
for k, v in doc.items():
if k != "_id":
update_doc[k] = input(k.capitalize() + " [" + v + "] > ")
if update_doc[k] == "":
update_doc[k] = v
try:
coll.update_one(doc, {"$set": update_doc})
print("")
print("Document updated")
except:
print("Error accessing the database")
def delete_record():
doc = get_record()
if doc:
print("")
for k, v in doc.items():
if k != "_id":
print(k.capitalize() + ": " + v.capitalize())
print("")
confirmation = input("Is this the document you want to delete?\nY or N > ")
print("")
if confirmation.lower() == "y":
try:
coll.remove(doc)
print("Document deleted!")
except:
print("Error accessing the database")
else:
print("Document not deleted")
def main_loop():
while True:
option = show_menu()
if option == "1":
add_record()
elif option == "2":
find_record()
elif option == "3":
edit_record()
elif option == "4":
delete_record()
elif option == "5":
conn.close()
break
else:
print("Invalid option")
print("")
conn = mongo_connect(MONGO_URI)
coll = conn[DATABASE][COLLECTION]
main_loop()