WEBVTT 0 00:00.370 --> 00:00.740 All right. 1 00:00.750 --> 00:07.080 So in the previous module we looked at how we would use MongoDB in the command line through the use 2 00:07.140 --> 00:08.380 of the Mongo shell. 3 00:08.430 --> 00:16.380 And that's pretty much MongoDB in its purest form in an isolated setting. But that's no use to us because 4 00:16.380 --> 00:19.230 we want to build applications that have databases 5 00:19.230 --> 00:19.800 right? 6 00:19.860 --> 00:25.860 So we need to learn how to integrate all MongoDB database with on Node.js application. 7 00:25.860 --> 00:28.680 And that is what this module is all about. 8 00:29.850 --> 00:35.270 Now when you're creating a Node app that needs to connect to a MongoDB database 9 00:35.430 --> 00:39.080 there's essentially two options for you to choose from. 10 00:39.180 --> 00:44.450 One is to use the MongoDB native driver that we're going to talk about very quickly. 11 00:44.700 --> 00:50.850 Another is to use a ODM or an Object Document Mapper that's called mongoose. 12 00:50.850 --> 00:57.570 Now the most popular way of working with MongoDB and Node.js is through using this package called 13 00:57.660 --> 00:58.920 mongoose. 14 00:58.920 --> 01:05.700 And the reason is because it vastly simplifies and cuts down on the code that's required in order to 15 01:05.700 --> 01:08.220 work with your MongoDB database. 16 01:08.340 --> 01:14.340 But I want to show you first how the native MongoDB driver would work and then we'll go through how 17 01:14.340 --> 01:21.300 to use mongoose just so that you can see for yourself just how much easier it is to incorporate mongoose 18 01:21.300 --> 01:24.550 and it makes your life as a developer so much better. 19 01:24.570 --> 01:30.970 But first let's get started and take a look at how we would use the MongoDB native driver. 20 01:30.990 --> 01:37.310 Firstly I want to head over to the MongoDB documentation at docs.mongodb.com. 21 01:37.530 --> 01:45.160 And here we're going to head over to the tab that's called drivers. And on the left hand pane here you can 22 01:45.160 --> 01:49.560 see that there's a whole bunch of drivers for different languages. 23 01:49.780 --> 01:56.950 Now the driver is what's going to enable our MongoDB to interact with our application and depending on 24 01:57.010 --> 02:02.770 which language your application was developed with then you'll need to use a different driver. 25 02:02.770 --> 02:10.000 Now in our case we're going to choose Node.js and we're going to view the latest documentation. And from 26 02:10.000 --> 02:15.940 here we're going to head over to quick starts and it details pretty much step by step how you would 27 02:15.940 --> 02:20.860 get started using the native MongoDB Node.js driver. 28 02:20.860 --> 02:22.540 Well that's quite a long sentence. 29 02:22.540 --> 02:29.080 So essentially what we're trying to do is we're trying to glue together our MongoDB database with 30 02:29.210 --> 02:31.090 our Node.js application. 31 02:31.090 --> 02:37.600 And this is going to be the syntax and the code and we're going to install the driver to enable us to 32 02:37.600 --> 02:38.210 do that. 33 02:39.140 --> 02:43.780 First things first, we're going to need a new project. For this project 34 02:43.790 --> 02:49.220 I've decided that I'm going to make a database of fruits and I'm going to rate all the fruits that I've 35 02:49.220 --> 02:50.000 tried. 36 02:50.000 --> 02:56.150 And there's some really wacky ones out there that I've come across through my travels and I think it's 37 02:56.150 --> 03:03.200 a pretty nice way of showing you how to work with MongoDB and Node. CD over to wherever you keep 38 03:03.230 --> 03:04.770 all of your development files 39 03:04.790 --> 03:11.360 and then we're going to create a new directory and that's going to be called FruitsProject. And then 40 03:11.420 --> 03:13.780 we're going to head over to FruitsProject 41 03:14.030 --> 03:17.060 and here we're going to create a new app. 42 03:17.120 --> 03:23.850 So we're going to create a new file called app.js and as per usual we're going to initialize our npm. 43 03:24.080 --> 03:30.980 But this time instead if you don't want to hit enter to insert the default values when you initialize 44 03:30.980 --> 03:36.380 npm, you can just add a -y which basically says yes to everything. 45 03:36.650 --> 03:43.260 And when you hit enter you can see that we've initialized our npm and accepted all of the defaults. 46 03:43.280 --> 03:48.330 So now all we have to do is to install the MongoDB driver. 47 03:49.150 --> 03:53.490 And to do that with npm we have to install something called mongodb. 48 03:53.800 --> 03:58.510 And remember we don't need the --save anymore as of the latest versions of npm. 49 03:58.720 --> 04:04.220 So we can simply say npm i short for install and then it's mongodb. 50 04:04.510 --> 04:05.470 Hit enter 51 04:05.480 --> 04:09.150 and that should install the driver in a couple of seconds. 52 04:09.730 --> 04:17.440 So now all we have to do is open up our app.js inside Atom and get started developing on your Node 53 04:17.460 --> 04:22.360 JS application that integrates with a MongoDB database. 54 04:22.390 --> 04:30.130 I'm just going to show you how we can get started using this by just using some the code from the getting 55 04:30.130 --> 04:31.330 started guide. 56 04:31.360 --> 04:41.150 So I'm simply going to copy this part and add it to our aappp.js and again we've got a whole bunch 57 04:41.150 --> 04:45.840 of warnings so let's just silence that from //jshint. And 58 04:49.910 --> 04:52.770 now I want to walk you through this code. First 59 04:52.780 --> 04:56.390 we are requiring a package called mongodb 60 04:56.710 --> 05:01.760 and along with that package we also insert another one called assert. 61 05:01.780 --> 05:06.040 Now everything that you see that is related to assert be it on 62 05:06.070 --> 05:10.060 Node.js applications or iOS applications, 63 05:10.060 --> 05:12.470 it's always to do with testing. 64 05:12.820 --> 05:19.270 And so you'll see that when we add a lot of the MongoDB code into our application, there's a lot of 65 05:19.340 --> 05:26.700 assert that just validates our data entry and our connection to the MongoDB database. 66 05:26.710 --> 05:34.030 Now the next part is the connection URL. And you can see this looks a little bit similar to what 67 05:34.030 --> 05:38.710 we've been using to develop websites on our local system. 68 05:38.710 --> 05:44.830 The important thing to note here is that when you're working with MongoDB the port that they use is 69 05:45.010 --> 05:48.070 pretty much always 27017 70 05:48.070 --> 05:51.850 and this is just some sort of arbitrary number that they've decided upon. 71 05:51.880 --> 05:58.400 This is the base URL for connecting to our database. And then we specify a database name 72 05:58.420 --> 06:07.270 so let's change it to fruitsDB and then we create a new Mongo client which is going to connect to 73 06:07.300 --> 06:13.640 our MongoDB database and if a fruitsDB doesn't exist then it will create it. 74 06:13.810 --> 06:20.920 And if all of that happened without any errors then we are going to log that we have connected successfully 75 06:20.950 --> 06:23.940 to the server. Once we're done 76 06:23.970 --> 06:26.680 we're going to close the connection to our database 77 06:26.910 --> 06:34.230 and this is all that this code does so far, the majority of it down here is just to create a new connection 78 06:34.530 --> 06:36.190 to a new database. 79 06:36.270 --> 06:43.440 And this is equivalent to when we saw in the Mongos shell previously where we just said use fruits 80 06:43.440 --> 06:51.570 DB that use fruitsDB if we wanted to use it in our Node.js application using the MongoDB native driver 81 06:51.870 --> 06:54.700 is basically all of this code. 82 06:54.750 --> 06:56.320 So it's a bit crazy. 83 06:56.400 --> 07:03.120 And the reason why I'm walking you through this and explaining the code is that in reality most developers 84 07:03.120 --> 07:09.040 who are working with Node and MongoDB will rarely use the native MongoDB driver. 85 07:09.210 --> 07:11.090 Now it's not because it's no good, 86 07:11.160 --> 07:17.550 it works and allows a lot of personalization and you can drill down to the specifics and you can set 87 07:17.550 --> 07:22.460 up and use your MongoDB database with a high level of control. 88 07:22.980 --> 07:30.300 All right so now that we have created this, let's go ahead and run our app.js using again node app 89 07:30.300 --> 07:38.710 .js and you can see we get a error. And it says MongoNetworkError failed to connect to server. 90 07:39.000 --> 07:41.950 And whenever you get this, you always have to remind yourself 91 07:42.000 --> 07:46.250 do you still have the Mongo server up and running? 92 07:46.350 --> 07:53.610 So create a new tab and remember whenever we're using MongoDB we always have to first run our server 93 07:53.910 --> 08:01.380 using the command mongod. And only once it's done running and it's waiting for connections on again 94 08:01.380 --> 08:03.840 that port 27017 95 08:03.840 --> 08:05.880 it's going to come up in a lot of places. 96 08:05.880 --> 08:12.450 Well now we're ready to actually connect to our database and we can run node ap.js again 97 08:12.600 --> 08:16.920 and now we are successfully connected to the server. 98 08:17.430 --> 08:23.760 If at some point you get a deprecation warning as you see here then just read what the message says 99 08:23.790 --> 08:26.660 because MongoDB, because it's relatively new, 100 08:26.820 --> 08:34.750 they will add new syntax and new code and new APIs, new ways of doing things, on a regular basis. 101 08:34.770 --> 08:40.770 So in this case it says deprecation warning current URL string parser is deprecated and will 102 08:40.770 --> 08:45.780 be removed in a future version. To use the new parser, pass option 103 08:45.780 --> 08:48.900 this to the MongoClient.connect. 104 08:49.050 --> 08:53.300 If you didn't know what to do with this deprecation warning, then what do you do? 105 08:53.310 --> 09:01.590 Well you just copy it in its entirety and then you paste it into Google and see what does Google say. 106 09:01.680 --> 09:09.150 So first link is a link to Stack Overflow and it already tells you this is how you would work around 107 09:09.150 --> 09:09.550 it. 108 09:09.690 --> 09:15.300 So you can either change to a local version of Mongo which is not what we want to do, we want to work with 109 09:15.300 --> 09:16.410 the latest version. 110 09:16.590 --> 09:22.260 So if you do indeed want to use latest version, then you will have to make your connection using this 111 09:22.460 --> 09:23.030 use 112 09:23.070 --> 09:25.930 NewUrlParser key value pair. 113 09:26.250 --> 09:29.670 And here they've got some example code of how you would add it. 114 09:29.670 --> 09:33.140 So they've added it to MongoClient.connect. 115 09:33.150 --> 09:38.490 You can see that even though we're taking the code from MongoDB using the latest version of their 116 09:38.490 --> 09:44.130 documentation stuff gets out of date and it's really important that you don't get fazed by every little 117 09:44.130 --> 09:48.840 thing that you see in the console and simply just Google for an answer. 118 09:49.260 --> 09:52.640 Let's go back to Atom and let's go ahead and fix this. 119 09:52.650 --> 09:59.110 We're going to change our Mongo client to use the URL that we specified for our MongoDB server 120 09:59.220 --> 10:05.210 but then we're going to add a comma and we're going to paste in that option that they wanted us to add. 121 10:05.550 --> 10:06.630 So there we go. 122 10:06.630 --> 10:09.900 Now let's hit save and let's rerun our app. 123 10:09.900 --> 10:15.680 You can see now we get connected successfully to server and we don't get any deprecation warnings. 124 10:16.170 --> 10:21.840 Even though the last time we did get this warning our app is still running and connected successfully 125 10:21.840 --> 10:23.850 so it didn't actually affect our app. 126 10:24.030 --> 10:29.460 But it's important that whenever you see a deprecation warning to just copy and paste it into Google 127 10:29.520 --> 10:32.360 and see what the existing solutions are. 128 10:32.580 --> 10:38.310 It's very very rare that you will be coming across a problem for the first time and you're the only 129 10:38.310 --> 10:39.520 person with that problem. 130 10:39.540 --> 10:40.930 It almost never happens. 131 10:42.590 --> 10:48.800 OK so now that we have it connected successfully to our Mongo server the next part is adding some 132 10:48.800 --> 10:51.300 data to our database. 133 10:51.330 --> 10:52.450 So how do we do that? 134 10:52.470 --> 10:58.960 Well it's back to the documentation and the next part is of course insert a document. 135 10:59.010 --> 11:06.900 So let's copy all of this code from the documentation and let's paste it below the last line that we 136 11:06.900 --> 11:07.910 have. 137 11:08.010 --> 11:14.820 So this code what it does is it will create a new collection and the collection that we're going to 138 11:14.820 --> 11:17.820 create is going to be called fruits. 139 11:17.900 --> 11:21.840 We've got a collection called fruits inside our fruitsDB 140 11:22.010 --> 11:31.560 And this is equivalent to when we used our new database and then we said db.fruits.insert. 141 11:31.940 --> 11:39.340 And then we get to insert some data in here. The data is in the same format as we saw before. 142 11:39.350 --> 11:46.810 It's a bunch of key value pairs and here they're inserting three items into a field called a 143 11:46.850 --> 11:49.480 and then they've added the values 1,2 ,3. 144 11:49.550 --> 11:55.190 So as we saw before in the Mono shell whenever you see a pair of curly brackets that's going to be 145 11:55.220 --> 11:58.370 an individual document or an individual record. 146 11:58.590 --> 12:02.370 And in our case we're going to insert three fruits. 147 12:02.540 --> 12:09.560 So I'm going to just paste in some code that I wrote a little bit earlier on and we'll just format this 148 12:09.560 --> 12:11.720 or that you can see it more clearly. 149 12:14.320 --> 12:21.000 So here I'm inserting three documents and each document is a fruit. 150 12:21.070 --> 12:24.110 So the first one has a name of apple, a score of 8 151 12:24.160 --> 12:30.360 and a review of 'great fruit'. And the rest of them are for orange and banana. 152 12:30.820 --> 12:37.630 So we're now creating this array of fruits and we're using the method insertMany which comes from MongoDB 153 12:37.630 --> 12:44.620 to insert all of these three documents into a collection called fruits inside a database called 154 12:44.620 --> 12:45.630 fruitsDB. 155 12:48.740 --> 12:54.300 And down here you can see that the next few lines are dedicated to validation. 156 12:54.320 --> 12:57.350 So they've added a whole bunch of asserts. 157 12:57.380 --> 13:02.570 And if we go through them line by line, this one says validate to make sure that there are no errors 158 13:02.600 --> 13:05.280 when we inserted our document. 159 13:05.720 --> 13:11.080 And the next ones ensure that we have three results that are inserted into our collection. 160 13:11.330 --> 13:20.790 And if that is so then we're going to log inserted three documents into the collection. And the next 161 13:20.790 --> 13:26.930 part is to simply run that function inside our insert documents function and they show you that client. 162 13:27.030 --> 13:29.130 connect should now look like this 163 13:29.130 --> 13:37.290 with this additional part. Up here inside client.connect instead of calling client.close we're 164 13:37.290 --> 13:44.060 going to paste that new code which calls insertDocuments and only once it's done inserting the documents 165 13:44.070 --> 13:48.550 do we close the connection to our database. 166 13:48.560 --> 13:56.390 So now let's hit save and let's again run our code with node app.js and you can see we've successfully 167 13:56.390 --> 14:01.890 connected to the server and we've inserted three documents into the collection. 168 14:02.210 --> 14:08.210 And you can actually test out the validation quite easily. Say if we were only to add 2 records to 169 14:08.240 --> 14:13.890 our database, then you will get an assertion error saying that 3 doesn't equal 2. 170 14:13.910 --> 14:18.660 Well you only inserted 2 items but I was expecting 3. 171 14:18.770 --> 14:25.040 So you're ready beginning to see the validation that we can place inside our app.js using the Mongo 172 14:25.040 --> 14:29.300 DB driver in order to validate the data that we're inserting. 173 14:29.420 --> 14:37.870 And you can see just how very flexible all of these asserts are. If I head into my command line and I 174 14:37.870 --> 14:47.830 create a new terminal and then I tap into the Mongo shell and I say show dbs, you can see now in 175 14:47.830 --> 14:53.710 addition to our shopDB which we created inside the Mongo shell we've also got this new database 176 14:53.710 --> 15:00.340 called fruitsDB and that one was not created using the Mongo shell but instead from Node.js 177 15:00.670 --> 15:02.740 using the MongoDB driver. 178 15:02.950 --> 15:05.600 And that's where the fruitsDB got created. 179 15:08.180 --> 15:18.410 And now if I say use fruitsDB then we can say show collections inside this db 180 15:18.430 --> 15:23.680 so we have this collection called fruits and that of course comes from this part 181 15:23.680 --> 15:30.100 when we created our new collection using the db.collection method. And then we inserted a whole 182 15:30.100 --> 15:30.770 bunch of data 183 15:30.790 --> 15:36.620 if you're a member so we should be able to say db.fruits.find. 184 15:36.790 --> 15:43.070 And we're going to leave the parentheses empty to show us all the data that we currently have. The keen 185 15:43.160 --> 15:50.460 eyed amongst you will spot that we have 5 records instead of what you might expect to be 3 because 186 15:50.460 --> 15:56.430 the first time we added apple, orange, banana and the second time we deleted the banana record and we 187 15:56.430 --> 15:58.590 got an assertion failure. 188 15:58.650 --> 16:03.710 Now assertion is quite a complex topic but in this case this is a nonfatal assertion. 189 16:03.720 --> 16:10.770 So one actually prevent the insertion of these records but it simply alerts you to the fact that the 190 16:10.770 --> 16:13.910 assertion has failed. 191 16:14.010 --> 16:17.200 So the next step is how do we get our app. 192 16:17.280 --> 16:25.500 js to find all of these records? How do we read our data inside our Node.js app? 193 16:25.500 --> 16:27.970 Well it's only a short scroll away. 194 16:28.020 --> 16:34.150 So the next part is find all documents and let's just go ahead and add this into our code 195 16:35.410 --> 16:40.780 right at the bottom again and this section we have to specify the collection where we want to find something 196 16:40.780 --> 16:44.320 from which if you remember earlier on was called fruits, 197 16:44.340 --> 16:50.470 so let's change that to fruits. And then it's going to look through the collection and it's going to 198 16:50.470 --> 16:51.830 find all. 199 16:51.940 --> 16:54.490 And then it's going to save it into an array 200 16:54.670 --> 17:01.300 and for that array we need to have some fruits. And in the callback 201 17:01.330 --> 17:08.590 we're going to get back the object that we got from the find function and we're going to log those fruits 202 17:08.710 --> 17:10.140 into the console. 203 17:12.690 --> 17:21.020 Now if we hit save and call that find function inside again the client.connect, 204 17:21.270 --> 17:24.240 in this case we're not going to insert any more documents. 205 17:24.240 --> 17:26.780 We're simply just going to find our documents. 206 17:26.820 --> 17:31.500 So I'm just going to copy that part of the code and we're going to update this method 207 17:31.500 --> 17:40.040 client.connect to delete the insertion method call and just find those records inside our database. 208 17:40.350 --> 17:45.280 Let's head back to our terminal connection where we've got our prompt and let's call node app. 209 17:45.290 --> 17:46.390 js again. 210 17:46.620 --> 17:54.900 So now you can see that we've found the following records and this printed in here is an array of Javascript 211 17:54.960 --> 18:01.710 objects and you can see it looks very similar to what we've got in the Mongo shell but what's logged 212 18:01.710 --> 18:04.850 here are actually the documents from our MongoDB 213 18:05.070 --> 18:10.120 but here we've actually got our Javascript objects. 214 18:10.310 --> 18:17.810 So if we now have access to these living Javascript objects inside an array in our app.js then we 215 18:17.810 --> 18:23.790 can use our Javascript code and Node.js to do whatever it is that we wish with it. 216 18:24.200 --> 18:31.670 As you can see this code is very very wordy and there's a lot of it that's just boilerplate code that 217 18:31.670 --> 18:36.430 you have to add every single time you use the native MongoDB driver. 218 18:36.590 --> 18:42.300 And for some people especially if you want to develop applications quickly it can be a bit of a pain. 219 18:42.320 --> 18:49.130 So in the coming modules I want to show you what most Node developers will work with which is a module called 220 18:49.490 --> 18:56.090 mongoose and it will vastly simplify the code that we have here and we will walk through how to use 221 18:56.090 --> 19:02.180 mongoose to make your life developing Mongo and node apps so much easier. 222 19:02.270 --> 19:03.210 So I'll see you there.