简体   繁体   中英

Flask-Mongoengine API Pagination

I'm using MongoDB and Flask to make a JSON API for a mobile app.

When the app calls /topics/ it will return a list of topics, but how do i paginate the results efficiently? MongoEngine provides a offset and limit, but that grabs the whole result set before limiting and such, which is not very efficient if we have thousands of documents.

@app.route('/topics/<int:limit>/<int:page>', methods=['GET'])
def get_topics(limit=25, page=1):
    if limit is None or limit <= 1: 
        limit = 2

    if page is None or page <= 1:
        page = 1

    offset = (page - 1) * limit
    topics = Topic.objects().limit(limit).skip(offset)

Actually, mongoengine does queries with limit and skip parameters in mongodb. You can verify that by using explain to the query and have it return the execution plan instead of the results.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM