python连接mongodb操作代码

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

__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'], '\t',  i['bm'], '\t', i['date'], '\t', i['mod_date'], '\t', num)
            print('姓名', '\t', '年龄', '\t', '部门', '\t', '注册日期', '\t\t\t\t\t\t', '修改日期', '\t\t\t\t\t\t', '统计')
            conn.close()
            return data
        except Exception as e:
            conn.close()
            print('Fail:', '\t', 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', 27017, 'kkk', 'kkk').add('kkk', 99, 'OP')
    pgo('127.0.0.1', 27017, 'kkk', '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, ip, port, db, 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,xarg):
        try:
            conn = pymongo.Connection(self.ip, 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, xarg):
        try:
            conn = pymongo.Connection(self.ip, self.port)
            db = conn[self.db]
            data = db[self.tb]
            data.insert(xarg)
            conn.close()
        except:
            conn.close()
 
    def remove(self, xarg):
        try:
            conn = pymongo.Connection(self.ip, self.port)
            db = conn[self.db]
            data = db[self.tb]
            data.remove(xarg)
            conn.close()
        except:
            conn.close()
 
    def insert(self, xarg):
        try:
            conn = pymongo.Connection(self.ip, self.port)
            db = conn[self.db]
            data = db[self.tb]
            data.insert(xarg)
            conn.close()
        except:
            conn.close()
 
    def save(self, xarg):
        try:
            conn = pymongo.Connection(self.ip, self.port)
            db = conn[self.db]
            data = db[self.tb]
            data.save(xarg)
            conn.close()
        except:
            conn.close()
 
    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()
        except:
            conn.close()
 
 
 
if  __name__ == '__main__':
    #pgo('127.0.0.1', 27017, 'kkk', 'kkk').remove({'name':'kkk', 'age':{"$gte":32}})
    #pgo('127.0.0.1', 27017, 'kkk', 'kkk').add('kkk', 99, 'OP')
    #pgo('127.0.0.1', 27017, 'kkk', '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', 27017, 'kkk', 'kkk').run()