Skip to content

Commit

Permalink
Added support for old collection.count() which will support $geo quer…
Browse files Browse the repository at this point in the history
…ies.
  • Loading branch information
aravindnc committed Apr 1, 2020
1 parent 852b3fd commit ebdb194
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mongoose-paginate-v2",
"version": "1.3.8",
"version": "1.3.9",
"description": "A cursor based custom pagination library for Mongoose with customizable labels.",
"main": "dist/index.js",
"scripts": {
Expand Down
14 changes: 11 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ const defaultOptions = {
projection: {},
select: '',
options: {},
pagination: true
pagination: true,
forceCountFn: false
};

function paginate(query, options, callback) {
Expand All @@ -60,7 +61,8 @@ function paginate(query, options, callback) {
read,
select,
sort,
pagination
pagination,
forceCountFn
} = options;

const customLabels = {
Expand Down Expand Up @@ -104,7 +106,13 @@ function paginate(query, options, callback) {
skip = offset;
}

const countPromise = this.countDocuments(query).exec();
let countPromise;

if (forceCountFn === true) {
countPromise = this.count(query).exec();
} else {
countPromise = this.countDocuments(query).exec();
}

if (limit) {
const mQuery = this.find(query, projection, findOptions);
Expand Down
9 changes: 4 additions & 5 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,11 +389,9 @@ describe('mongoose-paginate', function () {
it('2dsphere', function () {
var query = {
loc: {
$geoWithin: {
$center: [
[-10, 20], 999
]
}
$nearSphere:
[ 50, 50 ]

}
};

Expand All @@ -413,6 +411,7 @@ describe('mongoose-paginate', function () {
title: 1,
price: 1
},
forceCountFn: true,
customLabels: myCustomLabels
};
return Book.paginate(query, options).then((result) => {
Expand Down

0 comments on commit ebdb194

Please sign in to comment.