PYTHON   28

db.py

Guest on 6th June 2021 05:18:29 PM

  1. # PyMongo
  2. from pymongo import MongoClient
  3. # Utils
  4. import os
  5.  
  6. db_host = os.environ.get('DB_HOST')
  7. db_port = os.environ.get('DB_PORT')
  8. db_user = os.environ.get('DB_USER')
  9. db_password = os.environ.get('DB_PASSWORD')
  10. db_name = os.environ.get('MONGO_INITDB_DATABASE')
  11. auth_mechanism = 'SCRAM-SHA-256'
  12.  
  13.  
  14. class DB():
  15.     def __init__(self, *args, **kwargs):
  16.         self.uri = 'mongodb://{db_user}:{db_password}@{db_host}:{db_port}/{db_name}?authMechanism={auth_mechanism}'.format(
  17.             db_user=db_user,
  18.             db_password=db_password,
  19.             db_host=db_host,
  20.             db_port=db_port,
  21.             db_name=db_name,
  22.             auth_mechanism=auth_mechanism,
  23.         )
  24.  
  25.     def clean_train(self):
  26.         client = MongoClient(self.uri)
  27.  
  28.         db = client.get_database()
  29.  
  30.         statements = db.statements
  31.  
  32.         x = statements.delete_many({})
  33.  
  34.         print('Records clean: {}'.format(x))

Raw Paste


Login or Register to edit or fork this paste. It's free.