Skip to content

Commit

Permalink
Merge pull request #61 from sliit-foss/master
Browse files Browse the repository at this point in the history
Added match stage before facet to improve performance
  • Loading branch information
aravindnc committed Jul 5, 2024
2 parents 419db51 + d65250f commit dde53c3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mongoose-aggregate-paginate-v2",
"version": "1.1.0",
"version": "1.1.1",
"description": "A cursor based custom aggregate pagination library for Mongoose with customizable labels.",
"main": "index.js",
"types": "types/index.d.ts",
Expand Down

0 comments on commit dde53c3

Please sign in to comment.