I'm creating a chat app using MongoDB. Basically I need to retrieve the latest 20 chat messages and then display them by ascending date. In order to get the 20 last chat messages my code looks something like this:
db.messages.find().sort({$date:-1}).limit(20)
but this leaves the messages in reverse order.
Is there a way to do this in a single query? I know this is incorrect but something like db.messages.find().sort({$date:-1}).limit(20).sort({$date:1}) ?