Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB - paging

Tags:

mongodb

paging

When using MongoDB, are there any special patterns for making e.g. a paged view? say a blog that lists the 10 latest posts where you can navigate backwards to older posts.

Or do one solve it with an index on e.g. blogpost.publishdate and just skip and limit the result?

like image 728
Roger Johansson Avatar asked Feb 19 '11 09:02

Roger Johansson


People also ask

What is paging in MongoDB?

The pagination is nothing but the retrieve next page by using the previous page. To faster access data, we can create the index on the collections field in MongoDB. Syntax: Hadoop, Data Science, Statistics & others. Below is the syntax of MongoDB pagination.

What is paging in Nosql?

Pagination is the process of separating the contents into discrete pages. Each page has a list of entities from the database.

What is mongoose paginate?

mongoose-paginate-v2 is a pagination library having a page wrapper. The main usage of the plugin is you can alter the return value keys directly in the query itself so that you don't need any extra code for transformation.


1 Answers

Using skip+limit is not a good way to do paging when performance is an issue, or with large collections; it will get slower and slower as you increase the page number. Using skip requires the server to walk though all the documents (or index values) from 0 to the offset (skip) value.

It is much better to use a range query (+ limit) where you pass in the last page's range value. For example if you are sorting by "publishdate" you would simple pass the last "publishdate" value as the criteria for the query to get the next page of data.

like image 93
Scott Hernandez Avatar answered Sep 21 '22 03:09

Scott Hernandez