Skip to content

Instantly share code, notes, and snippets.

@alonronin
Created July 4, 2013 16:44
Show Gist options
  • Save alonronin/5929043 to your computer and use it in GitHub Desktop.
Save alonronin/5929043 to your computer and use it in GitHub Desktop.
Mongoose helper for paginate a query
var mongoose = require('mongoose');
mongoose.Model.paginate = function(query, page, records, callback){
page || (page = 1);
page = page.toNumber().abs();
var from = (page * records) - records;
query.skip(from).limit(records).exec(function(err, results){
if(err) return callback(err);
query.count(function(err, count){
callback(err, results, count, (count / (records || count)).ceil());
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment