File size: 1,488 Bytes
211843b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pymongo
import json
import certifi
# Mongo client Debugging: 
# Tsl error :https://stackoverflow.com/questions/54484890/ssl-handshake-issue-with-pymongo-on-python3
# Ip address whilisting mongo atlas..
class ImportCSV:
    def __init__(self,database,maindb=0) -> None:
        ca = certifi.where()
        # This tells python to specifically send a tls certificate for the connection.
        if maindb == 0: # User Accounts Database
            client = pymongo.MongoClient(f"mongodb+srv://palondrome:[email protected]/roadmaptestdb?retryWrites=true&w=majority",tlsCAFile=ca)
            self.db = client[database]
        elif maindb == 1:# Question Paper 1 Database
            client = pymongo.MongoClient(f"mongodb+srv://palondrome2:[email protected]/chemistryqp?retryWrites=true&w=majority",tlsCAFile=ca)
            self.db = client[database]
        elif maindb == 2:# Question Paper 1 Database
            client = pymongo.MongoClient(f"mongodb+srv://palondrome3:[email protected]/?retryWrites=true&w=majority",tlsCAFile=ca)
            self.db = client[database]
    def load_data(self,collection_name,init_data):
        # Initialises collection 
        db_cm = self.db[collection_name]
        def load_n_insert(data):
            # Input is Dataframe
            data_json = json.loads(data.to_json(orient='records'))
            db_cm.insert_many(data_json)
        load_n_insert(init_data)