1 00:00:01,140 --> 00:00:02,980 Hello and welcome back. 2 00:00:02,980 --> 00:00:07,000 So, now it's finally time to connect the MongoDB database 3 00:00:07,000 --> 00:00:10,123 that we created with our Express application. 4 00:00:11,760 --> 00:00:13,880 And, the first step in doing that is 5 00:00:13,880 --> 00:00:16,850 to actually get our connection string from Atlas. 6 00:00:16,850 --> 00:00:20,620 So, just like we did before when we connected the database 7 00:00:20,620 --> 00:00:23,230 to Compass and to the Mongo Shell, 8 00:00:23,230 --> 00:00:25,460 we need to get our connection string 9 00:00:25,460 --> 00:00:29,760 in order to connect the application to this hosted database. 10 00:00:29,760 --> 00:00:32,310 Now, in case you're not using the hosted database 11 00:00:32,310 --> 00:00:34,300 on Atlas, then I will also show you 12 00:00:34,300 --> 00:00:36,410 how to connect your local database 13 00:00:36,410 --> 00:00:38,760 with the application a bit later in this video. 14 00:00:39,840 --> 00:00:43,670 Anyway, just click here on Connect Your Application 15 00:00:43,670 --> 00:00:46,430 and then this here is our connection string. 16 00:00:46,430 --> 00:00:49,160 Just make sure that you select Node.js here actually 17 00:00:49,160 --> 00:00:51,460 because the string might change based on that. 18 00:00:53,290 --> 00:00:54,770 Now, we also need the version. 19 00:00:54,770 --> 00:00:57,050 And, of course, it's later than three. 20 00:00:57,050 --> 00:01:00,780 And so, let's now go ahead and copy our connection string 21 00:01:00,780 --> 00:01:03,440 and then back in our application, 22 00:01:03,440 --> 00:01:05,700 we create it as an environment variable 23 00:01:05,700 --> 00:01:07,390 here in our config file. 24 00:01:07,390 --> 00:01:09,590 So, I already have DATABASE_PASSWORD. 25 00:01:09,590 --> 00:01:11,133 Let's create DATABASE now. 26 00:01:12,440 --> 00:01:14,410 And then, put our string in here. 27 00:01:14,410 --> 00:01:17,010 Now, two things about this connection string. 28 00:01:17,010 --> 00:01:21,140 First, here in this place, we need to put in our password. 29 00:01:21,140 --> 00:01:24,010 Now, we could go ahead and copy the password from here 30 00:01:24,010 --> 00:01:25,970 and then paste it here, but I prefer 31 00:01:25,970 --> 00:01:28,050 to leave it in a separate variable 32 00:01:28,050 --> 00:01:29,693 and then replace it in code. 33 00:01:30,640 --> 00:01:33,950 I will just actually change this password here 34 00:01:33,950 --> 00:01:35,813 to an uppercase password. 35 00:01:38,045 --> 00:01:41,030 So, PASSWORD to make it a bit more visible. 36 00:01:41,030 --> 00:01:44,070 Then, second, this here is actually our host, 37 00:01:44,070 --> 00:01:47,400 so basically the place where the database is hosted 38 00:01:47,400 --> 00:01:49,123 and then /test. 39 00:01:50,384 --> 00:01:53,320 Now, this test here is the name of the database 40 00:01:53,320 --> 00:01:54,940 that we want to connect to. 41 00:01:54,940 --> 00:01:58,093 And, in our case, we already created the natours database. 42 00:01:59,030 --> 00:02:03,100 Let's check that out actually here back in Compass. 43 00:02:03,100 --> 00:02:05,173 And, if we go here to COLLECTIONS, 44 00:02:06,020 --> 00:02:09,880 then we see that here, we already have our natours database. 45 00:02:09,880 --> 00:02:12,400 So, we did that by the end of the last section. 46 00:02:12,400 --> 00:02:13,360 You remember? 47 00:02:13,360 --> 00:02:15,180 And so, it's very important that here, 48 00:02:15,180 --> 00:02:18,260 in this connection string, we replace this test 49 00:02:18,260 --> 00:02:19,893 with the name of our database. 50 00:02:21,300 --> 00:02:23,630 So, natours, okay? 51 00:02:23,630 --> 00:02:25,810 So, very important here, again, 52 00:02:25,810 --> 00:02:28,690 do not forget to put this here, otherwise, 53 00:02:28,690 --> 00:02:29,893 it's not gonna work. 54 00:02:30,830 --> 00:02:33,180 Now, in case you're using the local database 55 00:02:33,180 --> 00:02:34,760 like I just mentioned before, 56 00:02:34,760 --> 00:02:37,033 this connection string is a lot easier. 57 00:02:37,920 --> 00:02:39,320 So, let's just create a variable 58 00:02:39,320 --> 00:02:44,320 for that one as well, DATABASE_LOCAL. 59 00:02:44,500 --> 00:02:47,490 But, again, if you're using this database string here, 60 00:02:47,490 --> 00:02:49,540 you don't need to worry about this part. 61 00:02:49,540 --> 00:02:50,780 I'm just doing this in case 62 00:02:50,780 --> 00:02:52,680 that you are using the local one. 63 00:02:52,680 --> 00:02:54,793 And so, in this case, it's quite simple. 64 00:02:55,940 --> 00:03:00,380 It's just mongodb and this is kind of a protocol here. 65 00:03:00,380 --> 00:03:04,280 So, instead of http, Mongo uses these like mongodb 66 00:03:04,280 --> 00:03:05,603 and protocol or something. 67 00:03:07,230 --> 00:03:09,720 Then, localhost and then the port, 68 00:03:09,720 --> 00:03:12,110 which we already talked about a couple of times 69 00:03:12,110 --> 00:03:16,770 and it's 27017, remember that? 70 00:03:16,770 --> 00:03:18,640 And then, just like before, we also need 71 00:03:18,640 --> 00:03:21,430 to specify the name of the database, 72 00:03:21,430 --> 00:03:22,870 which should be natours. 73 00:03:23,880 --> 00:03:25,820 Now, in order for this to work, of course, 74 00:03:25,820 --> 00:03:28,100 you need to keep your MongoDB server 75 00:03:28,100 --> 00:03:31,563 so that Mongo D process running at all times. 76 00:03:33,890 --> 00:03:35,790 So, switching it to the terminal. 77 00:03:35,790 --> 00:03:38,943 So, this local process here is the one that I mean. 78 00:03:39,930 --> 00:03:43,440 So, never close this process, so this terminal window, 79 00:03:43,440 --> 00:03:45,690 when you're working with your local database. 80 00:03:46,623 --> 00:03:49,190 So, otherwise, it's not gonna work. 81 00:03:49,190 --> 00:03:51,470 And, that's it for configuration. 82 00:03:51,470 --> 00:03:54,480 Next up, we need to install a MongoDB driver, 83 00:03:54,480 --> 00:03:57,540 so basically a software that allows our Node code 84 00:03:57,540 --> 00:04:01,170 to access and interact with a MongoDB database. 85 00:04:01,170 --> 00:04:04,050 And, there are a couple of different MongoDB drivers, 86 00:04:04,050 --> 00:04:05,320 but we're gonna use the one 87 00:04:05,320 --> 00:04:07,320 that I would say is the most popular one, 88 00:04:07,320 --> 00:04:10,340 which is called Mongoose, which adds a couple of features 89 00:04:10,340 --> 00:04:12,873 to the more native MongoDB driver. 90 00:04:13,770 --> 00:04:15,290 But, don't worry, in the next lecture, 91 00:04:15,290 --> 00:04:17,650 you're actually gonna learn all about Mongoose 92 00:04:17,650 --> 00:04:18,903 and why we're using it. 93 00:04:19,840 --> 00:04:22,040 For now, let's just go ahead and install it. 94 00:04:24,930 --> 00:04:26,790 So, mongoose, and the version 95 00:04:26,790 --> 00:04:29,020 that I'm gonna use is version five 96 00:04:29,020 --> 00:04:32,280 and so please go ahead and install version five as well 97 00:04:32,280 --> 00:04:35,480 so that we're on the same page with our code here. 98 00:04:35,480 --> 00:04:37,490 So, by the time you're watching this video, 99 00:04:37,490 --> 00:04:40,360 Mongoose six, or even seven maybe, 100 00:04:40,360 --> 00:04:42,140 might already be out there 101 00:04:42,140 --> 00:04:43,620 and then something might have changed 102 00:04:43,620 --> 00:04:47,261 and then your code might not work the exact same way. 103 00:04:47,261 --> 00:04:49,460 So, please just install version five 104 00:04:49,460 --> 00:04:51,450 because that's actually a pretty stable version 105 00:04:51,450 --> 00:04:53,210 at this point and I don't think 106 00:04:53,210 --> 00:04:55,600 that they are gonna add much important features 107 00:04:55,600 --> 00:04:56,433 in the future. 108 00:04:57,430 --> 00:05:00,630 So, in our package.json, it should now be here. 109 00:05:00,630 --> 00:05:04,720 So, 5.5.2, yours is probably a bit later, 110 00:05:04,720 --> 00:05:06,663 but that should not be a problem. 111 00:05:08,450 --> 00:05:11,660 Let's go now to server.js here, which, remember, 112 00:05:11,660 --> 00:05:14,020 is kind of the file where we do all 113 00:05:14,020 --> 00:05:16,190 of the setup of our application. 114 00:05:16,190 --> 00:05:18,260 For example, the environment variables 115 00:05:18,260 --> 00:05:21,410 or the imports or Express application. 116 00:05:21,410 --> 00:05:23,400 And, we also start the server down here. 117 00:05:23,400 --> 00:05:24,820 And so, this is also the file 118 00:05:24,820 --> 00:05:26,830 where we're gonna configure MongoDB. 119 00:05:29,350 --> 00:05:32,363 So, let's start by requiring our mongoose package here. 120 00:05:41,740 --> 00:05:44,900 And so, now we have access to the mongoose variable 121 00:05:44,900 --> 00:05:48,563 and on there, we simply call the connect method. 122 00:05:49,630 --> 00:05:51,920 Now, into this connect method, we need to, of course, 123 00:05:51,920 --> 00:05:54,513 pass in our database connection string. 124 00:05:55,360 --> 00:05:58,990 But, remember that, in there, we still have the password. 125 00:05:58,990 --> 00:06:01,730 So, basically this password placeholder here. 126 00:06:01,730 --> 00:06:03,620 And so, before we pass in the string, 127 00:06:03,620 --> 00:06:05,353 we need to actually replace it. 128 00:06:06,300 --> 00:06:09,100 So, replace it with the real password that we have here. 129 00:06:10,710 --> 00:06:12,203 So, let's quickly do that. 130 00:06:15,500 --> 00:06:19,970 And, I'm simply gonna call this variable DB for database. 131 00:06:19,970 --> 00:06:24,970 And so, let's get our process.env.DATABASE. 132 00:06:26,220 --> 00:06:29,320 So, that is, of course, our variable 133 00:06:29,320 --> 00:06:30,920 where the string here is stored. 134 00:06:32,570 --> 00:06:35,873 So, we're getting that and then replace. 135 00:06:38,580 --> 00:06:41,083 And, we want to replace PASSWORD. 136 00:06:42,600 --> 00:06:46,330 This here comes from the HTML close tag package 137 00:06:46,330 --> 00:06:48,653 that I have on my VS Code. 138 00:06:51,660 --> 00:06:54,393 So, process.env.DATABASE_PASSWORD. 139 00:06:59,480 --> 00:07:00,630 Give it a save. 140 00:07:00,630 --> 00:07:02,610 And, okay, so that makes sense. 141 00:07:02,610 --> 00:07:04,860 So, we're simply replacing this placeholder string 142 00:07:04,860 --> 00:07:08,563 with our real password in this connection string. 143 00:07:09,602 --> 00:07:11,680 And so, now we can use this variable 144 00:07:11,680 --> 00:07:13,820 in our mongoos.connect(). 145 00:07:13,820 --> 00:07:15,900 So, in here, the first argument is actually 146 00:07:15,900 --> 00:07:18,370 that connection string, so DB. 147 00:07:18,370 --> 00:07:21,720 And then, second, we pass in an object with some options 148 00:07:21,720 --> 00:07:25,250 and these are just some options that we need to specify 149 00:07:25,250 --> 00:07:29,150 in order to deal with some deprecation warnings. 150 00:07:29,150 --> 00:07:31,923 So, don't worry too much about these ones. 151 00:07:33,210 --> 00:07:37,870 So, useNewUrlParser, set it to true. 152 00:07:37,870 --> 00:07:41,483 Then, we need to use useCreateIndex, 153 00:07:43,780 --> 00:07:44,973 also set to true. 154 00:07:46,120 --> 00:07:48,207 And then, useFindAndModify 155 00:07:49,860 --> 00:07:51,203 and set it to false. 156 00:07:52,430 --> 00:07:55,040 So, again, these here are just some options 157 00:07:55,040 --> 00:07:57,270 to deal with some deprecation warnings. 158 00:07:57,270 --> 00:07:59,270 And so, when you're creating your own applications, 159 00:07:59,270 --> 00:08:01,943 just go ahead and use just exactly the same. 160 00:08:03,830 --> 00:08:07,050 Now, this connect method here is gonna return a promise 161 00:08:07,050 --> 00:08:12,050 and so let's actually handle that promise by using then(). 162 00:08:12,580 --> 00:08:14,720 And, this promise here actually gets access 163 00:08:14,720 --> 00:08:16,203 to a connection object. 164 00:08:17,590 --> 00:08:20,453 So, let's simply call it con here for connection. 165 00:08:22,989 --> 00:08:24,070 So, basically this connection will 166 00:08:24,070 --> 00:08:26,710 be the resolved value of the promise. 167 00:08:26,710 --> 00:08:29,140 And so, just to show you that we're actually connected, 168 00:08:29,140 --> 00:08:32,063 I want to just log this object to the console. 169 00:08:33,550 --> 00:08:37,690 So, console.log connection and actually I want 170 00:08:37,690 --> 00:08:40,343 to see the connections property on there. 171 00:08:42,740 --> 00:08:44,490 And, we can also log to the console 172 00:08:46,610 --> 00:08:50,340 something like DB connection successful. 173 00:08:54,090 --> 00:08:55,283 Give it a save here. 174 00:08:56,150 --> 00:08:58,640 And then, of course, it displays these warnings here 175 00:08:58,640 --> 00:09:01,160 because of our ESLint, but don't worry about that. 176 00:09:01,160 --> 00:09:03,260 That's just the way that we configured it. 177 00:09:04,290 --> 00:09:05,200 And so, let's now go ahead 178 00:09:05,200 --> 00:09:07,573 and actually run this application. 179 00:09:08,510 --> 00:09:12,880 Now, let me remember what kind of scripts we have. 180 00:09:12,880 --> 00:09:15,380 So, we have start:dev and start:prod. 181 00:09:15,380 --> 00:09:18,150 Now, since our script here is not just called start, 182 00:09:18,150 --> 00:09:19,520 which is kind of a standard, 183 00:09:19,520 --> 00:09:23,400 we always have to type npm run start:dev 184 00:09:23,400 --> 00:09:27,450 instead of writing just npm start, without the run. 185 00:09:27,450 --> 00:09:28,430 And so, let me go ahead 186 00:09:28,430 --> 00:09:30,127 and change the name here back to start. 187 00:09:30,127 --> 00:09:32,533 And then, we have start for production. 188 00:09:34,452 --> 00:09:35,513 So, npm start. 189 00:09:37,420 --> 00:09:38,653 The app is running now. 190 00:09:40,090 --> 00:09:43,820 And, indeed, our database connection was successful. 191 00:09:43,820 --> 00:09:45,150 So, great. 192 00:09:45,150 --> 00:09:47,900 And now, here we have our connections object 193 00:09:47,900 --> 00:09:50,450 and, as you see, there's a lot of stuff in here 194 00:09:50,450 --> 00:09:53,623 and we can see our username here, host, 195 00:09:54,900 --> 00:09:56,470 the password, of course. 196 00:09:56,470 --> 00:09:59,840 And, this stuff here doesn't really matter, of course. 197 00:09:59,840 --> 00:10:02,000 I just wanted to log it here 198 00:10:02,000 --> 00:10:05,653 just so we see that our connection was actually successful. 199 00:10:06,660 --> 00:10:08,903 And so, let's get rid of that. 200 00:10:09,900 --> 00:10:12,643 And, we can actually get rid of all this here as well. 201 00:10:15,140 --> 00:10:17,493 So, this is just a one-liner here. 202 00:10:19,420 --> 00:10:21,743 Oh yeah, just missing this parenthesis. 203 00:10:24,440 --> 00:10:27,470 Now, here, it's saying me that we declared this variable, 204 00:10:27,470 --> 00:10:30,353 but never used it, so let's, indeed, get rid of it. 205 00:10:31,990 --> 00:10:33,800 Now, just as a final step, 206 00:10:33,800 --> 00:10:36,070 let me show you how we could also connect 207 00:10:36,070 --> 00:10:38,230 to the local database, again, 208 00:10:38,230 --> 00:10:40,530 in case you are using that one. 209 00:10:40,530 --> 00:10:44,170 So, let's just duplicate this line, comment it out. 210 00:10:44,170 --> 00:10:47,060 So, this is the hosted database version 211 00:10:47,060 --> 00:10:51,523 and then here we would just have to put in the local one. 212 00:10:52,520 --> 00:10:56,080 So, that's DATABASE_LOCAL. 213 00:10:56,080 --> 00:10:57,890 Let's give it a save. 214 00:10:57,890 --> 00:11:00,933 And, we're also connected successfully to that one. 215 00:11:02,020 --> 00:11:04,470 So, that's just how you would do it in that case. 216 00:11:06,040 --> 00:11:07,530 But, as I said, in this course, 217 00:11:07,530 --> 00:11:09,470 we will actually use the hosted one, 218 00:11:09,470 --> 00:11:11,470 so that's the one that I'm keeping here. 219 00:11:12,600 --> 00:11:14,100 Actually, I'm deleting this one. 220 00:11:14,100 --> 00:11:15,350 We don't need it anymore. 221 00:11:16,470 --> 00:11:17,780 Now, of course, there could also 222 00:11:17,780 --> 00:11:20,470 be problems connecting to the database. 223 00:11:20,470 --> 00:11:22,810 For example, the host might be down 224 00:11:22,810 --> 00:11:25,700 or we might have some error in our connection string 225 00:11:25,700 --> 00:11:28,660 and, in that case, we should catch that error. 226 00:11:28,660 --> 00:11:30,120 But, we will leave error handling 227 00:11:30,120 --> 00:11:31,710 for a bit later in the course 228 00:11:31,710 --> 00:11:34,530 and so for now, I'm not having any catch method here. 229 00:11:34,530 --> 00:11:35,850 Just the then method here, 230 00:11:35,850 --> 00:11:37,743 assuming that everything works fine. 231 00:11:39,242 --> 00:11:41,280 And, now finally, I want to actually go ahead 232 00:11:41,280 --> 00:11:42,780 and delete the collection 233 00:11:42,780 --> 00:11:45,110 that we already created in our database 234 00:11:45,110 --> 00:11:48,360 just so that our Mongoose driver does not interfere 235 00:11:48,360 --> 00:11:52,470 with that in any way so that there's no problem there. 236 00:11:52,470 --> 00:11:55,430 And so, let's move back to Atlas here. 237 00:11:55,430 --> 00:11:56,930 Hit on our COLLECTIONS. 238 00:11:56,930 --> 00:12:00,633 And then, simply delete the collection from here. 239 00:12:01,860 --> 00:12:05,500 And now, we need to put in here the name of the collection 240 00:12:05,500 --> 00:12:08,930 just so we don't do any mistake here. 241 00:12:08,930 --> 00:12:12,750 So, drop and, all right. 242 00:12:12,750 --> 00:12:14,620 So, now we have an empty database here, 243 00:12:14,620 --> 00:12:17,990 ready to start filling with our data starting right 244 00:12:17,990 --> 00:12:19,553 in the next couple of videos.