MotorCursor.count and MotorGridOutCursor.count. Use MotorCollection.count_documents() or MotorCollection.estimated_document_count()
使用分页功能,导致两次执行mongo查询。例子
async def page(query_filer, page, page_page_count):
_count = await get_collection().count_documents(query_filer)
total_page = math.Ceil(_count/page_page_count)
if total_page > page:
return [], total_page
records = await get_collection().find(query_filer}).skip((page - 1) * limit).limit(limit).to_list()
return res, total_page
但是使用旧的1.x电机,可以编写以下代码
async def old_page(query_filer, page, page_page_count):
cursor = get_collection().find(query_filer})
_count = await cursor.count()
total_page = math.Ceil(_count/page_page_count)
if total_page > page:
return [], total_page
records = await cursor.skip((page - 1) * limit).limit(limit).to_list()
return res, total_page
从性能角度看,电机2.0的性能更低?