-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmongodb.py
48 lines (42 loc) · 1.44 KB
/
mongodb.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
import pymongo
class mongodbconnection:
# This class shall be used for mongoDB operation
def __init__(self, username, password):
try:
self.username = username
self.password = password
self.url = f"mongodb+srv://{self.username}:{self.password}@cluster0.bisvs.mongodb.net/?retryWrites=true&w=majority"
except Exception as e:
raise e
def getMongoClient(self):
# It creates connection with the database
try:
client = pymongo.MongoClient(self.url)
return client
except Exception as e:
raise e
def getDatabase(self, dbName):
# Gives database
try:
client = self.getMongoClient()
database = client[dbName]
return database
except Exception as e:
raise e
def getCollection(self, dbName, collectionName):
# Gives collection of a database
try:
database = self.getDatabase(dbName)
collection = database[collectionName]
return collection
except Exception as e:
raise e
def getdata(self, dbName, collectionName):
try:
database = self.getDatabase(dbName)
collection = database[collectionName]
all_records = collection.find()
list_cursor = list(all_records)
return list_cursor
except Exception as e:
raise e