motor2.0不推荐使用MotorCursor count API,为什么呢?

时间:2018-09-13 03:28:43

标签: mongodb tornado-motor motordriver

Motor 2.0 Migration Guide

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的性能更低?

0 个答案:

没有答案