• python连接集群mongodb,封装增删改查


    1.下载pymongo

    pip install pymongo

    2.直接上代码

    [ini配置文件]

     封装读ini省略~~

    [db.py]

    class Database():
    
        def __init__(self):
            self.cf = ConfigParser.ConfigParser()
            self.mq = StringManipulation
        
    
         # 操作mongodb
        def db_mongo(self,Positioning_mode,Location_element,Input_content):
    
            self._mongo_p= self.mq._mongo_path()
            self.cf.read(self._mongo_p)
    
            db_host = self.cf.get(Positioning_mode, "host")
    
            operation_mr=['delete','select','update','insert']
    
            get_mer=Location_element[0:6]
    
            count_str=Location_element.count('.')
    
            if Location_element and count_str == 2:
    
                get_me_list = Location_element.split('.')
    
                get_method=get_me_list[0]
    
                get_base=get_me_list[1]
    
                get_table=get_me_list[2]
                if get_method in operation_mr and get_method and get_base and  get_table:
    
                    print(get_method)
                    print(get_base)
                    print(get_table)
    
                    # 连接mongodb 目前只写了集群的连接方法
                    client = MongoClient(db_host)
    
                    # 目标数据库
                    db=eval("client."+get_base)
    
                    # 指定表
                    collection = eval("db."+get_table)
                    # 需要select
                    # Input_content=eval(Input_content)
                    if Input_content:
                        if get_method == 'select':
                            Input_content = eval(Input_content)
                            for get_select in collection.find(Input_content):
                                print("查询的数据成功:")
                                print(get_select)
    
                        elif get_method == 'update':
                            Input_content=eval(Input_content)
                            # print(Input_content)
    
                            get_find_dict=Input_content[0]
                            get_set_dict=Input_content[1]
                            collection.update(get_find_dict, {"$set":get_set_dict})
    
                            print("数据修改成功")
    
                            # collection.update({"userName":"45852488@qq.com"},{"$set":{"code":"1783558010"}})
    
                        elif get_method =='insert':
    
                            Input_content=eval(Input_content)
                            if isinstance(Input_content,tuple):
                                print(type(Input_content))
                                insert_list=[]
                                for i in Input_content:
                                    insert_list.append(i)
                                collection.insert(insert_list)
                                # print(insert_list)
                                print("插入数据成功")
                            else:
                                collection.insert(Input_content)
                                print("插入数据成功")
    
                        elif  get_method =='delete':
                            print('delete')
                            Input_content=eval(Input_content)
                            if isinstance(Input_content,tuple):
                                for get_del_data in Input_content:
                                    collection.remove(get_del_data)
                                    print("数据已删除")
    
                            else:
                                print("非t")
                                collection.remove(Input_content)
                else:
                    print("操作关键字或数据库或表填写格式有误")
            else:
                print("操作关键字或数据库或表填写格式有误")
    

      

    调用上面的方法增删改查

    """
    #参数解释
    Positioning_mode 对应配置文件[base_test],连接指定的mongodb
    Location_element  参数格式: 操作方法.数据库.表    
    Input_content   增删该查的条件,mongo都是以字典的形式
    """
    
    
    # 删除数据
    # Database().db_mongo('base_test','delete.mydb.email','{"userName" : "1435117266@qq.com"},{"userName" : "1435117288@qq.com"}')
    #
    # 插数据,多个或单个
    # Database().db_mongo('base_test','insert.mydb.email','{"userName":"1435117288@qq.com","code" : "9999999999"},{"userName":"1435117266@qq.com","code" : "99999999999"}')
    #
    # 查询
    # Database().db_mongo('base_test','select.mydb.email','{"userName":"45852488@qq.com"}')
    #
    #
    # 修改
    # Database().db_mongo('base_test','update.mydb.email','{"userName":"45852488@qq.com"},{"code":"1783558010","success":True}')
    #
    # {"code":"9999999999"}
    #
    

      

  • 相关阅读:
    调用Config.ini类
    winform用AForge拍照功能
    winform导出csv
    winform导出excel
    net里面using的使用
    DBNULL与null的区别
    mui app在线更新
    redux-thunk初步使用
    初步使用Web Notification 实现浏览器消息通知
    ios唤起键盘后,页面不收回导致元素错位的问题.(譬如固定在底部的自定义键盘等)
  • 原文地址:https://www.cnblogs.com/Jack-cx/p/9411851.html
Copyright © 2020-2023  润新知