npm

mongoose-paginations
TypeScript icon, indicating that this package has built-in type declarations

1.0.6 • Public • Published

mongoose-paginations

Pagination plugin for Mongoose & expressJS and It is used to paginate the bulk datas.

NPM version Build status

Note: This plugin will only work with Node.js >= 4.2 and Mongoose >= 4.2

NPM

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} - Use req to set get the url for next set of results & get the url for previous set of results.
    • [page=2] {Number} - Use page to get results based on page number (If use page option, then it will ignore the skip option).
    • [limit=10] {Number} - Use limit to set number of results to be obtain.
    • [skip=0] {Number} - Use skip to set skip position.
    • [is_secure=true] {Boolean} - Use is_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 documents
  • count {Number} - Total number of documents in collection that match a query
  • next {Number} - URL for the next set of results
  • previous {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} - Use req to set get the url for next set of results & get the url for previous set of results.
    • [skip=0] {Number} - Use skip to set skip position.
    • [limit=10] {Number} - Use limit 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 documents
  • count {Number} - Total number of documents in collection that match a query
  • next {Number} - URL for the next set of results
  • previous {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

License

MIT

Package Sidebar

Install

npm i mongoose-paginations

Weekly Downloads

19

Version

1.0.6

License

MIT

Unpacked Size

37.7 kB

Total Files

9

Last publish

Collaborators

  • dxmari