forked from winhtut/MongoDB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDB_Handler-2.py
66 lines (49 loc) · 2.06 KB
/
DB_Handler-2.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
import pymongo
import json
connection=pymongo.MongoClient("localhost",27017)
database = connection["my_db1"]
collection_nameAndPw = database["nameAndPw"]
myquery ={"hint":"unknowuser"}
class ToDelete:
def __init__(self,myquery):
self.toDelete= myquery
def delete(self):
try:
data = collection_nameAndPw.delete_one(self.toDelete)
print("Deleted Documents:", data.deleted_count)
except Exception as err:
print(err)
def print_data(self):
try:
all_data = collection_nameAndPw.find()
for i in all_data:
print(i)
except Exception as err:
print(err)
def toSearch(self,userNameToSerach :str):
try:
toSearchQuery = {"profileName":userNameToSerach}
data =collection_nameAndPw.find(toSearchQuery)
print("Type of return data is:",type(data))
return userNameToSerach
except Exception as err:
print(err)
def toUpate(self,profileName,NewName):
try:
toUpdate = {'profileName':profileName}
name = {"$set":{'profileName':NewName}}
collection_nameAndPw.update_many(toUpdate,name)
print("Data Successfully updated!")
except Exception as err:
print("Data Note Successfully updated!")
print(err)
if __name__ =="__main__":
userNameToSerach = input("Please Enter a profile name to Update:")
NewName = input("Enter New Name of Profile:")
process=ToDelete(myquery)
#data = process.toSearch(userNameToSerach)
#print(data)
process.print_data()
process.toUpate(userNameToSerach,NewName)
print("After Update")
process.print_data()