コード例 #1
0
    def update(self, condition_dict, update_dict, addtime=True):
        '''
        通过mongodb的update命令修改数据数据,如果没有该数据,直接插入
        :参数
            collect_name:集合名
            condition_dict:查询条件
            update_dict:{'collect':集合名,'data':更新字典}
        :返回
            一个列表
        '''

        if not self.connecter:
            self.logger.error(self.error_reason)
            return (False, self.error_reason)

        try:
            collect_name = update_dict['collect']
            # 获取集合
        except Exception as e:
            self.logger.error(self.log_prefix +
                              ' 更新数据失败,原因:参数update_dict格式出错,缺少collect,即缺少集合名')
            return (False, '更新数据失败,参数update_dict格式出错,缺少collect,即缺少集合名')

        updatedict = update_dict['data']
        condition_dict = dot2_(condition_dict)
        updatedict = dot2_(updatedict)

        if not collect_name or not (isinstance(updatedict, dict)
                                    and updatedict != {}):
            self.logger.error(self.log_prefix + ' 从集合' + collect_name +
                              '更新数据失败,原因:参数update_dict格式出错')
            return (False, False)

        collection = self.dbs[collect_name]
        condition_dict = self._handler_condition(condition_dict)
        result = collection.update(condition_dict, updatedict)
        # result 类似与{'n': 1, 'nModified': 1, 'ok': 1.0, 'updatedExisting': True}
        # {'n': 0, 'nModified': 0, 'ok': 1.0, 'updatedExisting': False}
        try:
            res_code = result['n']
            if res_code:
                self.logger.info(self.log_prefix + ' 从集合' + collect_name +
                                 '更新数据成功')
                return (True, True)
            else:
                self.logger.warn(self.log_prefix + ' 从集合' + collect_name +
                                 '更新数据失败,原因:根据查询条件无法查询到指定数据,使用插入函数进行处理')
                return self.insert(update_dict, addtime=addtime)
        except Exception as e:
            self.logger.warn(self.log_prefix + ' 从集合' + collect_name +
                             '更新数据失败,原因:根据查询条件无法查询到指定数据,使用插入函数进行处理,' + str(e))
            return self.insert(update_dict, addtime=addtime)
コード例 #2
0
    def batch_insert(self, insert_list, addtime=True):
        '''
        同库批量插入数据
        '''
        if not self.connecter:
            self.logger.error(self.error_reason)
            return (False, self.error_reason)

        if not (isinstance(insert_list, (list, tuple))):
            self.logger.error(self.log_prefix +
                              ' 批量插入数据失败,原因:参数insert_list不是列表或者元组')
            return (False, '批量插入数据失败,参数insert_list不是列表或者元组')

        insert_data = {}
        for insert_dict in insert_list:
            try:
                collect_name = insert_dict['collect']
                # 获取集合
            except:
                continue

            try:
                data = insert_dict['data']
                # 获取插入数据
                if not isinstance(data, dict):
                    continue
                    # 插入数据类型不为字典,不执行

                if addtime:
                    data['add_time'] = time.time()
            except:
                continue

            if not collect_name in insert_data:
                insert_data[collect_name] = [data]
            else:
                insert_data[collect_name].append(data)

        insert_data = dot2_(insert_data)
        result_dict = {}
        for collect_name, data in insert_data.items():
            collection = self.dbs[collect_name]

            try:
                result = collection.insert_many(data)
            except Exception as e:
                self.logger.error(self.log_prefix + ' 批量插入数据到集合' +
                                  collect_name + '失败,原因:' + str(e))
                return (False, '批量插入数据失败,' + str(e))

            result_dict[collect_name] = result

        self.logger.error(self.log_prefix + ' 批量插入数据到集合' + collect_name + '成功')
        return (True, result_dict)
コード例 #3
0
    def insert(self, insert_dict, addtime=True):
        '''
        指定表插入数据
        :参数
            insert_dict:需要插入的数据
            addtime是否追插入日期
        :返回
            一个元组,(False,原因)或者(True, 结果) 
        '''

        if not self.connecter:
            self.logger.error(self.error_reason)
            return (False, self.error_reason)

        try:
            collect_name = insert_dict['collect']
            # 获取集合
        except Exception as e:
            self.logger.error(self.log_prefix +
                              ' 插入数据失败,原因:参数insert_dict格式出错,缺少collect,即缺少集合名')
            return (False, '插入数据失败,参数insert_dict格式出错,缺少collect,即缺少集合名')

        try:
            data = insert_dict['data']
            # 获取插入数据
            if not isinstance(data, dict):
                self.logger.error(self.log_prefix + ' 插入数据到集合' + collect_name +
                                  '失败,原因:参数insert_dict格式出错,data值不为字典')
                return (False, '插入数据到集合' + collect_name +
                        '失败,参数insert_dict格式出错,data值不为字典')
        except Exception as e:
            self.logger.error(self.log_prefix + ' 插入数据到集合' + collect_name +
                              '失败,原因:参数insert_dict格式出错,缺少键data')
            return (False, '插入数据失败,参数insert_dict格式出错,缺少键data')

        if addtime:
            data['add_time'] = time.time()

        collection = self.dbs[collect_name]
        data = dot2_(data)
        try:
            result = collection.insert(data)
            self.logger.info(self.log_prefix + ' 插入数据到集合' + collect_name +
                             '成功')
            return (True, result)
        except Exception as e:
            self.logger.error(self.log_prefix + ' 插入数据到集合' + collect_name +
                              '失败,原因:' + str(e))
            return (False, '插入数据失败,' + str(e))
コード例 #4
0
    def _handler_condition(self, condition_dict):
        '''
        在查询或更新数据时,对查询条件进行处理
        '''
        condition_dict = dot2_(condition_dict)
        for k, v in condition_dict.items():
            if k == '_id':
                # 把_id的值进行转化
                if isinstance(v, str):
                    try:
                        v = int(v)
                    except:
                        v = ObjectId(v)

                    condition_dict[k] = v

        return condition_dict
コード例 #5
0
    def find_one(self, collect_name, get_field=[], condition_dict={}):
        '''
        指定表的部分数据        
        :参数
            collect_name:集合名
            get_field_list:获取需要的字段
            condition_dict:查询条件
        :返回
            一个列表
        
        '''

        if not self.connecter:
            self.logger.error(self.error_reason)
            return (False, self.error_reason)

        condition_dict = dot2_(condition_dict)

        collection = self.dbs[collect_name]
        try:
            if isinstance(condition_dict, dict):
                if condition_dict != {}:
                    condition_dict = self._handler_condition(condition_dict)
                    query_list = collection.find_one(condition_dict)
                else:
                    query_list = collection.find_one()
            else:
                query_list = collection.find_one()

            result = self._handler_result([query_list], get_field=get_field)
            self.logger.info(self.log_prefix + ' 从集合' + collect_name +
                             '查询并返回其中一条数据成功')
            return (True, result)
        except Exception as e:
            self.logger.error(self.log_prefix + ' 从集合' + collect_name +
                              '查询并返回其中一条数据失败,原因:' + str(e))
            return (False, '查询并返回其中一条数据失败,原因:' + str(e))