mongoose-efficient-pagination

0.2.0 • Public • Published

mongoose-efficient-pagination

Build Status Coverage Status Dependency Status npm version

How it works

Use Case

Installation

npm install --save mongoose-efficient-pagination

API Reference

mongooseEfficientPagination

Using skip is not efficient for large data sets. Rather, we can achieve pagination by sorting by _id then telling mongo to use that _id as a starting point when returning the next page of records.

Example

// configure mongoose
var mongoose = require('mongoose');
var paginator = require('mongoose-efficient-pagination');
 
// optionally globally configure the default perPage value.
// this can be overridden in the paginate function itself.
paginator.perPage = 25;

Use it with a model...

var Customer = mongoose.model('Customer');
var sortOrder = -1;
var perPage = 10;
var startAfter = '52c1190207d5dbccda00000f'; // this value should be passed from your previous result set.
 
Customer.find({
    status: 'active'
})
.sort({
    createdAt: sortOrder, // this is up to you, sort by a field. I chose createdAt for this example.
})
.paginate(perPage, startAfter)
.exec();

mongooseEfficientPagination~paginator(perPage, [nextID]) ⇒

Can be called from the mongoose model prototype providing an easy way to paginate the result set of a query.

Kind: inner method of mongooseEfficientPagination
Returns: this

Param Type Default Description
perPage number 20 number of records per page
[nextID] ObjectId (null) the id of the document which you will be starting after

Dependents (0)

Package Sidebar

Install

npm i mongoose-efficient-pagination

Weekly Downloads

1

Version

0.2.0

License

Apache-2.0

Last publish

Collaborators

  • jrthib
  • bachonk
  • pbrain19