But I need it to output with the latest document at the bottom like this :
post one
post two
post three
post four
post five
I’ve tried a bunch of different things including $natural and reverse but I just get errors.
I understand that I am flipping the order with my sort command, but I need to do that to only show the last 30 records, so how do I then re-sort the sort?
@robfallows Sorry to be a pain, but could you please explain what that extra sort function is doing now so I can understand it?
My guess is it’s cycling through every document and comparing the dates using greater than and less than operators, but I just want to be sure as that seems like quite a lot of work it’s having to do there for what essentially is a pretty straight forward sort.
You’ve already did a sort in MongoDB. If you badly need to do the 2nd sort using Mongodb, then check aggregate function and create a pipeline. But that makes it more complex than necessary.
Here’s an alternative, which may be faster. Given that your array from fetch() is just reverse ordered, you can use the array prototype reverse() to get the ordering how you want: