diff --git a/package.json b/package.json index fdebcce..ac7ef44 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,11 @@ { "name": "mongoose-paginate-v2", - "version": "1.3.1", + "version": "1.3.2", "description": "A cursor based custom pagination library for Mongoose with customizable labels.", "main": "dist/index.js", "scripts": { "pretest": "npm run build", - "test": "./node_modules/.bin/mocha tests/*.js -R spec --ui bdd --timeout 5000", + "test": "./node_modules/.bin/mocha tests/*.js -R spec --ui bdd --timeout 9999999", "build": "babel src -d dist", "prepublish": "npm run test" }, @@ -65,4 +65,4 @@ "engines": { "node": ">=4.0.0" } -} \ No newline at end of file +} diff --git a/src/index.js b/src/index.js index 53f6404..7050733 100644 --- a/src/index.js +++ b/src/index.js @@ -102,7 +102,19 @@ function paginate(query, options, callback) { } // const countPromise = this.countDocuments(query).exec(); - const countPromise = this.find(query).exec(); + // const countPromise = this.find(query).exec(); + + const countQuery = this.find(query); + if (populate) { + countQuery.populate(populate); + } + + // Hack for mongo < v3.4 + if (Object.keys(collation).length > 0) { + countQuery.collation(collation); + } + + const countPromise = countQuery.exec(); if (limit) { const mQuery = this.find(query, projection, findOptions); @@ -115,12 +127,12 @@ function paginate(query, options, callback) { mQuery.collation(collation); } - mQuery.skip(skip); - mQuery.limit(limit); - if (populate) { mQuery.populate(populate); } + + mQuery.skip(skip); + mQuery.limit(limit); docsPromise = mQuery.exec();