I am Hack Sparrow
Captain of the Internets.

MongoDB Pagination – using range queries

How to implement pagination in MongoDB using range queries


This method of MongoDB pagination requires manually setting the _id field in your documents. It uses the min() and max() MongoDB cursor functions to implement the pagination.

We'll be learning by examples on a collection named 'companies'. Make sure there are no documents on it.

db.companies.remove()

Make sure the _id value starts from 0 and increment it with each new document inserted.

db.companies.insert({_id:0, name:'Google'})
db.companies.insert({_id:1, name:'Facebook'})
db.companies.insert({_id:2, name:'Apple'})
db.companies.insert({_id:3, name:'Microsoft'})
db.companies.insert({_id:4, name:'Oracle'})
db.companies.insert({_id:5, name:'IBM'})
db.companies.insert({_id:6, name:'Yahoo'})
db.companies.insert({_id:7, name:'HP'})

In your web app, you will need to set up a system to take care of incrementing the _id value.

min() and max() can be called only on indexed key, since _id is indexed by default, we are good to go.

Observe the results of these range queries.

db.companies.find().min({_id:0}).max({_id:3})
db.companies.find().min({_id:3}).max({_id:6})

Actually run them on a mongo shell and see the results. From those two commands you probably realized we have a working pagination technique in place, it just needs to done programatically now.

The code below is an example of pagination implemented using range query with min() and max().

var min_page = NUMBER_OF_ITEMS * (PAGE_NUMBER - 1)
var max_page = min_page + NUMBER_OF_ITEMS
db.companies.find().min({_id:min_page}).max({_id:max_page})

NUMBER_OF_ITEMS is the number of items to be shown on a page
PAGE_NUMBER is the current page number

Use db.companies.count() to get the number of documents in the collection and implement the pagination navigation links.

Notes

  1. Needs a system to increment _id
  2. Data structure logic is mostly maintained
  3. Will fail if any of the root documents are deleted

So that's my way of implementing pagination in MongoDB using a range query. There could be other methods pagination using range queries, if you know any interesting methods, share them in the comments. Any questions, opinions about this pagination technique? Ping me in the comments.

3 Responses to “MongoDB Pagination – using range queries”

  1. John says:

    Just out of curiosity, why use integers instead of ObjectId’s as ID’s?

  2. Captain says:

    Hi John, it’s so that the GET params look neat, and also to enable pagination.

  3. Hwang says:

    great example.!!!!
    i’m making paging in node.js your example!
    but error occurs !
    error message:TypeError: Object [object Object] has no method ‘min’
    my code
    function(callback){
    db.member.find({}).min({_id:min_page}).max({_id:max_page},function(error,list){
    if(error){
    console.log(‘find error:’+error);
    }
    });
    }
    why do these errors occur?

Make a Comment