mongoose-infinite-pagination
v1.1.3
Published
It is used to paginate the bulk datas.
Downloads
86
Maintainers
Readme
mongoose-infinite-pagination
Pagination plugin for Mongoose & expressJS and It is used to paginate the bulk datas.
Note: This plugin will only work with Node.js >= 4.2 and Mongoose >= 4.2
Installation
npm install mongoose-infinite-pagination
Usage
Add plugin to a schema and then use model findWithPaginate
method:
const mongoose = require('mongoose');
const { paginate } = require('mongoose-infinite-pagination');
# OR
const mongooseInfinitePaginate = require('mongoose-infinite-pagination');
var schema = new mongoose.Schema({ /* schema definition & fields */ });
schema.plugin(mongooseInfinitePaginate.paginate);
# OR
schema.plugin(paginate);
var Model = mongoose.model('Model', schema);
Normal Pagination
Model.findWithPaginate(query, options, callback) : Promise
Parameters
[query]
{Object} - Query criteria. Documentation[options]
{Object}[req]
{URL request} - Usereq
to set get the url for next set of results & get the url for previous set of results.[skip=0]
{Number} - Useskip
to set skip position.[limit=10]
{Number} - Uselimit
to set number of results to be obtain.
[callback(err, result)]
- If specified the callback is called once pagination results are retrieved or when an error has occurred
Return value
Promise fulfilled with object having properties:
results
{Array} - Array of documentscount
{Number} - Total number of documents in collection that match a querynext
{Number} - URL for the next set of resultsprevious
{Number} - URL for previous set of results
Examples
Skip 5 documents and return upto 10 documents
Model.findWithPaginate({}, {req : req, skip: 5, limit: 10 }, function(err, result) {
// result.results
// result.count
// result.next
// result.previous
});
Skip 5 documents and return upto 10 documents with select & populate:
Model
.paginateSelect('name') // Select Options same as mongoose select();
.paginatePopulate([populateOpts]) // Populate Options same as mongoose populate();
.paginateSort([sortOpts]) // Sort Options same as mongoose sort();
.findWithPaginate({}, {req : req, skip: 5, limit: 10 }, function(err, result) {
// result.results
// result.count
// result.next
// result.previous
});
Note : Please use paginateSelect() & paginatePopulate() & paginateSort() before findWithPaginate() otherwise it will not work.
With promise:
Model.findWithPaginate({}, {req : req, skip: 3, limit: 10 }).then(function(result) {
// ...
}).catch(err =>{
// ...
});
Paginate with Aggregation
Model.aggregatePaginate(pipelines, options, callback) : Promise
Parameters
[pipelines]
{Array - Aggregate Pipeline criteria. Documentation[options]
{Object}[req]
{URL request} - Usereq
to set get the url for next set of results & get the url for previous set of results.[skip=0]
{Number} - Useskip
to set skip position.[limit=10]
{Number} - Uselimit
to set number of results to be obtain.
[callback(err, result)]
- If specified the callback is called once pagination results are retrieved or when an error has occurred
Return value
Promise fulfilled with object having properties:
results
{Array} - Array of documentscount
{Number} - Total number of documents in collection that match a querynext
{Number} - URL for the next set of resultsprevious
{Number} - URL for previous set of results
Examples
Skip 5 documents and return upto 10 documents
Model.aggregatePaginate([{ <Pipelines Query> }, ...], {req : req, skip: 5, limit: 10 }, function(err, result) {
// result.results
// result.count
// result.next
// result.previous
});
With promise:
Model.aggregatePaginate([{ <Pipelines Query> }, ...], {req : req, skip: 3, limit: 10 }).then(function(result) {
// ...
}).catch(err =>{
// ...
});
Set custom default options for all queries
config.js:
var mongooseInfinitePaginate = require('mongoose-infinite-pagination');
mongooseInfinitePaginate.paginate.options = {
defaulLimit: 10,
defaulSkip: 0,
};
controller.js:
Model
.paginateSelect('name mobile ...')
.paginatePopulate({path : 'userprofile'})
.paginateSort({created : 1})
.findWithPaginate(query).then(function(result) {
// result.docs - array of objects
// result.limit - 10
// result.skip - 0
});
Model
.aggregatePaginate(pipelines,opts).then(function(result) {
// result.docs - array of objects
// result.limit - 10
// result.skip - 0
});
Tests
npm install
npm test