1 00:00:02,230 --> 00:00:05,010 Now sorting is one thing we can do, 2 00:00:05,170 --> 00:00:09,310 we can also skip a certain amount of elements. 3 00:00:09,310 --> 00:00:10,650 Now why would you skip? 4 00:00:10,870 --> 00:00:17,840 Let's say in your client application, a website, you implement some pagination, so people can view the 5 00:00:17,850 --> 00:00:23,020 results which are distributed across multiple pages because you don't want to have one page with 100 6 00:00:23,080 --> 00:00:26,530 results but you want to have 10 on every page. 7 00:00:26,530 --> 00:00:32,230 Now if the user switches to page number two, you want to skip the first 10 results which would have been 8 00:00:32,230 --> 00:00:35,980 on page number one but now you want to display the results for page number two 9 00:00:36,370 --> 00:00:41,500 and therefore, you can add a skip method here to skip results. 10 00:00:41,620 --> 00:00:48,040 So let's save and add skip ten here, you will see that the results here are slightly different, 11 00:00:48,040 --> 00:00:49,970 we got 6.6 ratings 12 00:00:50,020 --> 00:00:55,320 if you remember previously in the same amount of documents, we had 6.4 and so on, 13 00:00:55,330 --> 00:00:59,320 so it looks like we skipped some documents and indeed we did. 14 00:00:59,320 --> 00:01:03,290 So skipping does allow us to move through our data set, 15 00:01:03,340 --> 00:01:09,580 if I skip 100, then we can see the ratings are much higher because we skipped all the bad ones, the 100 16 00:01:09,580 --> 00:01:12,060 bad ones, we skipped them, 17 00:01:12,060 --> 00:01:21,290 now skip is also used on a cursor as you can tell. Now related to skip somehow is the limit function, limit 18 00:01:21,290 --> 00:01:26,640 allows you to limit the amount of elements the cursor should retrieve at a time 19 00:01:26,810 --> 00:01:31,940 and that also means the amount of documents you can then move through with a cursor. 20 00:01:31,940 --> 00:01:34,570 You can limit this to 10 for example, 21 00:01:34,610 --> 00:01:39,570 the interesting thing is if you here add count, you still have 240 22 00:01:39,800 --> 00:01:49,310 but if I now execute pretty here, indeed the cursor is not fully exhausted but I only get 10 elements 23 00:01:49,430 --> 00:01:50,540 with the help of limit, 24 00:01:50,570 --> 00:01:56,540 so limit also allows us to retrieve a certain amount of documents but only the amount of documents we 25 00:01:56,540 --> 00:01:57,630 specify. 26 00:01:57,680 --> 00:02:00,070 This also is related to pagination, 27 00:02:00,080 --> 00:02:06,160 if you had your website with 10 products per page, it doesn't suffice to skip 10 products because you're 28 00:02:06,160 --> 00:02:07,360 on page number two, 29 00:02:07,460 --> 00:02:13,250 you also only want to display 10 products on page number two and that is exactly what limit helps you 30 00:02:13,250 --> 00:02:13,820 do, 31 00:02:13,820 --> 00:02:19,150 it limits the amount of data that is retrieved and returned through the cursor. 32 00:02:19,260 --> 00:02:21,950 Now there is maybe one thing which you thought about, 33 00:02:22,130 --> 00:02:30,900 if we have this query where we sort and I do actually sort twice here, I didn't notice that, as you see 34 00:02:30,900 --> 00:02:33,490 it didn't do any harm but of course it is redundant, 35 00:02:33,490 --> 00:02:40,790 so if we have a query where we sort and then we skip and then we limit, does the order matter? 36 00:02:40,810 --> 00:02:43,090 In theory you would say it should matter right 37 00:02:43,120 --> 00:02:48,560 because if I skip before I sort, then I skip on the unsorted documents, 38 00:02:48,580 --> 00:02:55,990 so I simply skip the first 100 in the collection and I only sort the remaining ones which means I don't 39 00:02:55,990 --> 00:03:02,800 necessarily skip the badly rated ones but I simply skipped some random items and I only sort after 40 00:03:02,800 --> 00:03:03,420 skipping 41 00:03:03,490 --> 00:03:06,550 which of course is typically not what you want. 42 00:03:06,550 --> 00:03:10,710 Now the good thing is on a cursor, the order here does not matter, 43 00:03:10,870 --> 00:03:16,090 later in the aggregation framework, this will change and I will talk about it there but here, you can 44 00:03:16,090 --> 00:03:19,310 have skip, sort and limit in whichever order you want. 45 00:03:19,330 --> 00:03:21,420 You can limit first, you can skip first, 46 00:03:21,440 --> 00:03:25,380 mongodb and the cursor will automatically do it in the right order, 47 00:03:25,390 --> 00:03:28,930 it will sort first, then skip and then limit.