mongodb.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. class MongoDB(Database):
  2. class Query:
  3. def __init__(self, table: str, select: Dict = None, condition: Dict = None, data: List[Dict] = None):
  4. self.table = table
  5. self.select = select
  6. self.condition = condition
  7. self.data = data
  8. def __init__(self, host: str = 'localhost', user: str = None, password: str = None, scheme: str = ''):
  9. super(MongoDB, self).__init__(host, user, password, scheme)
  10. self.conn = pymongo.MongoClient(f'mongodb://{host}:27017/{scheme or ""}')
  11. def write(self, query: "MongoDB.Query", single_insert: bool = True) -> bool:
  12. super(MongoDB, self).write(query)
  13. db = self.conn[self.scheme][query.table]
  14. if single_insert:
  15. res = db.insert_one(query.data[0])
  16. return res is not None
  17. else:
  18. res = db.insert_many(query.data)
  19. return len(res.inserted_ids) == len(query.data)
  20. def read_raw(self, query: "MongoDB.Query") -> pymongo.cursor.Cursor:
  21. db = self.conn[self.scheme][query.table]
  22. res = db.find(query.condition, query.select)
  23. return res
  24. def read(self, query: "MongoDB.Query", single_row: bool = False) -> Optional[pd.DataFrame]:
  25. super(MongoDB, self).read(query, single_row)
  26. db = self.conn[self.scheme][query.table]
  27. res = db.find(query.condition, query.select)
  28. return self.raw_to_dataframe(res, single_row=single_row)
  29. def update(self, query: "MongoDB.Query") -> bool:
  30. db = self.conn[self.scheme][query.table]
  31. res = db.update_many(query.condition, {'$push': query.data[0]}, upsert=True)
  32. return res.matched_count > 0
  33. def close(self) -> None:
  34. super(MongoDB, self).close()
  35. self.conn.close()
  36. def reconnect(self) -> None:
  37. super(MongoDB, self).reconnect()
  38. @staticmethod
  39. def raw_to_dataframe(raw: pymongo.cursor.Cursor, single_row: bool = False) -> Optional[pd.DataFrame]:
  40. if raw is None:
  41. return None
  42. data = list(raw)
  43. if len(data) == 0:
  44. return None
  45. if single_row:
  46. return pd.DataFrame(data).iloc[0]
  47. else:
  48. return pd.DataFrame(data)