python连接mongodb操作代码
发布时间:2020-12-17 17:24:20 所属栏目:Python 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 __author__ = 'Administrator'#pip install pymongofrom bson.objectid import ObjectIdimport pymongoimport timeclass pgo: def __init__(self,ip,p
以下代码由PHP站长网 52php.cn收集自互联网 现在PHP站长网小编把它分享给大家,仅供参考 __author__ = 'Administrator' #pip install pymongo from bson.objectid import ObjectId import pymongo import time class pgo: def __init__(self,ip,port,db,tb): self.ip = ip self.port = port self.db = db self.tb = tb def run(self): num = 0 try: conn = pymongo.Connection(self.ip,self.port) #db = conn.kkk[self.database] #data = db.kkk print('88888') db = conn[self.db] data = db[self.tb].find() for i in data: num += 1 print(i,num) print(i['_id'],i['name'],'t',i['age'],i['bm'],i['date'],i['mod_date'],num) print('姓名','年龄','部门','注册日期','tttttt','修改日期','统计') conn.close() return data except Exception as e: conn.close() print('Fail:',e) def add(self,name,age,bm): try: conn = pymongo.Connection(self.ip,self.port) db = conn[self.db] data = db[self.tb] #data.insert({'name':'kkk','age':22,'bm':'op'}) data.insert({'name':name,'age':age,'bm':bm,'date':(time.strftime('%Y-%m-%d %H:%M'))}) conn.close() print('OK') except: conn.close() print('Fail!') def remove(self,xarg): try: conn = pymongo.Connection(self.ip,self.port) db = conn[self.db] data = db[self.tb] #id ='ObjectId("%s")'%xarg id = '%s'%xarg data.remove({"_id": ObjectId(id)}) #data.remove(xarg) conn.close() print('OK') except: conn.close() print('Fail!') def update(self,*xarg): try: conn = pymongo.Connection(self.ip,self.port) db = conn[self.db] data = db[self.tb] #data.update({"name":{'$eq':'kkk'}},{'$set':{'age':10}},upsert=False,multi=True) data.update(*xarg,upser=False,multi=True) conn.close() print('OK') except: conn.close() print('Fail!') if __name__ == '__main__': #a = "549be654b69e727c1396c8f8" #pgo('127.0.0.1',27017,'kkk','kkk').remove(a) #pgo('127.0.0.1','kkk').add('kkk',99,'OP') pgo('127.0.0.1','kkk').update({'name':'user_18'},{'$set':{'date':(time.strftime('%Y-%m-%d %H:%M')),'mod_date':(time.strftime('%Y-%m-%d %H:%M'))}}) 2- __author__ = 'Administrator' #pip install pymongo #from bson.objectid import ObjectId import pymongo import time class pgo: def __init__(self,tb): self.ip = ip self.port = port self.db = db self.tb = tb def run(self): try: conn = pymongo.Connection(self.ip,self.port) db = conn[self.db] data = db[self.tb].find({}).limit(1500) conn.close() return data except Exception as e: conn.close() def find(self,self.port) db = conn[self.db] data = db[self.tb].find_one(xarg) conn.close() return data except Exception as e: conn.close() def add(self,self.port) db = conn[self.db] data = db[self.tb] data.insert(xarg) conn.close() except: conn.close() def remove(self,self.port) db = conn[self.db] data = db[self.tb] data.remove(xarg) conn.close() except: conn.close() def insert(self,self.port) db = conn[self.db] data = db[self.tb] data.insert(xarg) conn.close() except: conn.close() def save(self,self.port) db = conn[self.db] data = db[self.tb] data.save(xarg) conn.close() except: conn.close() def update(self,multi=True) conn.close() except: conn.close() if __name__ == '__main__': #pgo('127.0.0.1','kkk').remove({'name':'kkk','age':{"$gte":32}}) #pgo('127.0.0.1','OP') #pgo('127.0.0.1','kkk').update({},{'$set':{'date': (time.strftime("%Y-%m-%d_%H:%M")),'mod_date': (time.strftime("%Y-%m-%d_%H:%M"))}}) pgo('127.0.0.1','kkk').run() 以上内容由PHP站长网【52php.cn】收集整理供大家参考研究 如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |