class mongoDB

2019年5月1日17:56:27 发表评论 2,632 views
# import pymongo
# class MyMongodb:
# def __init__(self,dataname,collection):
# client = pymongo.MongoClient() # 建立连接
# self.db = client[dataname] # 指定数据库名
# self.usecollection = db[collection]
#
# def find(self,data,onlyOne = True):
#
#
# def insert(self,data,onlyOne = True):
import pymongo
class MyMong:
def __init__(self,database,collection):
self.client = pymongo. MongoClient()#建立连接
self.db = self.client[database]#连接数据库
self.my_col = self.db[collection]
def find(self,onlyOne = True):
if onlyOne:
ret = self.my_col.find_one()
return ret
else:
ret = self.my_col.find()
for i in ret:
print(i)
def insert(self,data,onlyOne = True):
if onlyOne:
self.my_col.insert_one(data)
else:
self.my_col.insert_many(data)
def update(self,data,new_data,onlyOne = True):
if onlyOne:
self.my_col.update_one(data,{'$set':new_data})
else:
self.my_col.update_many(data,{'$set':new_data})
def delete(self, data, onlyOne=True):
if onlyOne:
self.my_col.delete_one(data)
else:
self.my_col.delete(data)

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: