mogodb_helper.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # coding:utf-8
  2. from pymongo import MongoClient
  3. from pymongo.errors import AutoReconnect
  4. from bson import ObjectId
  5. import time
  6. import urllib.parse as parse
  7. class MongoDBInterface:
  8. def __init__(self, config):
  9. self.__host = config.get("ip_port", "")
  10. self.__user = config.get("user", "")
  11. self.__password = config.get("password", "")
  12. self.__database = config.get("db", "")
  13. self.connect()
  14. def connect(self):
  15. """
  16. 连接数据库
  17. :return:
  18. """
  19. # 特殊符号转义
  20. self.__user = parse.quote_plus(self.__user)
  21. self.__password = parse.quote_plus(self.__password)
  22. # 连接数据库
  23. try:
  24. if self.__user:
  25. self.client = MongoClient(
  26. "mongodb://{}:{}@{}".format(self.__user, self.__password, self.__host),
  27. unicode_decode_error_handler='ignore', directConnection=True)
  28. else:
  29. self.client = MongoClient("mongodb://{}".format(self.__host), unicode_decode_error_handler='ignore',
  30. directConnection=True)
  31. self.db = self.client[self.__database]
  32. except AutoReconnect as e:
  33. self.connect()
  34. def disconnect(self):
  35. self.client.close()
  36. def find_by_id(self, collection_name, m_id):
  37. try:
  38. collection = self.db[collection_name]
  39. if isinstance(m_id, str):
  40. m_id = ObjectId(m_id)
  41. documents = collection.find_one({"_id": m_id})
  42. return documents
  43. except AutoReconnect:
  44. time.sleep(5)
  45. print("Connection to MongoDB lost. Reconnecting...")
  46. self.connect() # 自动重连
  47. return self.find_by_id(collection_name, m_id)
  48. def update_by_id(self, collection_name, m_id, fields: dict):
  49. try:
  50. collection = self.db[collection_name]
  51. if isinstance(m_id, str):
  52. m_id = ObjectId(m_id)
  53. documents = collection.update_one({"_id": m_id}, {"$set": fields})
  54. return documents
  55. except AutoReconnect:
  56. print("Connection to MongoDB lost. Reconnecting...")
  57. self.connect() # 自动重连
  58. return self.update_by_id(collection_name, m_id, fields)
  59. def find_relus(self, collection_name, hash_id):
  60. collection = self.db[collection_name]
  61. row = collection.find_one({"rules_id": hash_id})
  62. return row
  63. def find_rule_by_company(self, collection_name, company, version):
  64. collection = self.db[collection_name]
  65. row = collection.find_one({"company_name": company, "version": version})
  66. if not row:
  67. return None
  68. return row.get("rules_id")
  69. def find_rule_by_title(self, collection_name, title):
  70. collection = self.db[collection_name]
  71. row = collection.find_one({"title": title})
  72. if not row:
  73. return None
  74. return row
  75. def insert2db(self, collection_name, data: dict):
  76. collection = self.db[collection_name]
  77. collection.insert_one(data)