Advertisement
Guest User

MongoDB singleton class

a guest
Jul 25th, 2021
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.04 KB | None | 0 0
  1. class MongodbConnection(metaclass=Singleton):
  2.     def __init__(self):
  3.         config = configparser.RawConfigParser()
  4.         config.readfp(open(r'singleton.txt'))
  5.         self.__MONGO_HOST = config.get('mongo_creds', 'MONGO_HOST')
  6.         self.__MONGO_USER = config.get('mongo_creds', 'MONGO_USER')
  7.         self.__MONGO_PORT = int(config.get('mongo_creds', 'MONGO_PORT'))
  8.         self.__MONGO_PASSWORD = config.get('mongo_creds', 'MONGO_PASSWORD')
  9.         self.__MONGO_DATABASE = config.get('mongo_creds', 'MONGO_DATABASE')
  10.  
  11.     def connect_to_mongodb(self):
  12.         self.client = MongoClient(host=self.__MONGO_HOST, port=self.__MONGO_PORT, username=self.__MONGO_USER, password=self.__MONGO_PASSWORD, authSource=self.__MONGO_DATABASE)
  13.         return self.client
  14.    
  15.     def execute_mongo_query_and_return_dataframe(self, input_query):
  16.         database = self.client[database_name]
  17.         # sample_query_mongo_shell = database[collection_name].find({})
  18.         return pd.DataFrame(list(input_query))
  19.  
  20.     def close_connection(self):
  21.         self.client.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement