1 00:00:02,230 --> 00:00:11,320 To play around with cursors, I'll use my movie data database and there, remember we had movies where 2 00:00:11,690 --> 00:00:18,280 if I counted, we have 240 documents. By the way count is already one method we can chain on a cursor because 3 00:00:18,330 --> 00:00:19,610 find gives us a cursor, 4 00:00:19,610 --> 00:00:25,820 so whatever we call here is called on the cursor and count simply has a look at the cursor and determines 5 00:00:25,820 --> 00:00:29,020 how many elements can I get from the cursor, 6 00:00:29,060 --> 00:00:30,590 so that's important. 7 00:00:30,620 --> 00:00:31,880 How does this work technically? 8 00:00:31,940 --> 00:00:38,750 Well on the database side, the query will already have ran and therefore information like this is already 9 00:00:38,750 --> 00:00:41,670 loaded into memory and ready to be delivered to us, 10 00:00:41,690 --> 00:00:46,460 so it's not like that if we now cycle through all the elements, we have to manually read them from a 11 00:00:46,460 --> 00:00:50,730 file, they have been loaded efficiently into memory there already. 12 00:00:50,750 --> 00:00:58,320 So now with this, we can of course execute find with the pretty command and the pretty command is really 13 00:00:58,320 --> 00:00:59,430 just the utility command, 14 00:00:59,430 --> 00:01:04,470 we could also output it like this and get a bunch of documents, to be precise 20. 15 00:01:04,540 --> 00:01:10,350 That's just something the shell does for us and the shell also gives us this shortcut to type it to 16 00:01:10,350 --> 00:01:11,510 get the next 20 17 00:01:11,610 --> 00:01:16,620 and we can do this until this is exhausted, the it command will not be available to you when working with 18 00:01:16,680 --> 00:01:20,470 a driver and there, you will indeed work differently with the cursor. 19 00:01:20,640 --> 00:01:28,290 So on find, then in most drivers, you will have some next method you can call. If I call next here, I get 20 00:01:28,290 --> 00:01:29,970 back exactly one document, 21 00:01:30,060 --> 00:01:33,570 hence you also see no type it to see more at the bottom. 22 00:01:33,670 --> 00:01:38,320 I get back exactly one document because next gives me the next document, 23 00:01:38,370 --> 00:01:41,300 the cool thing is now that I called next, 24 00:01:41,460 --> 00:01:43,780 I got back Glee with the id 8, 25 00:01:43,980 --> 00:01:49,470 if I call it again, I get back the next document in line. 26 00:01:49,610 --> 00:01:55,220 However, this again is glee because this query is now basically executed from scratch. 27 00:01:55,550 --> 00:01:57,720 This is different 28 00:01:57,740 --> 00:02:05,510 if I store my data cursor, however you want to name it, you could name it just cursor in a variable or constant 29 00:02:05,630 --> 00:02:10,550 and since the shell uses javascript, I can use javascript syntax to create such a variable here. 30 00:02:10,790 --> 00:02:16,230 So here I can use db.movies find without any condition to find all 31 00:02:16,610 --> 00:02:22,730 and now I can use my data cursor here with next to get the first element which again is Glee with the 32 00:02:22,730 --> 00:02:23,510 id 8 33 00:02:23,660 --> 00:02:31,160 and if I now use that same cursor again with next, I get a different element with the id 7, Homeland 34 00:02:31,290 --> 00:02:36,110 and If I type this again, I get yet another document ID 5, 35 00:02:36,140 --> 00:02:37,580 True Detective. 36 00:02:37,610 --> 00:02:40,090 So this is the cursor doing its work. 37 00:02:40,190 --> 00:02:47,580 The cursor also has other methods, so that was our data cursor here, if I execute it like this, it gives 38 00:02:47,580 --> 00:02:49,030 me 20 documents again 39 00:02:49,290 --> 00:02:54,210 but if I use that data cursor, I also can specify forEach method. 40 00:02:54,210 --> 00:02:58,600 Now again this will vary on the driver you are using, for javascript, 41 00:02:58,650 --> 00:03:04,860 you can use it like this, forEach takes a function that will be executed for every element that can 42 00:03:04,860 --> 00:03:06,610 be loaded through the cursor 43 00:03:06,960 --> 00:03:09,780 and now in there, we therefore get the document. 44 00:03:09,780 --> 00:03:13,680 Now this is just a special function syntax provided by javascript 45 00:03:13,680 --> 00:03:19,320 with that arrow here, here is the input to the function which is given to us by the forEach method 46 00:03:19,470 --> 00:03:23,070 and here is then our function body where we write what we want to do 47 00:03:23,310 --> 00:03:28,420 and then we got print json which is a method provided by the shell to output the document. 48 00:03:28,500 --> 00:03:33,820 And what this would do now is it would cycle through all the documents that are in the cursor 49 00:03:34,020 --> 00:03:37,440 and that by the way are only documents we didn't look at yet, 50 00:03:37,470 --> 00:03:43,860 so the two we fetched via next and the 20 we fetched via just typing the cursor name will be 51 00:03:43,860 --> 00:03:44,910 excluded 52 00:03:44,910 --> 00:03:49,500 and now we will cycle through the remaining documents which we haven't fetched yet and it will fetch 53 00:03:49,500 --> 00:03:52,590 them from the database and output them here as you see. 54 00:03:52,740 --> 00:03:55,080 And that is why you see no type it 55 00:03:55,090 --> 00:04:02,540 to see more at the bottom because we did now fetch all documents in the database simply with forEach. 56 00:04:02,940 --> 00:04:07,260 So this another way of using the cursor to go through all the documents 57 00:04:07,320 --> 00:04:11,170 and of course inside of the function you execute on every document, 58 00:04:11,310 --> 00:04:17,420 you could also have some condition, an if check to for example see if you want to output that document, 59 00:04:17,500 --> 00:04:21,030 though just be aware, if you use an if check to filter out a document, 60 00:04:21,060 --> 00:04:26,460 it would be more efficient to filter that already as a filter in the find method and not fetch it from 61 00:04:26,460 --> 00:04:30,690 the database because here, you're only filtering on the client side, 62 00:04:30,690 --> 00:04:36,060 that of course is not as efficient as filtering on the mongodb server side but you can run 63 00:04:36,090 --> 00:04:42,410 any code you want on all the documents or as I mentioned, use next to only fetch one at a time. 64 00:04:42,540 --> 00:04:48,070 By the way, if I execute next now, I get an error because the cursor has been exhausted. 65 00:04:48,360 --> 00:04:54,460 You can check for that manually with the hasNext field here, 66 00:04:55,740 --> 00:04:58,330 function excuse me, hasNext function, 67 00:04:58,320 --> 00:04:59,130 this is false 68 00:04:59,130 --> 00:05:01,920 and this indicates there is no data to be fetched. 69 00:05:02,250 --> 00:05:09,400 If I recreate my cursor by again reassigning it and this was a constant, so I'm not allowed to do that, 70 00:05:09,400 --> 00:05:13,090 so let's create a new one, just named cursor. 71 00:05:13,240 --> 00:05:18,200 So now I'm creating a new cursor based on the same query but the shell doesn't know that. 72 00:05:18,670 --> 00:05:23,290 So I got a new cursor, now on that cursor if I call hasNext, I get 73 00:05:23,300 --> 00:05:28,320 true and hence I can safely execute next. 74 00:05:28,600 --> 00:05:31,230 Now with cursors, you can do a couple of other things too, 75 00:05:31,240 --> 00:05:33,350 there are other methods you can execute 76 00:05:33,370 --> 00:05:39,130 and again this is something which you can always of course find in the official docs. There, under 77 00:05:39,160 --> 00:05:44,410 mongodb crud operations, query documents, you want to have a look at iterate a cursor in the mongo shell to 78 00:05:44,410 --> 00:05:48,380 learn more about using that cursor and different ways of iterating through it 79 00:05:48,520 --> 00:05:54,660 and we'll also have a look at how to use a cursor in the environment of our mongodb drivers. 80 00:05:54,700 --> 00:05:59,170 It is important for you to understand what a cursor is and why it exists though and I hope this became 81 00:05:59,170 --> 00:05:59,880 clearer.