from itemadapter import ItemAdapter import pymongo class Mongo_Client(object): def __init__(self,collection_name): self.clinet = pymongo.MongoClient("x", username='x', password='x', authSource='admin', authMechanism='SCRAM-SHA-256',port=27017, connect=False ) # db = self.clinet["test1"] db = self.clinet["new_movie"] self.collection_name = db[collection_name] def insert(self, data): self.collection_name.insert_one(data) def __del__(self): # print(self.PhRes.count_documents()) # print("插入的数据------》"+self.count) print("关闭连接") self.clinet.close() def get(self): print("查询一条数据") return self.collection_name.find_one({}) def getall(self): print("所有数据") data_list = self.collection_name.find({}) count = 0 for tmp in data_list: count += 1 print(tmp,"----",count) def count(self): # count_documents print("数据总数") return self.collection_name.count_documents({}) def drop_collection_data(self): # 删除collection数据 count = self.collection_name.delete_many({}) print("s删除的数据量是",count.deleted_count) def get_list_page(self,page_size,page_no): # 分页查询 print("分页查询") skips = page_size * (page_no -1 ) return self.collection_name.find().limit(page_size).skip(skips) def update_one(self,query,update_data): # 更新一条数据 self.collection_name.update_one(query,update_data) def get(self,query): return self.collection_name.find_one(query)
标签:count,name,self,collection,print,工具,pymongo,def From: https://www.cnblogs.com/lzjloveit/p/17358382.html