1 00:00:02,130 --> 00:00:06,610 Now this is actually it for the query selectors and operators. 2 00:00:06,610 --> 00:00:12,910 Let's now briefly come back to that cursor thing which I already touched on earlier in this course, the 3 00:00:12,910 --> 00:00:17,500 find method unlike the findOne method, yields us a cursor 4 00:00:17,500 --> 00:00:18,750 if you remember. 5 00:00:19,060 --> 00:00:26,290 Now why is that important and why does it do that? Well if we have our client communicate with the mongodb 6 00:00:26,290 --> 00:00:27,470 server, 7 00:00:27,740 --> 00:00:33,500 we potentially get back thousands or even millions of documents with find, especially if we have no 8 00:00:33,500 --> 00:00:34,250 condition in there 9 00:00:34,250 --> 00:00:40,100 but even with a condition, you easily have a condition that still meets like 1000 documents or more 10 00:00:40,190 --> 00:00:42,350 depending on the scale of your app. 11 00:00:42,410 --> 00:00:47,690 So you get back all these results and that is very inefficient because all these results have to be 12 00:00:47,690 --> 00:00:49,640 fetched from the database, 13 00:00:49,640 --> 00:00:56,130 they have to be sent over the wire and then they have to be loaded into memory in your client application. 14 00:00:56,300 --> 00:01:02,750 So these are three things that are not optimal because chances are you will not need all thouand documents 15 00:01:02,810 --> 00:01:11,840 at the same time and therefore, find gives you a cursor. A cursor is basically a pointer which has the 16 00:01:11,930 --> 00:01:17,600 query you wrote stored and which can therefore quickly go to the database and say hey, give me the next 17 00:01:17,600 --> 00:01:17,980 batch, 18 00:01:18,020 --> 00:01:19,370 give me the next batch 19 00:01:19,370 --> 00:01:28,110 and indeed you work with batches of data then. You fetch the data one by one, so one document by a time 20 00:01:28,160 --> 00:01:32,540 and it really is only transferred over the wire when you request the next one. 21 00:01:32,720 --> 00:01:39,470 Now in the shell, we get 20 by default because the shell automatically basically takes the cursor and gets 22 00:01:39,470 --> 00:01:42,370 us the first 20 documents before we can get more. 23 00:01:42,440 --> 00:01:48,230 If you write an application with a mongodb driver and we'll see that in the from driver to 24 00:01:48,230 --> 00:01:49,300 shell module 25 00:01:49,400 --> 00:01:54,980 at the end of the course, then you have to control that cursor manually and make sure you get back your 26 00:01:54,980 --> 00:01:57,590 results. And that cursor approach is 27 00:01:57,590 --> 00:01:59,950 great because it saves resources. 28 00:02:00,110 --> 00:02:06,650 If you have a query that meets 1000 documents, but let's say you have a website where you only display 29 00:02:06,650 --> 00:02:13,850 10 items, let's say 10 products you fetched at a time anyways, then there is no need to load all thousand 30 00:02:13,850 --> 00:02:16,920 results that matched your query right at the start. 31 00:02:16,940 --> 00:02:19,920 Instead you would only fetch the first 10, 32 00:02:19,920 --> 00:02:24,360 display them on the screen and then go ahead and fetch the next 10 33 00:02:24,380 --> 00:02:30,110 when the user navigated to the next page or anything like that. This is the idea behind a cursor, 34 00:02:30,140 --> 00:02:33,130 now we'll find out how to work with a cursor 35 00:02:33,290 --> 00:02:39,270 later as I mentioned when I talk about how you use the mongodb driver in a real application 36 00:02:39,350 --> 00:02:42,630 but I can already show you some things in this module. 37 00:02:42,710 --> 00:02:45,320 So in the next lectures, we'll play around with the cursor a bit.