From the course: MongoDB Essential Training

Unlock the full course today

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

Sort, skip, limit

Sort, skip, limit

- [Instructor] Something you use very often when accessing data is sorting, to return the results of a fine query in a sorted manner you can add the sort method after the find method. In this example, I'm going to be using the movies collection and I'm going to just perform a find query and then add the sort method, as the argument to the sort method, I'm going to pass on a document with the field name that I want to sort on, in this case it's the title of the movie. All right, now this is a bit hard to pass cause there's a lot of data. So I'm actually going to run this once more and I'm going to add a second argument to the find query where we project out only the fields that we want, in this case, I'm going to say, we're going keep the title, the director and the genre fields. All right, that's a bit easier to pass. And now you can easily see that the results here are sorted 'cause all of the results' titles start…

Contents