Pagination with mongoDB

Dear Reader,

All the post in have been moved to

shakthydoss.wordpress is no longer functioning. To get the latest updates and follow up your comments please come to and get subscribed.

Find Pagination with mongoDB @

Thank you


I was trying some thing myself and in the middle of the work I was in need to design pagination (by definition , showing the page content one after other possibly by some sorted order) Here I document my Pagination technique that doesn’t to use Limits, Skips, and Sorts query mechanism.

Google Pagination

Google Pagination

In my case I had a text data along with date stored in the mongoDB. I am Support to decision a pagination such that it shows the latest text data sorted with respect to date. First 10 results should shown on first pagination page then next 10 result should shown on second pagination page and so on.

My document on mongo db will look like this

    "key_1": "value_1",
    "count" : 100,
    "text": [
            "data": "text data 1",
            "date": "new date()"
            "data": "text data 2",
            "date": "new date()"
            "data": "text data 3",
            "date": "new date()"
            "data": "text data 4",
            "date": "new date()"

            "data": "text data 100",
            "date": "new date()"

Like any developer , I had an idea to use Limits, Skips, and Sorts query mechanism to achieve pagination. But on long run if document is going to have more than few thousand records , then it is not a good practices to use Limits, Skips, and Sorts. As Limits, Skips, and Sorts reduces the performance on large scale documents

Fortunately I used $push while inserting these sub documents to mongoDB. $push pushes the elements to the end of the array. This is the good thing I have done and it helps me to figure out the way to do pagination without Limits, Skips, and Sorts but by using The special “$slice” operator.

The special “$slice” operator can be used to return a subset of elements for an array key . “$slice” have ability to return elements in the middle of the array by taking an offset and the number of elements to returned. We will make use of this advantage to do our pagination.

Eg :

  , {"key" : {"$slice" : [23, 10]}})

This would skip the first 23 elements and return the 24th through 34th. If there are fewer than 34 elements in the array, it will return as many as possible.

 Technique is to get the elements of array in range and put those range in the corresponding pagination page that makes what I want.

Pagination Design


      1. Total No of text_data
      2. Total No of items per page
      3. Total No of pages


        1. using The $slice operator

Finding Total no of text_data 

With mongoDB there is no way (perhaps I don’t know )to find the size of sub document. But when decisioning the document I make sure that there will a key called ‘count’ which increments its value when ever a sub document is pushed into array. By reading the count value we have Total no of text_data.

Total No of items per page

we have to set this value manually . I have set to 10  so that 10 items  will be displayed per page.

Total no of pages

Required total no of page can be found by computing. Total no of pages = Total no of text_data / Total No of items per page

Here is code logic for pagination with mongoDB by using the $slice

$text_count = getCount(); if ($text_count>0) { $per_page = 8; $pages = ceil($text_count / $per_page); $page_id = (isset($_GET['page_id'])) ? $_GET['page_id'] : 1; $check = $text_count - ($page_id * $per_page); if ($check != 0) { if ($check + abs($check) != 0) { $start = $text_count - ($page_id * $per_page); $end = $per_page; } else { $start = 0; $end = $text_count % $per_page; } } else { $start = 0; $end = $per_page; } $cursor = $collection->find(array(), array('text' => array('$slice' => array($start, $end)))); foreach ($cursor as $value) { // your code to display the results } if ($page_id>=1 && $page_id <=$pages ; $text_count > $per_page) { for ($index = 1; $index <= $pages; $index++) { echo "<a href=?page_id=$index>$index</a> "; } } }  function getCount() { // your logic to get the count  }

My output :-





One Response to “Pagination with mongoDB”

  1. Padmini Says:

    Informative .. keep posting..

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: