mongoose-paginations
v1.0.6
Published
It is used to paginate the bulk datas.
Downloads
64
Maintainers
Keywords
Readme
mongoose-paginations
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-paginations
Usage
Add plugin to a schema and then use model findWithPaginate
method:
const mongoose = require('mongoose');
const { paginate } = require('mongoose-paginations');
# OR
const mongooseInfinitePaginate = require('mongoose-paginations');
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.[page=2]
{Number} - Usepage
to get results based on page number (If usepage
option, then it will ignore theskip
option).[limit=10]
{Number} - Uselimit
to set number of results to be obtain.[skip=0]
{Number} - Useskip
to set skip position.[is_secure=true]
{Boolean} - Useis_secure
to set the url as secure(convert http to https).
[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-paginations');
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