aravindnc / mongoose-aggregate-paginate-v2

A cursor based custom aggregate pagination library for Mongoose with customizable labels.
MIT License
131 stars 23 forks source link

How to optimize a $match'less paginate query? #53

Open vresetnikov opened 1 year ago

vresetnikov commented 1 year ago

Hello,

So the library allows to pass in an aggregation query that shapes the paginated results. One thing that I cannot seem to understand, is that since we are retrieving multiple unknown aggregated objects, the $match stage appears to be off the table. This makes more complex aggregation queries (such as the ones using $lookup stage very slow). For example:

Let's say I have products and containers collections. Products point to containers like so:

Product collection

{
_id: <mongo id>,
container: <container document id>,
<.. other product details>
}

Now, if I wanted to retrieve a specific container and $lookup its products, the query would be very fast because I could put $match in the beginning:

[MATCH container by container ID] [LOOKUP products that share the container id] [PROJECT]

However, with mongoose-aggregate-paginate-v2, I am now retrieving multiple resources, so the MATCH stage is lost, making the LOOKUP very slow?

Is there a way to counter this? What should I do?