Skip to content

Commit

Permalink
Merge 9eda33c into 6710cb2
Browse files Browse the repository at this point in the history
  • Loading branch information
algun committed Jul 24, 2018
2 parents 6710cb2 + 9eda33c commit a34b57b
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 120 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ This plugin must first be added to a schema:

```js

var mongooseAggregatePaginate = require('mongoose-aggregate-paginate');
let mongooseAggregatePaginate = require('mongoose-aggregate-paginate');

mySchema.plugin(mongooseAggregatePaginate);

Expand All @@ -43,8 +43,8 @@ mySchema.plugin(mongooseAggregatePaginate);
* `options` - An object with options for the [Mongoose][mongoose] query, such as sorting
- `page` - Default: `1`
- `limit` - Default: `10`
- `sortBy` - Default: `undefined`
* `callback(err, results, pageCount, itemCount)` - A callback is called once pagination results are retrieved, or an error has occurred. If not specified promise will be returned
- `sort` - Default: `undefined`
* `callback(err, results, pages, total)` - A callback is called once pagination results are retrieved, or an error has occurred. If not specified promise will be returned

**Returns**
* `Promise` - Promise object
Expand All @@ -53,20 +53,20 @@ mySchema.plugin(mongooseAggregatePaginate);

```js

var MyModel = mongoose.model('MyModel',{
let MyModel = mongoose.model('MyModel',{
name : String,
age: Number,
city, String
})

// find users above 18 by city
var aggregate = MyModel.aggregate();
let aggregate = MyModel.aggregate();
aggregate.match({age : {'lt' : 18 } })
.group({ _id: '$city' , count : { '$sum' : 1 } })
var options = { page : 1, limit : 15}
let options = { page : 1, limit : 15}

// callback
MyModel.aggregatePaginate(aggregate, options, function(err, results, pageCount, count) {
MyModel.aggregatePaginate(aggregate, options, function(err, results, pages, count) {
if(err)
{
console.err(err)
Expand All @@ -80,7 +80,7 @@ MyModel.aggregatePaginate(aggregate, options, function(err, results, pageCount,
// Promise
MyModel.aggregatePaginate(aggregate, options)
.then(function(value) {
console.log(value.data, value.pageCount, value.totalCount)
console.log(value.docs, value.pages, value.total)
})
.catch(function(err){
console.err(err)
Expand Down
36 changes: 22 additions & 14 deletions lib/mongoose-aggregate-paginate.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,46 @@
/**
* Paginate Mongoose aggregate result
* @param {Aggregate} aggregate
* @param {any} options {page: number/string default 10, limit: number/string default 10,sortBy: any default null}
* @param {any} options {page: number/string default 10, limit: number/string default 10,sort: any default null}
* @param {function} [callback]
* @returns {Promise}
*/
function aggregatePaginate (aggregate, options, callback) {
options = options || {}
var pageNumber = parseInt(options.page || 1, 10)
var resultsPerPage = parseInt(options.limit || 10, 10)
var skipDocuments = (pageNumber - 1) * resultsPerPage
var sortBy = options.sortBy
let pageNumber = parseInt(options.page || 1, 10)
let resultsPerPage = parseInt(options.limit || 10, 10)
let skipDocuments = (pageNumber - 1) * resultsPerPage
let sort = options.sort

var q = this.aggregate(aggregate._pipeline)
var countQuery = this.aggregate(q._pipeline)
let q = this.aggregate(aggregate._pipeline)
let countQuery = this.aggregate(q._pipeline)
if (q.hasOwnProperty('options')) {
q.options = aggregate.options
countQuery.options = aggregate.options
}

if (sortBy) {
q.sort(sortBy)
if (sort) {
q.sort(sort)
}

return Promise.all([q.skip(skipDocuments).limit(resultsPerPage).exec(), countQuery.group({ _id: null, count: { $sum: 1 } }).exec()])
return Promise.all([
q.skip(skipDocuments).limit(resultsPerPage).exec(),
countQuery.group({
_id: null,
count: {$sum: 1}
}).exec()
])
.then(function (values) {
var count = values[1][0] ? values[1][0].count : 0
let count = values[1][0] ? values[1][0].count : 0
if (typeof callback === 'function') {
return callback(null, values[0], Math.ceil(count / resultsPerPage) || 1, values[1][0] ? count : 0)
}
return Promise.resolve({
data: values[0],
pageCount: (Math.ceil(count / resultsPerPage) || 1),
totalCount: count
docs: values[0],
total: count,
limit: resultsPerPage,
page: pageNumber,
pages: (Math.ceil(count / resultsPerPage) || 1)
})
})
.catch(function (reject) {
Expand Down

0 comments on commit a34b57b

Please sign in to comment.