Skip to content

Commit

Permalink
perf: added match stage before facet to use indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
Akalanka47000 committed Jul 5, 2024
1 parent 419db51 commit 881fe64
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/mongoose-aggregate-paginate.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ function aggregatePaginate(query, options, callback) {
const allowDiskUse = options.allowDiskUse || false;
const isPaginationEnabled = options.pagination === false ? false : true;

const q = this.aggregate();
let q = this.aggregate();

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

Expand Down Expand Up @@ -125,7 +125,13 @@ function aggregatePaginate(query, options, callback) {

let promise;
if (options.useFacet && !options.countQuery) {
const [pipeline, countPipeline] = constructPipelines();
let [pipeline, countPipeline] = constructPipelines();
const match = pipeline[0]?.$match;
if (match) {
pipeline.shift();
countPipeline.shift();
q = q.match(match);
}
promise = q
.facet({
docs: pipeline,
Expand Down

0 comments on commit 881fe64

Please sign in to comment.