1 00:00:02,290 --> 00:00:05,420 So let's take a break from the theory now and let's simply get started. 2 00:00:05,440 --> 00:00:06,490 Let's install mongodb 3 00:00:06,490 --> 00:00:07,720 locally for now 4 00:00:07,860 --> 00:00:10,550 and let's connect to it and see how we can work with it. 5 00:00:10,670 --> 00:00:13,550 Now mongodb runs on all operating systems, 6 00:00:13,630 --> 00:00:17,470 Mac, Linux, Windows, on all three operating systems, 7 00:00:17,470 --> 00:00:21,760 you can go to mongodb.com and on that page, click on get 8 00:00:21,760 --> 00:00:27,640 mongodb in the top right corner, that will allow you to download the mongodb installer and install 9 00:00:27,640 --> 00:00:30,020 it locally on your machine. 10 00:00:30,040 --> 00:00:31,000 Now there by default, 11 00:00:31,000 --> 00:00:33,070 it selected this atlas thing, 12 00:00:33,130 --> 00:00:35,580 that is something we'll dive into later in the course, 13 00:00:35,650 --> 00:00:38,150 for now let's use the free community server. 14 00:00:38,410 --> 00:00:41,800 Now the enterprise server would be your paid solution, 15 00:00:42,130 --> 00:00:45,320 we don't want to use that, we'll use the free one which gives us everything we need, 16 00:00:45,430 --> 00:00:51,640 the enterprise server just adds some enterprise level features like different kinds of authentication 17 00:00:51,640 --> 00:00:53,680 and encryption which we don't need here, 18 00:00:53,890 --> 00:00:59,640 so let's go with the community server and then there, choose your operating system in case it's not pre-selected 19 00:00:59,770 --> 00:01:01,020 correctly. 20 00:01:01,080 --> 00:01:06,220 Now there, you can go with the default version that is installed and then you can click the download 21 00:01:06,220 --> 00:01:06,970 button, 22 00:01:06,970 --> 00:01:11,930 by the way also always check the installation instructions where you find a link to here 23 00:01:12,040 --> 00:01:15,930 if you're facing any issues. So now I'll download that 24 00:01:16,110 --> 00:01:20,090 and you don't need to sign up here, you can just wait for the download to start 25 00:01:20,180 --> 00:01:25,740 and once the download finished, you can on Mac and Linux simply extract that file that was downloaded, 26 00:01:26,160 --> 00:01:32,550 on Windows you can start the installer. Now if you have that installer, simply walk through the different 27 00:01:32,550 --> 00:01:34,790 steps there and choose custom 28 00:01:34,830 --> 00:01:39,570 when you have the chance for it. Now there you can leave the defaults as they are, 29 00:01:39,570 --> 00:01:44,970 I just want to inform you that on Windows, mongodb will be installed as a service as you can see 30 00:01:44,970 --> 00:01:45,280 here. 31 00:01:45,280 --> 00:01:47,700 Now you could uncheck this but this is fine, 32 00:01:47,910 --> 00:01:51,230 this is a little different to how it will then run on Mac and Linux 33 00:01:51,240 --> 00:01:56,500 but you can follow along, fine in both cases and I will of course show you how to connect to it 34 00:01:56,550 --> 00:01:57,600 in both scenarios. 35 00:01:57,810 --> 00:02:04,860 So simply click next and install and install it on windows in the destination you chose. On Mac and Linux, 36 00:02:04,870 --> 00:02:10,020 you simply have that extracted folder here and now you can grab all these files in that folder and 37 00:02:10,020 --> 00:02:15,600 copy them into any place on your operating system where you want to install mongodb so to 38 00:02:15,600 --> 00:02:16,360 say, 39 00:02:16,480 --> 00:02:23,930 so there, I will go to a folder I prepared and simply paste that into here. 40 00:02:23,970 --> 00:02:26,250 Now the bin folder is the important folder, 41 00:02:26,250 --> 00:02:29,380 this has all the mongodb related executables, 42 00:02:29,460 --> 00:02:35,310 most of them you'll not really need for this course but we will work with a couple of them to start 43 00:02:35,370 --> 00:02:39,890 our own mongodb server which then hosts our mongodb databases, 44 00:02:39,960 --> 00:02:45,200 we'll also use the one for importing data at some point in the course and we will use the shell client 45 00:02:45,360 --> 00:02:50,610 which is a client that allows us to connect to the mongodb database and run commands on it 46 00:02:50,670 --> 00:02:54,150 and on Windows, you get the same executables in your installation path 47 00:02:54,150 --> 00:03:01,680 by the way. Now with these files being placed in the path or in the place of your operating system where 48 00:03:01,680 --> 00:03:06,470 you want them, you should do something else on Mac and Linux, 49 00:03:06,570 --> 00:03:14,220 you should go to your root folder on your system and there, you should create a new folder named data 50 00:03:15,750 --> 00:03:20,930 and in that folder, create another new folder named db and you can do that on Windows too if you want. 51 00:03:20,930 --> 00:03:25,820 You don't need to but you should create that folder at some place in your operating system, 52 00:03:25,820 --> 00:03:27,020 it can be anywhere, 53 00:03:27,020 --> 00:03:31,100 doesn't have to be the root folder but it will be easier to follow along for the rest of the course 54 00:03:31,130 --> 00:03:31,950 if you use that 55 00:03:31,970 --> 00:03:37,590 but I will also show you how to start mongodb when you use a different folder or a different path. 56 00:03:37,640 --> 00:03:45,440 So create that data db folder if possible in your root folder of your Macbook, of your computer, of 57 00:03:45,440 --> 00:03:48,830 your laptop or create it in any other path, 58 00:03:48,830 --> 00:03:51,170 I will show you how to make it work with both. 59 00:03:51,170 --> 00:03:57,020 Now with that, start your terminal or on Windows, your command prompt 60 00:03:57,290 --> 00:04:02,840 and this is where we'll work in for the majority of this course because there, we can run all these important 61 00:04:03,140 --> 00:04:08,990 queries or commands to insert or to retrieve data but of course, I will also show you how to then apply 62 00:04:08,990 --> 00:04:11,240 the knowledge you gain in here in a real app, 63 00:04:11,240 --> 00:04:14,450 I'll actually do this in this first module already. Now in there, 64 00:04:14,450 --> 00:04:18,470 try running mongo and you should get a command, not found error. 65 00:04:18,830 --> 00:04:24,230 Now on Mac and Linux, you fix this by going into your user folder and then look for a bash profile file, 66 00:04:24,380 --> 00:04:29,210 if you don't have that file, you can simply create it, .bash_profile, 67 00:04:29,390 --> 00:04:34,940 you can then edit this with a text editor and in there, you need to change your so-called path variable 68 00:04:34,940 --> 00:04:38,910 or you need to enhance it or you need to append something to it. 69 00:04:38,930 --> 00:04:50,760 You do this by adding export, starting with a lowercase e, path equal and then use the path to the folder 70 00:04:51,000 --> 00:04:53,510 where you placed your mongodb binaries in, 71 00:04:53,560 --> 00:05:02,670 so in my case this is in here and you can simply right click on that item, on that executable and copy 72 00:05:02,670 --> 00:05:09,210 that path here if you want and move that path into here, starting with one slash 73 00:05:09,210 --> 00:05:15,030 and in my case it's users, my user name, development, mongodb binary because that is the path I use 74 00:05:15,030 --> 00:05:22,080 to place mongodb in. Now important on Mac and Linux, to make sure that all your other commands work, 75 00:05:22,080 --> 00:05:27,690 you need to add a colon and then $path at the end and then save your bash profile file. 76 00:05:27,690 --> 00:05:32,430 Now on Windows, you also need to set this and you do this with a so-called environment variable, 77 00:05:32,550 --> 00:05:38,610 so simply press the Windows key and type environment and you should be suggested this option of setting 78 00:05:38,610 --> 00:05:46,440 environment variables at some point. Open that and there under your user variables, you should also choose 79 00:05:46,530 --> 00:05:54,090 the path variable, edit this variable and then add a new entry here which is the path to the directory 80 00:05:54,150 --> 00:05:58,440 where you installed mongodb and there to the bin directory as well, 81 00:05:58,440 --> 00:06:04,860 so always to the directory where all these executables can be found in. Simply copy that path and add 82 00:06:04,860 --> 00:06:08,920 it to the path variable and then simply save that. 83 00:06:09,060 --> 00:06:14,940 The same is true here for Mac and Linux, save that edited bash profile and thereafter, close 84 00:06:15,030 --> 00:06:17,600 all terminal or command prompt instances 85 00:06:17,610 --> 00:06:22,590 you need to restart both. Once you restarted your terminal or command prompt, you should be able to run 86 00:06:22,590 --> 00:06:23,190 mongo 87 00:06:23,310 --> 00:06:25,550 and it should give you an error that it can't connect, 88 00:06:25,620 --> 00:06:31,270 on Windows it will connect because mongod is running as a service there already, so the mongodb 89 00:06:31,350 --> 00:06:36,630 database server started as a background service because we checked this during the installation, 90 00:06:36,660 --> 00:06:39,990 so on Windows, you now probably already are connected. 91 00:06:40,500 --> 00:06:44,370 Now therefore on Windows, to also see how you could start it differently 92 00:06:44,700 --> 00:06:50,040 and I will have a whole module in this course where I talk a bit more about this shell and connection thing 93 00:06:50,040 --> 00:06:55,860 but on Windows, you can close that command prompt and start a new one as an administrator by right clicking 94 00:06:55,860 --> 00:06:58,950 on the executable and choosing run as administrator 95 00:06:59,340 --> 00:07:06,040 and there, you can now type net stop mongodb written like this and this will only work on Windows, 96 00:07:06,060 --> 00:07:06,660 don't do it, 97 00:07:06,660 --> 00:07:08,770 don't try it on Mac or Linux. 98 00:07:08,790 --> 00:07:13,770 This will stop that background service and now we can also start the mongodb server manually 99 00:07:13,800 --> 00:07:16,400 just as we do it here on Mac and Linux. 100 00:07:16,440 --> 00:07:20,110 You can now start your server by typing mongod, 101 00:07:20,180 --> 00:07:24,930 so mongo basically is a client which allows you to connect to the server and then run commands on 102 00:07:24,930 --> 00:07:26,050 your databases, 103 00:07:26,230 --> 00:07:32,610 mongod starts the mongo database server. So mongod is now a command you can use and when running it, it 104 00:07:32,610 --> 00:07:33,960 might fail. 105 00:07:33,960 --> 00:07:37,150 Now it might fail if you chose a different data 106 00:07:37,290 --> 00:07:38,010 db folder, 107 00:07:38,100 --> 00:07:44,130 I mentioned it would be easiest if you use your root directory so the C-Drive on Windows or if you use 108 00:07:44,130 --> 00:07:47,940 that route directory on Mac and Linux for the data db folder. 109 00:07:47,940 --> 00:07:57,030 If you use a different folder, you need to start your mongod instance here by typing mongodb 110 00:07:57,750 --> 00:08:03,860 --db path and then the path to the place where you created your data db folder. 111 00:08:04,110 --> 00:08:08,540 If that path includes some whitespace in there, you need to wrap it with curly braces, 112 00:08:08,540 --> 00:08:14,430 so for example here, I could type data db, that is redundant though that is the default where it looks 113 00:08:14,430 --> 00:08:19,610 for anyways and that is the path I'm using it here in my operating system, the default as suggested 114 00:08:19,650 --> 00:08:25,350 and on Windows, you can of course also add it in the same way to make sure you point at the folder where 115 00:08:25,350 --> 00:08:27,270 that data is stored. On Windows 116 00:08:27,270 --> 00:08:33,630 actually, I need to change that because there, the default path is inside of the installation folder of mongo 117 00:08:33,630 --> 00:08:34,400 database 118 00:08:34,500 --> 00:08:37,380 and then you start the mongodb server like this. 119 00:08:37,380 --> 00:08:39,900 Now if you type this, it should now start up, 120 00:08:39,900 --> 00:08:46,790 if you still get an error on Mac and Linux, simply try running sudo in front of this entire command, 121 00:08:46,860 --> 00:08:50,970 this will give you the right permissions to store data in that path. 122 00:08:51,330 --> 00:08:56,190 You are prompted to enter your password then and then it should work, on Windows this is not required 123 00:08:56,280 --> 00:08:58,170 and also not available. 124 00:08:58,440 --> 00:09:04,890 Now with that, the mongodb database server is up and running. On Windows, you don't need to do that, 125 00:09:04,980 --> 00:09:09,150 after restarting your system, the mongodb service should be running in the background again and you 126 00:09:09,150 --> 00:09:15,330 can absolutely use that, it's same server, just automatically started in the background. On Mac and Linux, 127 00:09:15,360 --> 00:09:17,250 you have to start it manually for now, 128 00:09:17,460 --> 00:09:22,920 I have a whole module where I dive deeper into that and explain how to start it as a service on Mac and 129 00:09:22,920 --> 00:09:23,890 Linux too. 130 00:09:24,210 --> 00:09:25,740 So now with that up and running, 131 00:09:25,830 --> 00:09:31,590 open a new window, a new terminal or command prompt window and leave the old one running, 132 00:09:31,590 --> 00:09:36,960 you need to leave that process running as long as you work on it because that is your running database 133 00:09:36,960 --> 00:09:37,500 server, 134 00:09:37,500 --> 00:09:39,960 if you shut that down, you can't work with it anymore. 135 00:09:40,140 --> 00:09:45,510 You can always quit it by pressing control c, this will quit and simply exit 136 00:09:45,810 --> 00:09:49,060 but then you need to restart if you want to work on it again. 137 00:09:49,080 --> 00:09:56,190 So in that new window, you can now run mongo and this will automatically connect to the running server, 138 00:09:56,250 --> 00:10:02,940 so now you are in the mongo shell which is the environment where you can run commands against your database 139 00:10:02,940 --> 00:10:08,410 server, where you can create new databases, new collections and most importantly, new documents. 140 00:10:08,580 --> 00:10:11,220 And this is now what we'll work on in the next lecture.