0 1 00:00:00,820 --> 00:00:08,230 Now that we've seen how we can create new documents and new collections into our database using Mongoose 1 2 00:00:08,320 --> 00:00:09,390 and MongoDB, 2 3 00:00:09,820 --> 00:00:13,690 the next thing to look at is how do we read from our database? 3 4 00:00:13,840 --> 00:00:14,790 How do we perform 4 5 00:00:14,800 --> 00:00:18,880 what is the equivalent of find that we saw in the Mongo shell? 5 6 00:00:19,300 --> 00:00:22,770 Well it's also pretty simple and it's a lot simpler than this. 6 7 00:00:22,990 --> 00:00:29,430 All we have to do is tap into our fruit model which you can effectively see as kind of your collection 7 8 00:00:29,440 --> 00:00:30,030 right? 8 9 00:00:30,040 --> 00:00:35,520 This is going to be all about fruits and we're going to call the Find function on it. 9 10 00:00:35,730 --> 00:00:43,540 Now this find function can accept a callback. And that callback has two parameters. 10 11 00:00:43,720 --> 00:00:48,540 The first one is error and the second one is whatever it finds back. 11 12 00:00:48,700 --> 00:00:54,160 So we can call it anything we want. But in this case given that we're searching through our fruits collection 12 13 00:00:54,220 --> 00:00:59,560 to find everything because we're not filtering, we're not querying anything, then what we'll get back 13 14 00:00:59,590 --> 00:01:04,030 is all of our fruits right? Inside this callback 14 15 00:01:04,030 --> 00:01:12,220 we can check to see if there was an error and if so we'll log the error. But otherwise we'll simply just 15 16 00:01:12,310 --> 00:01:18,510 log the fruits that we found from the find method. 16 17 00:01:18,580 --> 00:01:21,260 So the full syntax looks a bit like this. 17 18 00:01:21,340 --> 00:01:27,730 We tap into our fruits collection through the fruit model, we call the find function on it and then when 18 19 00:01:27,730 --> 00:01:34,150 that's completed a callback gets triggered and we have the possibility of having an error if anything 19 20 00:01:34,150 --> 00:01:39,830 went wrong with finding our fruits but otherwise we would get some results back which we've called fruit. 20 21 00:01:39,970 --> 00:01:47,180 And if there were no errors then we simply just log all the results that we get back from our find function. 21 22 00:01:47,200 --> 00:01:54,250 Now just before we run on app.js, unless you want to insert another three of these same fruits into 22 23 00:01:54,270 --> 00:02:00,820 our fruits collection, it's a good idea to comment out this part where we call the insertMany function. 23 24 00:02:01,270 --> 00:02:06,850 That way we won't end up with the same fruits inserted into our collection many many times or at least 24 25 00:02:06,910 --> 00:02:09,480 every single time we run app.js. 25 26 00:02:09,700 --> 00:02:15,010 So let's head over to our hyper terminal and let's hit CONTROL + C to exit. 26 27 00:02:15,010 --> 00:02:17,900 And again run on node app.js. 27 28 00:02:17,950 --> 00:02:24,870 So now you can see that it's read into our fruits database, which is what we asked it to do here, 28 29 00:02:25,090 --> 00:02:28,730 and then it's performed that find function onto it. 29 30 00:02:28,930 --> 00:02:34,270 And because there were no errors then it's logged all of the results that it found in that collection 30 31 00:02:34,450 --> 00:02:41,350 which is basically all of our fruits. And these aren't living Javascript objects and they're all contained 31 32 00:02:41,380 --> 00:02:43,300 inside an array. 32 33 00:02:43,300 --> 00:02:49,870 So everything that we've learned so far about arrays and Javascript objects now applies to our data 33 34 00:02:49,960 --> 00:02:56,470 in our database which means that we can tap into its properties by using the dot notation or pass it 34 35 00:02:56,470 --> 00:03:00,240 around in two different methods and we can work with it in our app. 35 36 00:03:00,250 --> 00:03:00,910 js. 36 37 00:03:03,720 --> 00:03:11,370 So given that you know that this thing we get back called fruits is an array of fruit objects each 37 38 00:03:11,370 --> 00:03:15,050 with their own properties such as name and rating 38 39 00:03:15,060 --> 00:03:18,870 and each of those properties have associated values, 39 40 00:03:19,140 --> 00:03:21,740 then here's a challenge for you. 40 41 00:03:22,050 --> 00:03:28,300 I want you to use the for each loop that we learnt about previously in the EJS challenge module 41 42 00:03:28,590 --> 00:03:35,110 and I want you to loop through the array of fruits and only log their names, 42 43 00:03:35,160 --> 00:03:37,030 so the names of the fruit. 43 44 00:03:37,110 --> 00:03:44,430 So if you were successful then inside the console here you should be able to see just apple, kiwi, orange 44 45 00:03:44,460 --> 00:03:47,150 banana without the rest of this data. 45 46 00:03:48,330 --> 00:03:51,450 Pause the video now and try to complete the challenge. 46 47 00:03:54,820 --> 00:04:01,210 Okay. So we know that inside here we have access to an array called fruits. 47 48 00:04:01,210 --> 00:04:04,810 How can we loop through just any other array? 48 49 00:04:04,810 --> 00:04:12,040 Well, we would tap into the array, fruits, and then we would call the forEach method onto it. 49 50 00:04:12,040 --> 00:04:15,710 And this forEach method accepts a callback 50 51 00:04:15,940 --> 00:04:22,960 and inside that callback we have a single parameter which is going to be each individual object inside 51 52 00:04:23,020 --> 00:04:23,760 the array. 52 53 00:04:23,950 --> 00:04:25,030 So when we loop through it 53 54 00:04:25,030 --> 00:04:28,470 we're going to pick out each and every fruit out of the array. 54 55 00:04:28,660 --> 00:04:30,850 So we'll just call it fruit 55 56 00:04:30,880 --> 00:04:33,470 in that case, the singular form. 56 57 00:04:33,730 --> 00:04:37,690 And now inside our callback we get two console log 57 58 00:04:37,690 --> 00:04:46,090 each fruit and we get to tap into its name property through the use of the dot name. 58 59 00:04:46,090 --> 00:04:53,410 Now through this little very short bit of code, we loop through our fruits array that we got back from 59 60 00:04:53,410 --> 00:04:54,450 our database 60 61 00:04:54,670 --> 00:05:00,520 and for each fruit, we will console log the fruit's name. 61 62 00:05:00,550 --> 00:05:03,490 There's a lot of singular and plurals going on around here. 62 63 00:05:03,520 --> 00:05:06,790 So you have to make sure you know which one you're using when. 63 64 00:05:07,000 --> 00:05:09,530 Let's go ahead and delete that previous console log 64 65 00:05:09,550 --> 00:05:12,080 where we logged the entire fruits array. 65 66 00:05:12,310 --> 00:05:16,990 And let's run our app.js to see it in action. 66 67 00:05:17,140 --> 00:05:23,330 So let's again close our database connection with CONTROL + C and then run node app. 67 68 00:05:23,360 --> 00:05:23,960 js. 68 69 00:05:24,160 --> 00:05:32,250 And you can see we now loop through our fruits array and we print out each and every name property. 69 70 00:05:32,260 --> 00:05:35,290 So we end up with apple, kiwi, orange, banana. 70 71 00:05:35,690 --> 00:05:42,460 And so you can imagine that inside our app.js now we have the entire flexibility of working with Javascript 71 72 00:05:42,460 --> 00:05:49,180 arrays and Javascript native objects to tap into their properties, to pass them around into functions 72 73 00:05:49,780 --> 00:05:51,850 and use them wherever we need them. 73 74 00:05:53,820 --> 00:06:00,800 Now at the moment, you might have noticed that we have to keep closing our database connection with CONTROL + 74 75 00:06:00,790 --> 00:06:03,750 C. This is because inside our app. 75 76 00:06:03,770 --> 00:06:09,860 js, we're not closing that database connection. We're keeping it open at the end. 76 77 00:06:09,960 --> 00:06:13,980 We open that connection when we call mongoose.connect. 77 78 00:06:14,100 --> 00:06:18,190 But it's also good practice to close the connection to our database 78 79 00:06:18,240 --> 00:06:26,050 once we're done with it in our app. Inside the method where we perform the last action we want to do 79 80 00:06:26,050 --> 00:06:31,800 with our database which in this case is finding fruits inside the database, 80 81 00:06:31,840 --> 00:06:40,090 then once it's completed and the callback is being called and we have no errors then what we can do 81 82 00:06:40,180 --> 00:06:51,690 is we can say "mongoose.connection.close" so we call the close method on our mongoose collection. 82 83 00:06:51,930 --> 00:06:54,630 And now if I rerun my app 83 84 00:06:54,720 --> 00:07:01,240 so for the last time use CONTROL + C to exit out of our database connection, run node app. 84 85 00:07:01,270 --> 00:07:06,050 js and you can see that once it's done performing the task I asked it to 85 86 00:07:06,300 --> 00:07:15,450 which is to find all of the fruits inside the fruit collection then log its names through this forEach 86 87 00:07:15,450 --> 00:07:20,210 loop then it's close the connection and I don't have to use CONTROL + C anymore. 87 88 00:07:20,340 --> 00:07:27,260 So this is good practice whenever you're working with MongoDB databases and Mongoose. Close the connection 88 89 00:07:27,270 --> 00:07:27,810 once you've done.