Flask-mongoengine pagination performance issue

So I'm using flask-mongoengine as my ORM in many projects.

The pagination comes with it is very useful and easy to use, especially the iter_pages() function.

However, there's a problem with flask-mongoengine's pagination that can cause a huge performance lag. The count() function used to calculate the number of documents used to be paginated does not take limit() and skip() into account. So, when you have 1000K documents and only want to display the first 10K to your users, it will eat a lot of resources that is undesired.

To fix this, just add with_limit_and_skip=True as a keyword argument when calling count()

Written with StackEdit.

comments powered by Disqus