WEBVTT 0 00:00.820 --> 00:08.230 Now that we've seen how we can create new documents and new collections into our database using Mongoose 1 00:08.320 --> 00:09.390 and MongoDB, 2 00:09.820 --> 00:13.690 the next thing to look at is how do we read from our database? 3 00:13.840 --> 00:14.790 How do we perform 4 00:14.800 --> 00:18.880 what is the equivalent of find that we saw in the Mongo shell? 5 00:19.300 --> 00:22.770 Well it's also pretty simple and it's a lot simpler than this. 6 00:22.990 --> 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 00:29.440 --> 00:30.030 right? 8 00:30.040 --> 00:35.520 This is going to be all about fruits and we're going to call the Find function on it. 9 00:35.730 --> 00:43.540 Now this find function can accept a callback. And that callback has two parameters. 10 00:43.720 --> 00:48.540 The first one is error and the second one is whatever it finds back. 11 00:48.700 --> 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 00:54.220 --> 00:59.560 to find everything because we're not filtering, we're not querying anything, then what we'll get back 13 00:59.590 --> 01:04.030 is all of our fruits right? Inside this callback 14 01:04.030 --> 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 01:12.310 --> 01:18.510 log the fruits that we found from the find method. 16 01:18.580 --> 01:21.260 So the full syntax looks a bit like this. 17 01:21.340 --> 01:27.730 We tap into our fruits collection through the fruit model, we call the find function on it and then when 18 01:27.730 --> 01:34.150 that's completed a callback gets triggered and we have the possibility of having an error if anything 19 01:34.150 --> 01:39.830 went wrong with finding our fruits but otherwise we would get some results back which we've called fruit. 20 01:39.970 --> 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 01:47.200 --> 01:54.250 Now just before we run on app.js, unless you want to insert another three of these same fruits into 22 01:54.270 --> 02:00.820 our fruits collection, it's a good idea to comment out this part where we call the insertMany function. 23 02:01.270 --> 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 02:06.910 --> 02:09.480 every single time we run app.js. 25 02:09.700 --> 02:15.010 So let's head over to our hyper terminal and let's hit CONTROL + C to exit. 26 02:15.010 --> 02:17.900 And again run on node app.js. 27 02:17.950 --> 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 02:25.090 --> 02:28.730 and then it's performed that find function onto it. 29 02:28.930 --> 02:34.270 And because there were no errors then it's logged all of the results that it found in that collection 30 02:34.450 --> 02:41.350 which is basically all of our fruits. And these aren't living Javascript objects and they're all contained 31 02:41.380 --> 02:43.300 inside an array. 32 02:43.300 --> 02:49.870 So everything that we've learned so far about arrays and Javascript objects now applies to our data 33 02:49.960 --> 02:56.470 in our database which means that we can tap into its properties by using the dot notation or pass it 34 02:56.470 --> 03:00.240 around in two different methods and we can work with it in our app. 35 03:00.250 --> 03:00.910 js. 36 03:03.720 --> 03:11.370 So given that you know that this thing we get back called fruits is an array of fruit objects each 37 03:11.370 --> 03:15.050 with their own properties such as name and rating 38 03:15.060 --> 03:18.870 and each of those properties have associated values, 39 03:19.140 --> 03:21.740 then here's a challenge for you. 40 03:22.050 --> 03:28.300 I want you to use the for each loop that we learnt about previously in the EJS challenge module 41 03:28.590 --> 03:35.110 and I want you to loop through the array of fruits and only log their names, 42 03:35.160 --> 03:37.030 so the names of the fruit. 43 03:37.110 --> 03:44.430 So if you were successful then inside the console here you should be able to see just apple, kiwi, orange 44 03:44.460 --> 03:47.150 banana without the rest of this data. 45 03:48.330 --> 03:51.450 Pause the video now and try to complete the challenge. 46 03:54.820 --> 04:01.210 Okay. So we know that inside here we have access to an array called fruits. 47 04:01.210 --> 04:04.810 How can we loop through just any other array? 48 04:04.810 --> 04:12.040 Well, we would tap into the array, fruits, and then we would call the forEach method onto it. 49 04:12.040 --> 04:15.710 And this forEach method accepts a callback 50 04:15.940 --> 04:22.960 and inside that callback we have a single parameter which is going to be each individual object inside 51 04:23.020 --> 04:23.760 the array. 52 04:23.950 --> 04:25.030 So when we loop through it 53 04:25.030 --> 04:28.470 we're going to pick out each and every fruit out of the array. 54 04:28.660 --> 04:30.850 So we'll just call it fruit 55 04:30.880 --> 04:33.470 in that case, the singular form. 56 04:33.730 --> 04:37.690 And now inside our callback we get two console log 57 04:37.690 --> 04:46.090 each fruit and we get to tap into its name property through the use of the dot name. 58 04:46.090 --> 04:53.410 Now through this little very short bit of code, we loop through our fruits array that we got back from 59 04:53.410 --> 04:54.450 our database 60 04:54.670 --> 05:00.520 and for each fruit, we will console log the fruit's name. 61 05:00.550 --> 05:03.490 There's a lot of singular and plurals going on around here. 62 05:03.520 --> 05:06.790 So you have to make sure you know which one you're using when. 63 05:07.000 --> 05:09.530 Let's go ahead and delete that previous console log 64 05:09.550 --> 05:12.080 where we logged the entire fruits array. 65 05:12.310 --> 05:16.990 And let's run our app.js to see it in action. 66 05:17.140 --> 05:23.330 So let's again close our database connection with CONTROL + C and then run node app. 67 05:23.360 --> 05:23.960 js. 68 05:24.160 --> 05:32.250 And you can see we now loop through our fruits array and we print out each and every name property. 69 05:32.260 --> 05:35.290 So we end up with apple, kiwi, orange, banana. 70 05:35.690 --> 05:42.460 And so you can imagine that inside our app.js now we have the entire flexibility of working with Javascript 71 05:42.460 --> 05:49.180 arrays and Javascript native objects to tap into their properties, to pass them around into functions 72 05:49.780 --> 05:51.850 and use them wherever we need them. 73 05:53.820 --> 06:00.800 Now at the moment, you might have noticed that we have to keep closing our database connection with CONTROL + 74 06:00.790 --> 06:03.750 C. This is because inside our app. 75 06:03.770 --> 06:09.860 js, we're not closing that database connection. We're keeping it open at the end. 76 06:09.960 --> 06:13.980 We open that connection when we call mongoose.connect. 77 06:14.100 --> 06:18.190 But it's also good practice to close the connection to our database 78 06:18.240 --> 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 06:26.050 --> 06:31.800 with our database which in this case is finding fruits inside the database, 80 06:31.840 --> 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 06:40.180 --> 06:51.690 is we can say "mongoose.connection.close" so we call the close method on our mongoose collection. 82 06:51.930 --> 06:54.630 And now if I rerun my app 83 06:54.720 --> 07:01.240 so for the last time use CONTROL + C to exit out of our database connection, run node app. 84 07:01.270 --> 07:06.050 js and you can see that once it's done performing the task I asked it to 85 07:06.300 --> 07:15.450 which is to find all of the fruits inside the fruit collection then log its names through this forEach 86 07:15.450 --> 07:20.210 loop then it's close the connection and I don't have to use CONTROL + C anymore. 87 07:20.340 --> 07:27.260 So this is good practice whenever you're working with MongoDB databases and Mongoose. Close the connection 88 07:27.270 --> 07:27.810 once you've done.