zoukankan      html  css  js  c++  java
  • python操作mongodb

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    #bbyxh5, 回档单个玩家数据
    #注意:执行回档脚本前,必须要保证数据库中存在一个bbh_xxx_old,即是导入备份数据到数据库中,并必须以bbh_xxx_old命名
    #导入数据语句:mongorestore -h 127.0.0.1 --port 27017  -uxxx -pxxxx --authenticationMechanism=MONGODB-CR --authenticationDatabase=admin  -d bbh_29_old bbh_178/
    
    import yaml
    import os
    import sys
    import datetime
    import pymongo
    
    reload(sys)
    sys.setdefaultencoding('utf8')
    
    filter_table = ["martialarts","posthouses","xk.lplayers","gdr.actors"]
    playerId = 193988687
    
    def load_config():
        config_file = os.path.join(os.getcwd(), '..', 'optconfig.yaml')
        config = yaml.load(open(config_file))
        server_name = config['GAME_CONFIG']['SERVER_NAME']
        server_id = config['GAME_CONFIG']['SERVER_ID']
        server_dir = os.path.join('/data', server_name, 'script')
        current_dbname = 'bbh_' + str(server_id)
        old_dbname = 'bbh_' + str(server_id) + '_old'
        ####
        mongodb_port = config['GAME_CONFIG']['MONGODB_PORT']
        mongodb_host = config['GAME_CONFIG']['MONGODB_HOST']
        mongodb_user = config['GAME_CONFIG']['MONGODB_USER']
        mongodb_pass = config['GAME_CONFIG']['MONGODB_PASS']
        mongodbUri = "mongodb://"+mongodb_user+":"+str(mongodb_pass)+"@"+mongodb_host+":"+str(mongodb_port)+"/admin"
        mongo_conn = pymongo.MongoClient(mongodbUri)
        ####
        return server_dir, server_id, current_dbname, old_dbname, mongo_conn
    
    
    if __name__ == '__main__':
        try:
            server_dir, server_id, current_dbname, old_dbname, mongo_conn = load_config()
            date = datetime.datetime.now().strftime('%Y-%m-%d')
            now_time = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
            dbs = mongo_conn.database_names()
            #print dbs
            if current_dbname not in dbs or old_dbname not in dbs:
                print("数据库中不同时存在库 %s %s,退出"%(current_dbname, old_dbname))
                sys.exit(1)
            db_obj_current = mongo_conn[current_dbname] #当前数据库连接对象
            db_obj_old = mongo_conn[old_dbname] #旧数据库连接对象
            all_tables = db_obj_old.list_collection_names() #获取所有表
            for table in all_tables:
                if table not in filter_table:
                    old_data_obj = db_obj_old[table].find({"playerId":playerId})    #从旧数据获取对应玩家数据
                    if old_data_obj.count() == 0:   #如果旧表对应玩家数据等于0,则删除当前表对应玩家数据
                        db_obj_current[table].remove({"playerId": playerId})  # 删除当前玩家的数据
                    if old_data_obj.count() > 0:    #判断有对应玩家数据的表
                        db_obj_current[table].remove({"playerId":playerId}) #删除当前玩家的数据
                        db_obj_current[table].insert_many(old_data_obj) #插入玩家回档的旧数据
                    current_data_obj = db_obj_current[table].find({"playerId":playerId})    #从当前数据获取对应玩家数据
                    if current_data_obj.count() == old_data_obj.count():
                        #print("table: %s 校对玩家 %s 数据,相同为 %s 条"%(table,playerId,current_data_obj.count()))
                        pass
                    else:
                        print("table: %s 校对玩家 %s 数据,不相同,当前: %s 条,旧: %s 条"%(table,playerId,current_data_obj.count(),old_data_obj.count()))
            print("%s 玩家 %s 回档完成......"%(current_dbname,playerId))
        except Exception as e:
            print(e)
  • 相关阅读:
    sfs2x 连接 mongodb
    java websocket
    webstorm 4.0 注册码
    解决 sfs2 admin tool 找不到扩展
    window 注册表五大类
    opengl 学习第二日
    java google Protobuf
    扩展 java sencha touch PhonegapPlugin
    sencha touch2 kryonet socket phonegap 通信 作者:围城
    sencha touch2 layout 笔记
  • 原文地址:https://www.cnblogs.com/chenjw-note/p/15403940.html
Copyright © 2011-2022 走看看