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.

Comments