-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmongoconnect.py
More file actions
51 lines (37 loc) Β· 1.49 KB
/
mongoconnect.py
File metadata and controls
51 lines (37 loc) Β· 1.49 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
import pymongo
class PyMongoClient():
def __init__(self, uri, db, col):
self.client = pymongo.MongoClient(uri)
self.db = self.client[db]
self.col = self.db[col]
def close(self):
self.client.close()
def getAll(self):
return self.col.find()
def getAllShort(self):
return self.col.find({}, {"_id":0, "id":1, "Nama" : 1, "Kategori":1, "Brand" : 1, "Harga" : 1})
def getAllByID(self,id_p):
return self.col.find({"id":id_p},{"_id" : 0})
def getPriceByID(self, id):
return self.col.find({"id":id},{"_id" : 0, "Harga" : 1})
def getDetailByID(self, id):
return self.col.find_one({"id":id},{"_id" : 0, "Harga" : 1, "Nama" : 1, "Brand":1})
def printAll(self):
myDict = self.getAll()
for record in myDict:
print("__________________________")
for key in record:
print(key," : ", record[key])
def printAllShort(self):
myDict = self.getAllShort()
for record in myDict:
print("__________________________")
for key in record:
print(key," : ", record[key])
def printPrice(self, id):
myDict = self.getPriceByID(id)
for record in myDict:
print("_________________________")
for key in record:
print(key," : ", record[key])
myclient = PyMongoClient("mongodb://localhost:27017/", "proyek-dmds", "products-final")