def get_file_by_name(self, coll_name, name): """ 根据文件名从gridFS获取文件 :param coll_name: :param name: :return: """ try: fs = GridFS(self.db, coll_name) gf = fs.find({'name': name}) if not gf: return None data = gf.read() info = { "chunk_size": gf.chunk_size, "metadata": gf.metadata, "length": gf.length, "upload_data": gf.upload_date, "name": gf.name, "content-type": gf.content_type } except Exception: raise exc.FindException( '{}Get file by name from "{}" failed! Name: {}, error: ' '{}'.format(self.log_prefix, coll_name, name, traceback.format_exc())) return data, info
def get_file_by_id(self, coll_name, file_id): """ 根据ObjectId从gridFS获取文件 :param coll_name: 集合名 :param file_id: 文件唯一ID :return: 文件数据流和文件信息 """ try: fs = GridFS(self.db, coll_name) gf = fs.get(ObjectId(file_id)) if not gf: return None data = gf.read() info = { "chunk_size": gf.chunk_size, "metadata": gf.metadata, "length": gf.length, "upload_data": gf.upload_date, "name": gf.name, "content-type": gf.content_type } except Exception: raise exc.FindException( '{}Get file from "{}" failed! _id: {}, error: {}'.format( self.log_prefix, coll_name, file_id, traceback.format_exc())) return data, info
def find_many(self, coll_name, offset=None, limit=None, sfilter=None, aggregate=False, sort_str=None, projection=None): """ 分页查询所有数据 :param coll_name:collection name :type coll_name: str :param offset: 偏移量,为0则表示查询全部 :type offset: int :param limit: 每页条数 :type limit: int :param aggregate: 是否为聚合操作 :type aggregate: bool :param sort_str: 排序依据 :type sort_str: str :param projection: 决定输出哪些字段 :type projection: dict :return: """ callback = self.default_callback try: if aggregate: ret = list( self.run_command(coll_name, 'aggregate', callback, sfilter)) leng = len(ret) if offset: ret = ret[offset::1] if limit: ret = ret[0:limit:1] else: # pymongo是在list转换的时候才会真正查询数据库,在此之前只是一个游标 ret = self.run_command(coll_name, 'find', callback, sfilter, projection=projection) if isinstance(offset, int): ret = ret.skip(offset) if isinstance(limit, int): ret = ret.limit(limit) if isinstance(sort_str, str): ret = ret.sort([(sort_str, pymongo.DESCENDING)]) leng = self.get_count(coll_name, sfilter) return list(ret), leng except Exception: raise exc.FindException( '{}Find data from collection "{}" failed! Error: {}'.format( self.log_prefix, coll_name, traceback.format_exc()))
def get_count(self, coll_name, sfilter): """ 获取记录个数 :param coll_name: :param sfilter: :return: """ callback = self.default_callback try: count = self.run_command(coll_name, 'count', callback, filter=sfilter) except Exception: raise exc.FindException( '{}Get data count from collection "{}" failed! Error: ' '{}'.format(self.log_prefix, coll_name, traceback.format_exc())) return count
def find_one(self, coll_name, sfilter, aggregate=False): """ 查询单条数据 :param coll_name: collection name :param sfilter: 查询条件 :param aggregate: 是否为聚合操作 :return: """ callback = self.default_callback try: if aggregate: ret = self.run_command(coll_name, 'aggregate', callback, sfilter) else: ret = self.run_command(coll_name, 'find_one', callback, sfilter) return ret except Exception: raise exc.FindException( '{}Find data from database "{}" failed! Error: {}'.format( self.log_prefix, coll_name, traceback.format_exc()))