-
Notifications
You must be signed in to change notification settings - Fork 9
/
database.py
63 lines (43 loc) · 1.2 KB
/
database.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
# -*- coding: utf-8 -*-
"""
Created by: Shaheen Syed
Date: August 2018
Class that handles all the database actions
"""
# packages and modules
from pymongo import MongoClient
import time, logging, sys
from bson.objectid import ObjectId
class MongoDatabase:
def __init__(self, client = 'LDA-github-2'):
self.client = MongoClient()
self.db = self.client[client]
def read_collection(self, collection):
"""
Read all documents in a certain collection
"""
try:
return self.db[collection].find({}, no_cursor_timeout=True)
except Exception, e:
logging.error("[{}] : {}".format(sys._getframe().f_code.co_name,e))
exit(1)
def insert_one_to_collection(self, collection, doc):
"""
Insert one document to a collection
"""
try:
self.db[collection].insert_one(doc)
except Exception, e:
logging.error("[{}] : {}".format(sys._getframe().f_code.co_name,e))
exit(1)
def update_collection(self, collection, doc):
"""
Update document to a collection
"""
try:
self.db[collection].update({'_id' : ObjectId(doc['_id'])},
doc
,upsert = False)
except Exception, e:
logging.error("[{}] : {}".format(sys._getframe().f_code.co_name,e))
exit(1)