on
Italy
- Get link
- X
- Other Apps
_id field in your documents. It uses the min() and max() MongoDB cursor functions to implement the pagination.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.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.
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 pagePAGE_NUMBER is the current page numberdb.companies.count() to get the number of documents in the collection and implement the pagination navigation links._id
Comments
Post a Comment