-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.py
More file actions
92 lines (72 loc) · 3 KB
/
db.py
File metadata and controls
92 lines (72 loc) · 3 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
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
import pymongo
from bson.objectid import ObjectId
from pymongo import MongoClient
from pymongo import errors
import datetime
class Mongo:
def __init__(self, dbString, dbName):
self.client:MongoClient = MongoClient(dbString)
self.db:MongoClient = self.client[dbName]
def insert(self, block, collectionName):
try:
collection = self.db[collectionName]
print(f'len of the block :: {len(block)}')
if len(block) == 1:
return True, collection.insert_one(block[0]).inserted_id
else:
return True, collection.insert_many(block)
except Exception as e:
print("An exception ocurred ::", e)
return False, None
def remove(self, block, collectionName):
try:
collection = self.db[collectionName]
return True, collection.delete_one(block)
except Exception as e:
print("An exception ocurred ::", e)
return False, None
def remove_ById(self, docId, collectionName):
try:
collection = self.db[collectionName]
return True, collection.delete_one({"_id" : ObjectId(docId)})
except Exception as e:
print("An exception ocurred ::", e)
return False, None
def remove_all(self, block, collectionName):
try:
collection = self.db[collectionName]
return True, collection.delete_many(block)
except Exception as e:
print("An exception ocurred ::", e)
return False, None
def update_doc(self, block, block2, collectionName):
try:
collection = self.db[collectionName]
return True, collection.update_one(block, {"$set": block2})
except Exception as e:
print("An exception ocurred ::", e)
return False, None
pass
def update_docById(self, docId, block2, collectionName):
try:
collection = self.db[collectionName]
return True, collection.update_one({"_id" : ObjectId(docId)}, {"$set": block2})
except Exception as e:
print("An exception ocurred ::", e)
return False, None
pass
def get_doc(self, block, collectionName):
collection = self.db[collectionName]
return collection.find_one(block)
def get_docById(self, docId, collectionName):
collection = self.db[collectionName]
return collection.find_one({"_id" : ObjectId(docId)})
def get_docs(self, block, collectionName, sortVar = None):
collection = self.db[collectionName]
return collection.find(block) if sortVar == None else collection.find(block).sort(sortVar)
def isDocExists(self, block, collectionName):
ret = self.get_doc(block, collectionName)
return False if ret == None else True
def isDocExistsById(self, docId, collectionName):
ret = self.get_docById(docId, collectionName)
return False if ret == None else True