I am Hack Sparrow
Captain of the Internets.

Mongoose Pagination Plugin – Mongoose Pages

I have been wanting to write a Mongoose pagination plugin for a long time. Not that there were no pagination plugins for Mongoose, it is just that the available plugins had a rather developer-unfriendly API.

Officially presenting Mongoose Pages - the developer-friendly pagination plugin for Mongoose.

Installing Mongoose Pages is simple. Just type the following command at the terminal.

$ npm install mongoose-pages

Mongoose pagination via skip method

If your collection size is small and is not likely to grow much over a period of time, you can use the skip method and specify the number of items to be shown per page, and the page number.

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var mongoosePages = require('mongoose-pages');

var UserSchema = new Schema({
username: String,
points: Number,
email: String
})

mongoosePages.skip(UserSchema); // makes the findPaginated() method available

var docsPerPage = 10;
var pageNumber = 1;

var User = mongoose.model('User', UserSchema);
User.findPaginated({}, function (err, result) {
if (err) throw err;
console.log(result);
}, docsPerPage, pageNumber); // pagination options go here

The response object of pagination via skip will have the following structure.

{
documents: Array; list of documents
totalPages: Number; total number of pages, as per the `docsPerPage` value
prevPage: Number; the previous page number
nextPage: Number; the next page number
}

Mongoose pagination via anchor method

If your collection size is very large and your app has a lot of traffic, it is recommended to use the anchor method. You pass it the number of items to be shown per page, and the anchor id. If you are requesting for the first page, you don't have to specify the anchor id. You will get the anchorId in the response of the first page, if there are more pages; which can then be used to request for the next page.

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var mongoosePages = require('mongoose-pages');

var UserSchema = new Schema({
username: String,
points: Number,
email: String
})

mongoosePages.anchor(UserSchema); // makes the findPaginated() method available

var docsPerPage = 10;
var anchorId = '53c797a2043db36f2b673cd1';

var User = mongoose.model('User', UserSchema);
User.findPaginated({}, function (err, result) {
if (err) throw err;
console.log(result);
}, docsPerPage, anchorId); // pagination options go here

The response object of pagination via anchor will have the following structure.

{
documents: Array; list of documents
totalPages: Number; total page count
prevAnchorId: String; ObjectId which was used as the anchor id in the last request
nextAnchorId: String; ObjectId which should be used as the anchor id in the next request
}

For more details about Mongoose Pages, reporting bugs and issues, requesting features, please visit the official project page on GitHub.

4 Responses to “Mongoose Pagination Plugin – Mongoose Pages”

  1. Kane says:

    Thanks for taking the time to build this module. I’m using it in my application and the pagination is working fine. By default it returns documents in order from oldest to newest. I am trying to instead return on the first page the newest inserted documents then page to the next oldest and so forth until the last page ultimately shows the first documented inserted into Mongo. Do you know the easiest way to essentially reverse the default sort order?

  2. AlSan says:

    Hi there!! Firstly, thanks for the time to help others. I wonder how I could implement the mongoose querying chained syntax ( ie. .where(‘product’).equals(req.product), or .populate([{ path: ‘product’, select: ‘name code provider’ }])) with your moongose pages plug in. Thanks

  3. AlSan says:

    Sorry for my ignorance, but could you give us an example of how the next and prev links should look like? I have “?anchorId=” and “?anchorId=” and next appears to work just fine, but prev just loads the same url. Thanks in advance

  4. Bishal Dangal says:

    Hey man.. great work with the tutorial but here is the thing…
    1) It’s okay I get the nextAnchorId but what if I want to go to say page 14 from page 1, there’s no use of nextAnchorId
    2) Also, incrementing the nextAnchorId by whatever the size per page of the page is doesn’t guarantee the id is genuine as some might be deleted.

    So, basically what I am trying to say is can this plugin be helpful if user wants to jump pages rather than sequentially hit next..

    Nevertheless, once again thanks for the tutorial

Make a Comment