1 00:00:02,210 --> 00:00:05,540 Now we worked with cursors in the last lectures, 2 00:00:05,570 --> 00:00:12,620 now there is something else we can do with the results this cursor stores or give us back to be 3 00:00:12,620 --> 00:00:13,830 precise. 4 00:00:13,850 --> 00:00:21,400 Now what can we do there? If I have a look at my movies and I find them all like this, 5 00:00:21,500 --> 00:00:22,830 I get a bunch of movies obviously 6 00:00:22,910 --> 00:00:25,770 and if I pretty print them, I well get them in a nicer form. 7 00:00:25,790 --> 00:00:29,480 Now here please note I'm not handling the cursor manually, 8 00:00:29,480 --> 00:00:32,910 I'll let the shell do that and let the shell fetch the first 20. 9 00:00:33,260 --> 00:00:38,450 Now of course, a common operation would be that you want to sort the results you get back, 10 00:00:38,630 --> 00:00:45,080 you can sort by anything, you can take a string and it will be sorted alphabetically or you can sort 11 00:00:45,080 --> 00:00:51,010 by a number and then it will obviously be sorted by that number, let's say by rating. 12 00:00:51,080 --> 00:00:59,880 Now we can sort by adding .sort if we use pretty, before pretty but always after find, now sort 13 00:00:59,880 --> 00:01:03,470 takes a document where you describe how to sort. 14 00:01:03,470 --> 00:01:11,460 Now here we could say we want to sort by the average rating, so rating.average and that already shows 15 00:01:11,460 --> 00:01:16,800 us that we can also sort by a field in an embedded document, you could of course also sort by a top 16 00:01:16,800 --> 00:01:17,620 level field. 17 00:01:18,030 --> 00:01:25,980 So here we can sort by rating.average, now the value you specify here describes the direction in which 18 00:01:25,980 --> 00:01:34,200 you sort, one means ascending, minus one means descending, so if I use minus one here, 19 00:01:34,650 --> 00:01:39,740 I actually have the highest ratings first 8.8, 8.8 20 00:01:39,750 --> 00:01:43,960 and if we scroll up a little bit further ahead, we see nine and so on 21 00:01:44,160 --> 00:01:48,440 and of course as I mentioned, we can also sort in the opposite direction 22 00:01:48,480 --> 00:01:54,710 and now we'll have the worse items first. Yor're also not limited to one sorting criteria, 23 00:01:54,910 --> 00:01:59,260 you could say you want to sort by the rating first and the order does matter, 24 00:01:59,290 --> 00:02:01,440 so it will first be sorted by rating 25 00:02:01,600 --> 00:02:10,480 but then within each rating so to say, you want to sort by runtime, also ascending or descending so let's 26 00:02:10,480 --> 00:02:11,890 say descending. 27 00:02:12,010 --> 00:02:16,900 Now what we have this we have our ratings, 6.4, 6.4 28 00:02:17,170 --> 00:02:21,090 but inside of that rating, it will actually sort by runtime. 29 00:02:21,100 --> 00:02:23,540 Now here I have the same runtime, here 30 00:02:23,550 --> 00:02:27,260 however I have 30 minutes in a 6.3 rating, 31 00:02:27,310 --> 00:02:34,720 here again I have 6.3 with 60 minutes and hence, 60 minutes comes first before 30 because I'm sorting 32 00:02:34,840 --> 00:02:37,070 the runtime in a descending order, 33 00:02:37,120 --> 00:02:39,550 so higher values first. 34 00:02:40,060 --> 00:02:47,590 So you can combine multiple, as many as you want sorting criteria here and sorting of course is a pretty 35 00:02:47,590 --> 00:02:54,070 common task you do when you work with data because it allows you to not just narrow down the set of 36 00:02:54,070 --> 00:02:57,960 data you want but then also retrieve it in the right order. 37 00:02:57,970 --> 00:03:00,510 Now sort is available on this cursor, 38 00:03:00,520 --> 00:03:05,740 it's not available for findOne for obvious reasons because findOne only gives you one document, 39 00:03:05,760 --> 00:03:06,970 there is nothing to sort.