1 00:00:00,560 --> 00:00:06,810 In the last section we provisioned an instance of Mongo D-B on in lab dotcom we're now going to continue 2 00:00:06,810 --> 00:00:12,570 by installing mongoose on our express API and then instructing mongoose to connect to this database 3 00:00:12,570 --> 00:00:13,850 that we've just provisioned. 4 00:00:14,340 --> 00:00:15,710 So let's get started. 5 00:00:16,050 --> 00:00:18,380 Well first flip on over to our terminal. 6 00:00:18,390 --> 00:00:24,000 I mean to find my running server I'm going to kill it because I want to install a new NPM module. 7 00:00:24,000 --> 00:00:28,360 So this is the one time that we will restart our server even though we're using modem Nordman. 8 00:00:28,620 --> 00:00:34,320 In general I usually like to kill the server whenever I'm stealing an NPM module so I'm going to make 9 00:00:34,320 --> 00:00:41,460 sure I'm inside my server directory and then we will run the command and install dash dash save mongoose 10 00:00:41,640 --> 00:00:43,140 like so. 11 00:00:43,140 --> 00:00:48,180 So what that do it's thing and while that runs we'll flip back over to our code editor and start to 12 00:00:48,180 --> 00:00:56,680 wire up mongoose Nauman side of my index genius file at the very top right underneath the Express require 13 00:00:56,680 --> 00:00:57,380 statement. 14 00:00:57,430 --> 00:01:01,700 We are going to start off by requiring in longus to this file. 15 00:01:01,810 --> 00:01:11,680 So we'll say Konst mongoose equals require longus like so then right above our apt declaration we are 16 00:01:11,680 --> 00:01:17,820 going to instruct mongoose to attempt to connect to that copy of Mongo D-B that we just provisioned. 17 00:01:18,100 --> 00:01:23,060 Now connecting to Mongo divi or any Mongo DB using Mongoose is pretty straightforward. 18 00:01:23,080 --> 00:01:30,640 We're going to call mongoose dot connect and then into this function we're going to pass the address 19 00:01:30,970 --> 00:01:33,970 of the Mongo instance that we've just created. 20 00:01:33,970 --> 00:01:40,480 So to get that instance or to get that address we will flip back over to our left console. 21 00:01:40,510 --> 00:01:41,770 Here it is right here. 22 00:01:41,860 --> 00:01:47,410 I'll make sure that I'm still looking at my e-mail dasht database then going to find the box at the 23 00:01:47,410 --> 00:01:53,500 very top that says to connect using a drive or using the standard Mongo D-B you or I and yep that's 24 00:01:53,500 --> 00:01:55,080 exactly what we want. 25 00:01:55,150 --> 00:01:59,460 So I will take this entire You are Ellar right here if I can highlight it. 26 00:01:59,540 --> 00:02:00,380 There we go. 27 00:02:00,760 --> 00:02:02,460 I will copy it. 28 00:02:02,590 --> 00:02:08,050 I will change back over to my index file and then paste it right in. 29 00:02:08,080 --> 00:02:12,550 Now before we get to comforter with that you might be thinking exactly what I'm thinking. 30 00:02:12,670 --> 00:02:19,120 This is a very special little piece of configuration right here that we probably do not want to accidentally 31 00:02:19,120 --> 00:02:24,760 commit to get up because if we connect or if we commit this get this you or I right here or this address 32 00:02:24,760 --> 00:02:30,880 right here anyone else in the world who has this address can freely connect to our database and make 33 00:02:30,910 --> 00:02:33,250 any changes to it that they want whatsoever. 34 00:02:33,580 --> 00:02:39,220 Now yes this is a development copy of our database and we will use a separate one for production. 35 00:02:39,400 --> 00:02:43,250 But hey there's no good reason to let people mess around with our data. 36 00:02:43,270 --> 00:02:49,690 So rather than pasting that link directly into the connect statement right here we will instead add 37 00:02:49,690 --> 00:02:53,710 that address to our config keys J.S. file. 38 00:02:53,710 --> 00:02:59,970 And remember that's where we are storing all of our different secret configuration data. 39 00:03:00,010 --> 00:03:07,140 So inside of keys yes I will make sure they add a comma after this line and they'll put a new line down 40 00:03:08,020 --> 00:03:10,790 and define lango. 41 00:03:11,350 --> 00:03:18,310 You or I like so now you can do lowercase your I uppercase you or I don't care. 42 00:03:18,310 --> 00:03:25,440 Just make sure you are consistent and then we will paste our link like some OK. 43 00:03:25,450 --> 00:03:26,970 So one last thing inside of your. 44 00:03:27,190 --> 00:03:33,250 If you look carefully at the address you'll notice that it is asking us to enter our D-B username and 45 00:03:33,250 --> 00:03:34,930 our DBI password. 46 00:03:34,930 --> 00:03:41,310 So that is the username and the password that we just created in the last section as a database user. 47 00:03:41,320 --> 00:03:44,160 So that's this right here. 48 00:03:44,230 --> 00:03:46,660 Keep clicking on that by mistake. 49 00:03:46,660 --> 00:03:52,110 So my user name or my database name was Steven and my password was Password. 50 00:03:52,110 --> 00:03:58,450 And so I'm going to take out the user and the brackets around it and replace it with Stephen. 51 00:03:58,720 --> 00:04:05,670 And the same thing for the password as well like the OK that's looking pretty good. 52 00:04:05,710 --> 00:04:12,850 Now the last thing we have to do is change back to our index as File we will require that keys file 53 00:04:12,850 --> 00:04:18,280 into here and that will pass that can that you or I into our next statement. 54 00:04:18,280 --> 00:04:25,350 So we'll do Konst keys equals require config keys like so. 55 00:04:25,960 --> 00:04:34,630 And then to mongoose connect we'll pass keys dot Mongo you or I. 56 00:04:34,990 --> 00:04:41,200 And then please again do triple check make sure the capital our capital I over here it needs to be the 57 00:04:41,200 --> 00:04:43,350 same as whatever you've got over here as well. 58 00:04:43,360 --> 00:04:46,180 So make sure the two are consistent. 59 00:04:46,180 --> 00:04:46,490 Okay. 60 00:04:46,510 --> 00:04:47,320 So that's pretty much it. 61 00:04:47,320 --> 00:04:51,020 That's all we really need to do to get started with mongoose. 62 00:04:51,100 --> 00:04:53,500 Now I'm going to change back over to my terminal. 63 00:04:53,530 --> 00:04:56,210 It looks like mongoose has been successfully installed. 64 00:04:56,210 --> 00:05:02,780 So I will now start my server up by executing a.m. run death. 65 00:05:03,270 --> 00:05:08,240 Now when I do so you may initially see some type of warning on here. 66 00:05:08,280 --> 00:05:10,920 Something it says deprecation warning. 67 00:05:11,040 --> 00:05:12,810 And so I have one that says open. 68 00:05:12,810 --> 00:05:13,900 Blah blah blah. 69 00:05:14,160 --> 00:05:19,720 And then I've also got another one that says DVD prototyped authenticate blah blah blah. 70 00:05:20,070 --> 00:05:27,180 So these two deprecation messages or these two warnings you may or may not see them if you don't see 71 00:05:27,180 --> 00:05:27,410 them. 72 00:05:27,420 --> 00:05:28,280 That's fine. 73 00:05:28,410 --> 00:05:30,690 If you do see them hey that's also fine. 74 00:05:31,020 --> 00:05:35,080 These warning messages are being produced by mongoose automatically. 75 00:05:35,160 --> 00:05:40,680 And so to give you a little bit more insight into what Mangu Mongoose is doing with Mongo Let's pull 76 00:05:40,680 --> 00:05:41,600 up our diagram. 77 00:05:41,670 --> 00:05:44,210 Let's go back to this one over here. 78 00:05:44,250 --> 00:05:44,520 OK. 79 00:05:44,550 --> 00:05:52,950 So remember Mongoose is interacting with Mongo DB Now Mongo itself has a driver layer or a layer of 80 00:05:52,950 --> 00:05:57,550 code that lets outside people interact with all the data inside of it. 81 00:05:57,600 --> 00:06:03,690 The two warnings that we are seeing right now are being produced by how Mongoose is interacting with 82 00:06:03,690 --> 00:06:04,450 Mongo. 83 00:06:04,710 --> 00:06:11,670 So these two deprecation warnings are being produced by the Mongo D-B instance because of some code 84 00:06:11,730 --> 00:06:14,180 that exists inside of mongoose. 85 00:06:14,190 --> 00:06:22,170 So until mongoose fixes their stuff up there is nothing nothing nothing we can do to get these messages 86 00:06:22,170 --> 00:06:22,870 to go away. 87 00:06:23,100 --> 00:06:24,290 So get used to them. 88 00:06:24,300 --> 00:06:25,370 They're going to stick around. 89 00:06:25,620 --> 00:06:27,560 And please just do not worry about them. 90 00:06:27,630 --> 00:06:29,640 It's totally fine to have seen these. 91 00:06:30,200 --> 00:06:30,880 OK. 92 00:06:31,380 --> 00:06:34,190 So in this section we have installed Mungo's. 93 00:06:34,350 --> 00:06:40,230 We wired it up to our application and we're now ready to create a brand new collection inside of our 94 00:06:40,230 --> 00:06:43,700 application and then start saving data to our database. 95 00:06:43,710 --> 00:06:45,470 So let's take a quick break. 96 00:06:45,510 --> 00:06:51,200 We will come back and start to create our first collection so I'll catch you in just a second.