Answer the question
In order to leave comments, you need to log in
Why do skip and limit in aggregation pipeline work differently than regular find?
I used to paginate with
await this.PupilModel.find().limit(limit).skip(offset) // limit and offset arrive as requset.query
And if I wanted to let's open a third page, given that on one I display 10 elements, I needed to set it limit
to 10, and offset
to 20.
But then it was necessary to do filtering and I use MongoDB Aggregation for this purpose.
const result = await this.PupilModel.aggregate(
this.createFilterPipeline(filters) || [{ $match: {} }]
)
.limit(limit)
.skip(offset);
limit
will be in the value 10, like offset
. I execute the request and get an empty array: limit
it should be 20, and offset
10, and if the third page, then limit
= 30, offset
= 20 Answer the question
In order to leave comments, you need to log in
The aggregation pipeline is executed one by one, first you limit the output limit and you have only 10 records in the output, and then you move the cursor 10 forward and get a void, so in the output after 10 non-records
you need to do this in the aggregation:
either like this :
[
{ "$limit": skip + limit },
{ "$skip": skip }
]
or like this
[
{ "$skip": skip }
{ "$limit": limit },
]
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question