Skip to content

Commit d65250f

Browse files
committed
perf: added match stage before facet to use indexing
1 parent 419db51 commit d65250f

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

lib/mongoose-aggregate-paginate.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ function aggregatePaginate(query, options, callback) {
8989
const allowDiskUse = options.allowDiskUse || false;
9090
const isPaginationEnabled = options.pagination === false ? false : true;
9191

92-
const q = this.aggregate();
92+
let q = this.aggregate();
9393

9494
if (query.options) q.options = query.options;
9595

@@ -125,7 +125,13 @@ function aggregatePaginate(query, options, callback) {
125125

126126
let promise;
127127
if (options.useFacet && !options.countQuery) {
128-
const [pipeline, countPipeline] = constructPipelines();
128+
let [pipeline, countPipeline] = constructPipelines();
129+
const match = pipeline[0]?.$match;
130+
if (match) {
131+
pipeline.shift();
132+
countPipeline.shift();
133+
q = q.match(match);
134+
}
129135
promise = q
130136
.facet({
131137
docs: pipeline,

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mongoose-aggregate-paginate-v2",
3-
"version": "1.1.0",
3+
"version": "1.1.1",
44
"description": "A cursor based custom aggregate pagination library for Mongoose with customizable labels.",
55
"main": "index.js",
66
"types": "types/index.d.ts",

0 commit comments

Comments
 (0)