mongoose-paginate-v2 icon indicating copy to clipboard operation
mongoose-paginate-v2 copied to clipboard

Sort doesn't work with mongoose-paginate (but works with vanilla mongoose)

Open JoshuaPerk opened this issue 3 years ago • 5 comments

I have a collection of calendar Events and in their schema is a start key which holds a proper date. If I run a vanilla mongoose query such as...

Event.find(filter).sort( { start: 1 } ).limit(250)

...as expected, I get a list of events, sorted oldest to newest.

However, when I run...

const options = {
   sort: 'start'
   // or sort: { start: 1 }
}
Event.paginate(filter, options)

...I successfully have paginated results returned; however, they're not in any specific order.

I've attempted to make start an index at both the field and schema level but to no avail. Am I missing a step or syntax?

JoshuaPerk avatar Dec 08 '21 03:12 JoshuaPerk

I just came from mongoose-aggregate-paginate-v2 here because it seems it's bugged when it comes to sorting, and now I'm reading this. Please if someone can confirm if this is a confirmed bug?

Actually I realised I don't need aggregation in my query and this paginate-v2 would suit me really well because it has same output fields as aggregate version, so I don't have to change my frontend. But I do need sorting of paginated results pretty badly...

trancephorm avatar Dec 17 '21 00:12 trancephorm

@JoshuaPerk Seems sorting issue is happening for random users. I've tested with different scenarios and didn't get any issues till now.

aravindnc avatar Dec 18 '21 06:12 aravindnc

I'm also getting this issue, is there any workaround or fix coming? I'm using Mongoose v6.1.4. Thanks

mryraghi avatar Dec 30 '21 14:12 mryraghi

Same issue. any luck so far?

divyeshdhokiya avatar Oct 02 '22 13:10 divyeshdhokiya

What I tried here that worked for me was including _id in the sort fields

orimdominic avatar Sep 15 '23 13:09 orimdominic