how to do pagination using mongoengine?
Asked Answered
I

3

14

i have a question about pagination using mongodb and mongoengine. i have a table which will have millions of records in future. and i am doing paging like this.

well i am not sure this is correct approach

    list = Books.objects.all()
    paginator = DiggPaginator(list, 20, body = 10, tail = 2)

here i open whole table and then do the pagination and we on for next page again above code runs and brings the 2nd or any page.

is this correct approach or there are any better ways to do this.

Indestructible answered 22/11, 2012 at 5:11 Comment(0)
I
30

You can use skip and limit from QuerySet to achieve pagination.
For example if you want to show the second page with a limitation of 10 items per page, you can do like this:

page_nb = 2 
items_per_page = 10 

offset = (page_nb - 1) * items_per_page

list = Books.objects.skip( offset ).limit( items_per_page )
Impudent answered 22/11, 2012 at 7:48 Comment(2)
Does that mean my approach is not correct ? or that will work fine too without putting any extra load on server.Indestructible
By using Books.objects.all(), you request all the documents from the table, so if your table have millions of records that will put some load on the server. Anyway, it should be trivial to set a benchmark to test what is the most effective for your use case.Impudent
A
2

The flask-mongoengine plugin has an example of a paginator you could adapt to follow the digg paginator.

Alphosis answered 22/11, 2012 at 9:23 Comment(0)
H
1

You can use array-slicing syntax as well which is a bit more decent and readable :

begin = (page - 1) * page_size # offset
end = offset + page_size
list = Books.objects[begin:end]()
Hersey answered 16/1, 2022 at 0:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.