1 00:00:02,170 --> 00:00:04,890 We've worked with our flight data quite a bit, 2 00:00:04,900 --> 00:00:10,310 now let me add another kind of data. I'll clear the console with the cls command for that 3 00:00:10,660 --> 00:00:15,460 and now I'll add a passenger's collection to my flight's database 4 00:00:15,460 --> 00:00:22,090 and I want to insertMany passengers in there. Now you find a file attached to this video, the 5 00:00:22,340 --> 00:00:25,830 passengers.json file with an array of passenger data, 6 00:00:25,840 --> 00:00:28,540 just some dummy names and ages. 7 00:00:28,570 --> 00:00:33,330 Copy that and add it here and add a closing parentheses at the end and hit enter 8 00:00:33,470 --> 00:00:38,500 and this should insert many elements with the insertMany command with all these automatically generated 9 00:00:38,520 --> 00:00:39,820 IDs. 10 00:00:39,880 --> 00:00:44,650 Now we can have a look at that data by accessing passengers and you can always just type and then use 11 00:00:44,710 --> 00:00:48,180 tab auto-completion here by the way, so simply hit tab in the shell 12 00:00:48,220 --> 00:00:53,950 and it should auto-complete, you can find them and pretty print the results, 13 00:00:54,220 --> 00:01:01,090 there are our passengers. Now on first sight, this might look good but actually if you have a look at the 14 00:01:01,090 --> 00:01:06,810 last entry, Albert Twostone and then you have a look at the data you found attached, 15 00:01:06,910 --> 00:01:10,900 you will see that Albert is not the last entry, 16 00:01:10,900 --> 00:01:14,310 Gordon Black actually is. Now the order 17 00:01:14,310 --> 00:01:17,370 here is the same order as it was added to the database, 18 00:01:17,370 --> 00:01:18,900 so where is Gordon? 19 00:01:19,200 --> 00:01:22,320 Well you also see the type it for more, 20 00:01:22,710 --> 00:01:25,930 if we type it, then we see Gordon Black. 21 00:01:25,980 --> 00:01:30,390 Now this it command is a feature shown in the shell, 22 00:01:30,390 --> 00:01:32,610 I will show you another way of getting the data 23 00:01:32,620 --> 00:01:36,840 if you were using a mongodb driver in an application in a second 24 00:01:37,350 --> 00:01:44,400 but the fact that we had used that is not shell specific, instead find, 25 00:01:44,430 --> 00:01:52,640 so passengers find or the find command in general no matter on which collection you use it gives you 26 00:01:52,640 --> 00:01:54,410 back a cursor 27 00:01:54,410 --> 00:01:57,120 and not all the data. 28 00:01:57,120 --> 00:02:00,770 Previously, it looked like it always gives us back all the data 29 00:02:00,920 --> 00:02:04,920 but actually it gives us a so-called cursor object, 30 00:02:04,940 --> 00:02:13,160 now what is that? Find does not give us an array of all the documents in a collection and that makes 31 00:02:13,160 --> 00:02:16,480 a lot of sense because that collection could be very big 32 00:02:16,610 --> 00:02:22,400 and if find would immediately send us back all documents and you think about a collection with let's 33 00:02:22,400 --> 00:02:29,000 say 20 million documents, then this would take super long but most importantly, it would send a lot of 34 00:02:29,000 --> 00:02:30,980 data over the wire, 35 00:02:30,980 --> 00:02:37,250 so instead of that, it gives us back cursor object which is an object with a lot of metadata behind it that 36 00:02:37,250 --> 00:02:40,260 allows us to cycle through the results 37 00:02:40,400 --> 00:02:43,200 and that is what that it command did, 38 00:02:43,280 --> 00:02:47,480 it basically used that cursor to fetch the next bunch of data. 39 00:02:47,540 --> 00:02:55,910 So find here really just gives us back that cursor and we can actually see that by using some other methods 40 00:02:55,910 --> 00:02:56,300 here, 41 00:02:56,300 --> 00:02:59,420 for example there is toArray, 42 00:02:59,420 --> 00:03:03,810 if you execute this, now you see Gordon Black as the last element 43 00:03:03,980 --> 00:03:06,990 and now there is no type it to see more 44 00:03:07,040 --> 00:03:11,880 because toArray will actually go ahead and exhaust the cursor, 45 00:03:11,900 --> 00:03:19,070 so go through the entire list and fetch all the documents and not stop after the first 20, which by the way 46 00:03:19,070 --> 00:03:21,680 is simply a feature by the mongodb shell, 47 00:03:21,740 --> 00:03:28,190 it gives you the first 20 documents automatically but then stops, toArray simply gets them all and gives 48 00:03:28,190 --> 00:03:29,360 you an array. 49 00:03:29,360 --> 00:03:35,210 Now that might be fine here but of course that is also not optimal if you've got a lot of elements. In 50 00:03:35,210 --> 00:03:42,130 your application code, you would most likely use something like passengers find 51 00:03:42,230 --> 00:03:52,040 and then there is a forEach method and forEach here simply allows you to write some code to do something 52 00:03:52,130 --> 00:03:57,170 on every element that is in your database, on every document you got in there. 53 00:03:57,200 --> 00:04:03,530 Now the exact syntax you can use is something you can find in the driver docs for your driver, 54 00:04:03,530 --> 00:04:09,110 so if you're using PHP, check out the PHP mongodb driver docs and you will see how to use that 55 00:04:09,110 --> 00:04:13,920 forEach function. In javascript and the shell is based on javascript, 56 00:04:14,030 --> 00:04:20,180 you could pass a javascript function to it using an arrow function here where you will get an argument 57 00:04:20,180 --> 00:04:25,970 passed into the function automatically and that will be your document and you can name this argument 58 00:04:25,970 --> 00:04:26,810 however you want, 59 00:04:26,850 --> 00:04:29,280 so in our case it will be our passenger data 60 00:04:29,720 --> 00:04:36,590 and then you can use it in here and here in the shell, there is a print json command which we can use 61 00:04:36,590 --> 00:04:42,860 to pass or to well yeah pass our json data which we want to output like passenger data. 62 00:04:43,100 --> 00:04:50,060 And now we also get all the documents because now what we're doing is on the cursor which find does 63 00:04:50,060 --> 00:04:55,670 return, we execute the built-in forEach method to execute this method here, 64 00:04:55,820 --> 00:05:02,720 this anonymous method on every element and the cursor will simply go through our entire collection, 65 00:05:02,730 --> 00:05:09,760 fetch all documents one at a time, execute that function on it, pass the document into the function 66 00:05:09,860 --> 00:05:15,950 and then here we just happened to print it out like this in your application with your favorite language. 67 00:05:15,950 --> 00:05:21,560 You can of course do to the data whatever you want to do, render it onto a html page, send it with an 68 00:05:21,560 --> 00:05:24,330 http request, whatever you need to do 69 00:05:24,710 --> 00:05:27,650 and that is just what I want you to take away from this lecture. 70 00:05:27,650 --> 00:05:34,640 Find does not by default give you all the documents even though it looked like it did because the shell 71 00:05:34,640 --> 00:05:38,000 actually gives you the first 20 documents by default, 72 00:05:38,000 --> 00:05:45,150 instead find gives you a cursor and then it's up to you to use that cursor with toArray to force it 73 00:05:45,170 --> 00:05:52,310 to get all documents and put them into an array or better for large sets of documents, with forEach 74 00:05:52,430 --> 00:05:57,110 and there are more methods available which you find in your driver docs. 75 00:05:57,230 --> 00:06:04,760 Technically you need to know that with forEach, it will really just fetch the next document for every loop 76 00:06:04,910 --> 00:06:05,510 cycle, 77 00:06:05,510 --> 00:06:11,990 so this is very efficient here because it does not fetch all the data in advance, load it into memory 78 00:06:12,080 --> 00:06:17,210 and then force you to go through it with forEach, instead it really just fetches the data on the demand 79 00:06:17,450 --> 00:06:23,100 therefore of course not overusing your bandwidth and not loading too much into memory. 80 00:06:23,630 --> 00:06:25,810 So this is the important thing to take away, 81 00:06:25,850 --> 00:06:29,920 find gives you a cursor and not the element itself. 82 00:06:30,140 --> 00:06:38,390 This also is the reason why on find one, we could not use pretty, you remember if I use passengers find 83 00:06:38,390 --> 00:06:47,470 one, this works, find one pretty fails because pretty is a method that simply exists on the cursor here. 84 00:06:47,600 --> 00:06:50,050 So this is why find one does not support it 85 00:06:50,120 --> 00:06:55,140 because find one does not give us a cursor because it only gives us one document anyways. 86 00:06:55,250 --> 00:06:57,680 Find gives us multiple documents, 87 00:06:57,680 --> 00:07:00,000 therefore we don't get the data immediately, 88 00:07:00,050 --> 00:07:07,380 we get the cursor instead, the shell happens to take that cursor and give us the first 20 documents by default 89 00:07:07,850 --> 00:07:10,890 but we can also control that with forEach or toArray 90 00:07:10,930 --> 00:07:17,570 and here, pretty exists because it is simply a method that exists on the cursor to pretty print the output. 91 00:07:18,140 --> 00:07:23,570 This is what you need to take away here, for the other methods like insert, update and delete, cursors 92 00:07:23,570 --> 00:07:26,290 do not exist because these methods don't fetch data, 93 00:07:26,330 --> 00:07:27,500 they manipulate it 94 00:07:27,520 --> 00:07:27,920 instead.