Package Exports
- mongoose-paginate
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (mongoose-paginate) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
mongoose-paginate
mongoose-paginate
is a plugin for Mongoose schemas to easily add paginated queries and results. This plugin is to be used in combination with view pagination middleware such as express-paginate.
NOTICE: Versions > 3.1.4
are deprecated and unpublished from NPM due to a bad commit. Please use version 3.1.3
for no breaking changes or upgrade to the latest stable 4.0.0
release (see below documentation).
Index
Install
npm install -S mongoose-paginate
Usage
This plugin must first be added to a schema:
var mongoosePaginate = require('mongoose-paginate');
MySchema.plugin(mongoosePaginate);
MySchema
will have a new function called paginate
(e.g. MySchema.paginate()
).
MySchema.paginate(query, options, [callback])
Arguments
query
- An object for the Mongoose query.options
- An object with options for the Mongoose query, such as sorting and populationpage
- Default:1
limit
- Default:10
columns
- Default:null
sortBy
- Default:null
populate
- Default:null
lean
- Default:null
callback(err, results, pageCount, itemCount)
- If specified the callback is called once pagination results are retrieved, or when an error has occurred. Otherwise will return a promise.
Examples
// basic example usage of `mongoose-pagination`
// querying for `all` {} items in `MySchema`
// paginating by second page, 10 items per page (10 results, page 2)
var mongoosePaginate = require('mongoose-paginate');
MySchema.plugin(mongoosePaginate);
MySchema.paginate({}, {
page: 2, limit: 10
}, callback);
// basic example usage of `mongoose-pagination` with promises
// querying for `all` {} items in `MySchema`
// paginating by second page, 10 items per page (10 results, page 2)
var mongoose = require('mongoose'); // required mongoose v4.1.0 or higher
mongoose.Promise = require('bluebird');
var mongoosePaginate = require('mongoose-paginate');
MySchema.plugin(mongoosePaginate);
MySchema.paginate({}, {
page: 2, limit: 10
})
.spread(function(questions, pageCount, itemCount) {
...
})
.catch(function(err) {
return next(err);
});
// advanced example usage of `mongoose-pagination`
// querying for `{ columns: 'title', { populate: 'some_ref' }, { sortBy : { title : -1 } }` items in `MySchema`
// paginating by second page, 10 items per page (10 results, page 2)
MySchema.paginate(
{},
{
page: 2,
limit: 10,
columns: 'title',
populate: 'some_ref',
sortBy: {
title: -1
},
lean: true
},
callback
);
// populating more than one ref
MySchema.paginate({}, {
page: 2,
limit: 10,
columns: 'title',
populate: [ 'some_ref', 'other_ref' ],
sortBy: {
title: -1
},
lean: true
}, callback);
// selecting specific field for population
// <http://mongoosejs.com/docs/api.html#query_Query-populate>
MySchema.paginate({}, {
columns: 'title',
populate: [
{
path: 'some_ref',
select: 'field_a field_b'
},
'other_ref'
],
sortBy: {
title: -1
},
lean: true
}, callback);
Tests
npm test
Contributors
- Edward Hotchkiss edwardhotchkiss@me.com
- Nick Baugh niftylettuce@gmail.com
- villesau ville.saukkonen@sulake.com
- Danilo Barsotti danilo.barsotti@musiclize.com
- t_yamo t_yamo@unknown-artifacts.info
- andrew <andrew@andrew-desktop.(none)>
- Alberto Gimeno Brieba gimenete@gmail.com
- zhiqingchen zhiqingchen@anjuke.com
- Alexander Manzyuk admsev@gmail.com
- Gia anggiaj@users.noreply.github.com
- Hrvoje Šimić shime.ferovac@gmail.com
- Mario Colque mario@feegos.com
- Richard van der Dys rhvanderdys@containerstore.com
- Yolanda Septiana yolapopop@gmail.com
- charles bourasseau charles.bourasseau@gmail.com
- giulianoiacobelli giuliano.iacobelli@gmail.com