Skip to content

Commit d4ecf92

Browse files
authored
Merge pull request #62 from MattLicense/master
fix: non-facet aggregations use correct countQuery to get total numbe…
2 parents 01fe8c1 + c43e303 commit d4ecf92

File tree

3 files changed

+50
-8
lines changed

3 files changed

+50
-8
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,5 @@ typings/
6060
# next.js build output
6161
.next
6262

63-
dist
63+
dist
64+
.idea/

lib/mongoose-aggregate-paginate.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ function aggregatePaginate(query, options, callback) {
102102
}
103103

104104
function constructPipelines() {
105-
let cleanedPipeline = pipeline.filter((stage) => stage !== PREPAGINATION_PLACEHOLDER);
105+
let cleanedPipeline = pipeline.filter(
106+
(stage) => stage !== PREPAGINATION_PLACEHOLDER
107+
);
106108

107109
const countPipeline = [...cleanedPipeline, { $count: "count" }];
108110

@@ -122,7 +124,6 @@ function aggregatePaginate(query, options, callback) {
122124
return [cleanedPipeline, countPipeline];
123125
}
124126

125-
126127
let promise;
127128
if (options.useFacet && !options.countQuery) {
128129
let [pipeline, countPipeline] = constructPipelines();
@@ -135,14 +136,15 @@ function aggregatePaginate(query, options, callback) {
135136
promise = q
136137
.facet({
137138
docs: pipeline,
138-
count: countPipeline
139+
count: countPipeline,
139140
})
140141
.then(([{ docs, count }]) => [docs, count]);
141142
} else {
143+
const [pipeline, countPipeline] = constructPipelines();
142144

143-
const [pipeline] = constructPipelines();
144-
145-
const countQuery = options.countQuery ? options.countQuery : this.aggregate(pipeline);
145+
const countQuery = options.countQuery
146+
? options.countQuery
147+
: this.aggregate(countPipeline);
146148

147149
if (allowDiskUse) {
148150
countQuery.allowDiskUse(true);
@@ -236,4 +238,4 @@ function aggregatePaginate(query, options, callback) {
236238

237239
module.exports = aggregatePaginate;
238240

239-
module.exports.PREPAGINATION_PLACEHOLDER = PREPAGINATION_PLACEHOLDER;
241+
module.exports.PREPAGINATION_PLACEHOLDER = PREPAGINATION_PLACEHOLDER;

tests/index.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,45 @@ describe("mongoose-paginate", function () {
267267
expect(result.pageCount).to.equal(5);
268268
});
269269
});
270+
271+
it("without facet", () => {
272+
// part of the reason for this test is due to working the usage of $search in the aggregation pipeline which is
273+
// not supported with facets. The building of a query without facets previously had a bug that meant the pagination
274+
// details were based on a single page of results, rather than the full count of results from a pipeline. While
275+
// this is a basic test it verifies the pagination still returns the correct result while not using facets.
276+
var aggregate = Book.aggregate([
277+
{
278+
$match: {
279+
title: {
280+
$in: [/Book/i],
281+
},
282+
},
283+
},
284+
{
285+
$sort: {
286+
date: 1,
287+
},
288+
},
289+
]);
290+
var options = {
291+
limit: 10,
292+
page: 2,
293+
useFacet: false,
294+
};
295+
return Book.aggregatePaginate(aggregate, options).then((result) => {
296+
expect(result.docs).to.have.length(10);
297+
expect(result.docs[0].title).to.equal("Book #41");
298+
expect(result.totalDocs).to.equal(100);
299+
expect(result.limit).to.equal(10);
300+
expect(result.page).to.equal(2);
301+
expect(result.pagingCounter).to.equal(41);
302+
expect(result.hasPrevPage).to.equal(true);
303+
expect(result.hasNextPage).to.equal(true);
304+
expect(result.prevPage).to.equal(1);
305+
expect(result.nextPage).to.equal(3);
306+
expect(result.totalPages).to.equal(10);
307+
});
308+
});
270309
});
271310

272311
after(async function () {

0 commit comments

Comments
 (0)