-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataLoad.py
50 lines (37 loc) · 1.4 KB
/
DataLoad.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
#!/usr/bin/env python
# coding: utf-8
# In[5]:
from pymongo import MongoClient
import pandas as pd
class MongoDB:
def __init__(self, user, password, host, db_name ,port='27017', authSource='admin'):
self.user = user
self.password = password
self.host = host
self.port = port
self.db_name = db_name
self.authSource = authSource
self.uri = 'mongodb://' + self.user + ':' + self.password + '@'+ self.host + ':' + self.port + '/' + self.db_name + '?authSource=' + self.authSource
try:
self.client = MongoClient(self.uri)
self.db = self.client[self.db_name]
print('MongoDB Connection Successful. CHEERS!!!')
except Exception as e:
print('Connection Unsuccessful!! ERROR!!')
print(e)
def insert_into_db(self, data, collection):
if isinstance(data, pd.DataFrame):
try:
self.db[collection].insert_many(data.to_dict('records'))
print('Data Inserted Successfully')
except Exception as e:
print('OOPS!! Some ERROR Occurred')
print(e)
else:
try:
self.db[collection].insert_many(data)
print('Data Inserted Successfully')
except Exception as e:
print('OOPS!! Some ERROR Occurred')
print(e)
# In[ ]: