mongoose-aggregate-paginate-v2
A cursor based custom aggregate pagination library for Mongoose with customizable labels.
If you are looking for basic query pagination library without aggregate, use this one mongoose-paginate-v2
Installation
npm install mongoose-aggregate-paginate-v2
Usage
Adding the plugin to a schema,
var mongoose = require('mongoose');
var aggregatePaginate = require('mongoose-aggregate-paginate-v2');
var mySchema = new mongoose.Schema({
});
mySchema.plugin(aggregatePaginate);
var myModel = mongoose.model('SampleModel', mySchema);
and then use model aggregatePaginate
method,
var myModel = require('/models/samplemodel');
const options = {
page: 1,
limit: 10
};
var myAggregate = myModel.aggregate();
myModel.aggregatePaginate(myAggregate, options).then(function(results){
console.log(results);
}).catch(function(err){
console.log(err);
})
var myModel = require('/models/samplemodel');
const options = {
page: 1,
limit: 10
};
var myAggregate = myModel.aggregate();
myModel.aggregatePaginate(myAggregate, options, function(err, results) {
if(err) {
console.err(err);
else {
console.log(results);
}
})
Model.aggregatePaginate([aggregateQuery], [options], [callback])
Returns promise
Parameters
[aggregate-query]
{Object} - Aggregate Query criteria. Documentation[options]
{Object}
[sort]
{Object | String} - Sort order. Documentation[page=1]
{Number} - Current Page.[limit=10]
{Number} - Docs. per page.[customLabels]
{Object} - Developers can provide custom labels for manipulating the response data.[allowDiskUse]
{Bool} - To enable diskUse for bigger queries. (default=false)
[callback(err, result)]
- (Optional) 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:
docs
{Array} - Array of documentstotalDocs
{Number} - Total number of documents that match a querylimit
{Number} - Limit that was usedpage
{Number} - Current page numbertotalPages
{Number} - Total number of pages.hasPrevPage
{Bool} - Availability of prev page.hasNextPage
{Bool} - Availability of next page.prevPage
{Number} - Previous page number if available or NULLnextPage
{Number} - Next page number if available or NULLpagingCounter
{Number} - The starting sl. number of first document.
Please note that the above properties can be renamed by setting customLabel attribute.
Sample Usage
Return first 10 documents from 100
const options = {
page: 1,
limit: 10
};
var aggregate = Model.aggregate();
Model.aggregatePaginate(aggregate, options)
.then(function(result) {
})
.catch(function(err){
console.log(err);
});
With custom return labels
Now developers can specify the return field names if they want. Below are the list of attributes whose name can be changed.
- totalDocs
- docs
- limit
- page
- nextPage
- prevPage
- totalPages
- hasNextPage
- hasPrevPage
- pagingCounter
You should pass the names of the properties you wish to changes using customLabels
object in options. Labels are optional, you can pass the labels of what ever keys are you changing, others will use the default labels.
Same query with custom labels
const myCustomLabels = {
totalDocs: 'itemCount',
docs: 'itemsList',
limit: 'perPage',
page: 'currentPage',
nextPage: 'next',
prevPage: 'prev',
totalPages: 'pageCount',
hasPrevPage: 'hasPrev',
hasNextPage: 'hasNext',
pagingCounter: 'pageCounter'
};
const options = {
page: 1,
limit: 10,
customLabels: myCustomLabels
};
var aggregate = Model.aggregate();
Model.aggregatePaginate(aggregate, options, function(err, result) {
if(!err) {
} else {
console.log(err);
};
License
MIT