From 42d58274503d074c96833678e298143eaf65ee0c Mon Sep 17 00:00:00 2001 From: Akalanka Perera Date: Thu, 4 Jul 2024 23:14:44 +0530 Subject: [PATCH] Patch(aggregate-paginate): added match stage before facet to use indexing --- plugins/mongoose-aggregate-paginate-v2/src/core.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/plugins/mongoose-aggregate-paginate-v2/src/core.js b/plugins/mongoose-aggregate-paginate-v2/src/core.js index 08c94c3..58e5cb2 100644 --- a/plugins/mongoose-aggregate-paginate-v2/src/core.js +++ b/plugins/mongoose-aggregate-paginate-v2/src/core.js @@ -86,7 +86,7 @@ export 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 (allowDiskUse) { q.allowDiskUse(true); @@ -120,7 +120,13 @@ export 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,