WHCSRL 技术网

mongodb + python 使用with管理连接

import pymongo

class MongoConnection:
	def __init__(self, collection):
		self.db_url = 'mongodb://localhost:27017'
		self.db_name = 'store'
		self.col_name = collection
		self.user_name = 'pyclient'
		self.user_pwd = 'password'
		self.client = pymongo.MongoClient(
			self.db_url,
			socketTimeoutMS=5000
			)
		self.db = self.client[self.db_name]
		self.db.authenticate(self.user_name, self.user_pwd)
		self.collection = self.db[self.col_name]

	def __enter__(self):
		return  self.collection

	def __exit__(self, exc_type, exc_val, exc_tb):
		self.client.close()


if __name__ == '__main__':

	with MongoConnection('test') as conn:
		file_dict = {
		    "filename":"wahaha",
		    "filesize":100,
		    "DateTime":"2021-11-19 00:00:00"
			}

		res = conn.insert(file_dict)
		print(res)

		q = {'filename':'wahaha'}
		res = conn.find(q)
		print(list(res))

		res = conn.drop()
		print(res)
       

  • 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
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
推荐阅读