-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
67 lines (42 loc) · 1.73 KB
/
db.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
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 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
'''x:Mongo = Mongo('mongodb://localhost:27017/', 'test')
block = {
"_id" : "5fdee8e175152ddc9e49a509"
}
ret = x.get_docs({}, 'hello')
for item in ret:
print(item.get('name'))'''