From the course: Next.js 14 from Scratch: Build a Real-World Project Using Next.js 14 and MongoDB

Unlock this course with a free trial

Join today to access over 25,200 courses taught by industry experts.

Order new first

Order new first

All right, guys. So now I just want to order these so that the new is always at the top. So we need to go to the API route, and that's going to be app, API, messages, and then route.js. Not the ID, just route.js directly in messages. And this is the get request right now. So we just really need to add a couple lines here. So first of all, where we get the messages, We're going to change this to read messages. And basically, we want to find where the recipient is the user ID. Let me just close that up. So where the recipient is the user ID, but also where read is true. And then I want to sort. Let's go under or right above the populates. And then let's sort. And we're going to pass in here. We want to sort by, let's say, created at. Because not only do we want the new at the top, but we still want the most recent at the top. So we can say created at and set that to minus 1. And what that'll do is sort read messages in ascending order. OK, now we want to get unread messages. So I'm just…

Contents