WEBVTT 0 00:00.510 --> 00:07.020 All right now that you've got MongoDB installed on your local system the next step is to go ahead 1 00:07.110 --> 00:07.970 and use it 2 00:08.010 --> 00:08.550 right? 3 00:08.550 --> 00:14.730 So let's head over to the www.mongodb.com website and we're going to navigate to the documentation 4 00:15.180 --> 00:19.100 through the docs tab and then we're going to head over to getting started. 5 00:19.320 --> 00:25.700 And if you take a look over here, there's the MongoDB CRUD operations. 6 00:25.770 --> 00:31.680 I recommend that whenever you're exploring a new database system the first thing to wrap your head around 7 00:31.770 --> 00:36.090 is how do you perform CRUD using that particular database. 8 00:36.090 --> 00:43.680 So we saw how we can create, read, update and delete data using a SQL based system such as SQL lite 9 00:43.950 --> 00:46.960 in the last module. In this section 10 00:46.980 --> 00:52.740 we're going to look at how we can do that using MongoDB on the command line. 11 00:52.770 --> 01:00.690 So this is the time to open up your hyperterminal. And after the prompt I'm going to type mongod or 12 01:00.690 --> 01:05.020 some people call it 'mongod', and this will spin up our mongo server 13 01:05.070 --> 01:13.570 when you hit enter. Once you see waiting for connections on port 27017 then you are ready to go. 14 01:13.650 --> 01:16.700 This is kind of similar to how we had a localhost 3000. 15 01:16.800 --> 01:22.600 But in this case we've got a local database that's being served up on this particular port. 16 01:22.860 --> 01:30.960 So now if you're go into Shell and new tab or if take a look here at the shortcut which on Mac is COMAND + 17 01:30.990 --> 01:37.960 T, then we can open up a brand new terminal. And this is a different connection because if you notice 18 01:37.960 --> 01:41.940 him where on MongoDB database is running 19 01:41.980 --> 01:43.400 we actually don't see our prompt. 20 01:43.420 --> 01:49.750 We can't type something in here and we can't for example cd over to the desktop over here because we 21 01:49.750 --> 01:52.470 don't have an active connection to our terminal. 22 01:52.540 --> 01:55.810 This terminal is connected to the MongoDB database. 23 01:56.170 --> 02:04.840 In our new terminal however we do now have a prompt and we can now tap into the Mongo shell. And to do 24 02:04.840 --> 02:05.110 that 25 02:05.110 --> 02:08.870 the key word is mongo without the 'd'. 26 02:08.980 --> 02:16.030 And now if we hit enter and you see your prompt being changed to this > then you are ready 27 02:16.060 --> 02:18.880 to use the Mongo shell. 28 02:18.950 --> 02:25.010 So the Mongo shell is basically just a way for us to be able to interact with our MongoDB databases on our 29 02:25.010 --> 02:28.260 local system using the command line. 30 02:28.550 --> 02:34.760 But before we create a brand new database, it's good practice to take a look at what databases we already 31 02:34.760 --> 02:36.540 have. How do we do that? 32 02:36.590 --> 02:41.150 Well if at any point you get stuck and you're inside the Mongo shell 33 02:41.150 --> 02:48.020 so you've got your little angle bracket showing there, you can always type help, as a cry for help, and 34 02:48.110 --> 02:53.810 you will be able to get a helpful list of things that you can do while using the Mongo shell. 35 02:53.810 --> 02:59.930 So the one that I want to highlight to you is this one which is the command show dbs. And this shows 36 02:59.930 --> 03:04.910 the names of the database that we currently have on our local system. 37 03:04.910 --> 03:12.980 So let's go ahead and try that out. Show dbs. Hit enter. And you can see that by default when you install 38 03:12.980 --> 03:21.170 MongoDB, it comes preloaded with three databases: admin, config and local and you can see that all 39 03:21.170 --> 03:26.560 of them are taking up zero gigabytes of space because they're completely empty. 40 03:26.570 --> 03:31.840 This is what it would look like if you have a brand new configuration of MongoDB. 41 03:31.910 --> 03:41.470 So let's go ahead and create our very first database. And to do that all we have to do is type use 42 03:41.620 --> 03:44.980 and then we specify the name of our database. 43 03:45.100 --> 03:52.060 So let's say the I wanted to create a database again for my shop so I might call my database shopDB. 44 03:52.540 --> 03:57.400 I can write use shopDB and hit enter. 45 03:57.400 --> 04:03.240 So now Mongo tells me that I've switched to the database that is called ShopDB. 46 04:03.250 --> 04:09.700 But if I go back and I write show dbs, I don't see shopDB anywhere. 47 04:10.120 --> 04:14.560 Well that's because for it to be listed, it has to have some content. 48 04:14.560 --> 04:16.490 So we first have to give it some data. 49 04:16.720 --> 04:24.450 We're now ready to tackle the 'C' in CRUD and see how we can create data using MongoDB. 50 04:24.690 --> 04:31.500 If you ever want to know which database you're currently in, you can always type db hit enter and it 51 04:31.500 --> 04:35.230 tells you that you're currently working within the shop database. 52 04:35.610 --> 04:38.690 So how do we create data in our database? 53 04:38.700 --> 04:45.090 This is the time to head back over to our documentation where we've got the MongoDB CRUD operations 54 04:45.120 --> 04:46.410 explained to us. 55 04:46.410 --> 04:54.300 So what we want to do is to create and you can see that in order to create one entry or many entries 56 04:54.360 --> 04:58.520 into our database, the syntax looks something like this. 57 04:58.620 --> 05:05.260 We write db.collection and this collection is actually the name of a collection. 58 05:05.310 --> 05:11.820 And if that collection name doesn't currently exist then by writing this line and by executing it, it 59 05:11.820 --> 05:18.920 will actually create that collection. And then we use a method such as insertOne or insertMany. 60 05:19.380 --> 05:22.750 And this is an example of how you would use this method. 61 05:23.010 --> 05:30.000 So for example if you wanted a collection called users inside your database you would say db.users. 62 05:30.090 --> 05:31.610 insertOne. 63 05:31.860 --> 05:39.250 And inside the method insertone you would pass over a Javascript object which will be the document. 64 05:39.750 --> 05:42.840 So it will have fields and it will have values. 65 05:43.260 --> 05:49.590 And this pretty much follows the key value pairs that we've been seeing in Javascript objects. 66 05:49.660 --> 05:54.120 Let's take what we learn here and let's implement it for our shopDB. 67 05:54.250 --> 06:01.810 So let's say db. and the collection that we want to make is the one that's called products. Collections 68 06:01.960 --> 06:07.630 in MongoDB is kind of similar to tables in the SQL world. 69 06:07.720 --> 06:14.350 They are a collection of documents and a document is simply just a single data record, 70 06:14.350 --> 06:16.080 so that would be a single row 71 06:16.150 --> 06:22.660 if you were using a SQL based database. If this collection products doesn't currently exist in our 72 06:22.660 --> 06:31.300 database then by executing this line of code it will create that collection called products. And then 73 06:31.300 --> 06:40.030 we write another . and we call the method insertOne and we open up a set of round brackets or parentheses. 74 06:40.660 --> 06:42.950 As the inputs for this method 75 06:42.980 --> 06:53.140 insertOne we get to insert a Javascript object. The product that we want to insert is the pen product 76 06:53.140 --> 06:55.110 that we had previously. 77 06:55.120 --> 06:59.310 So in this case it will have an id of 1, 78 06:59.680 --> 07:09.550 it will have a name of let's say Pen and it will have a price of 1.20. 79 07:09.550 --> 07:17.810 This is all the data that I want to add to each record or each document inside my product's collection. 80 07:18.670 --> 07:27.510 And when I hit enter my data gets added to my database. And now I can use the key words show collections 81 07:28.690 --> 07:34.390 and it will show me all the collections that currently exist in the current database which remember 82 07:34.390 --> 07:35.620 is shopDB. 83 07:35.620 --> 07:42.970 So I've only got one collection which is called products. And in my products I've only got one entry 84 07:43.210 --> 07:46.010 which is my pen product. 85 07:46.210 --> 07:52.170 This is how easy it is to add data to our database using MongoDB. 86 07:52.340 --> 07:59.050 And if you look at the documentation you can see that you can either insertOne or you can insertMany. 87 07:59.180 --> 08:05.470 And if you click on it you can see the syntax as well as examples for each of these methods. 88 08:07.250 --> 08:14.270 MongoDB is extremely well documented and pretty much anything you want to do with it is explained really 89 08:14.270 --> 08:19.150 well through the use of graphics and examples in their documentation. 90 08:19.160 --> 08:26.810 So now it's your turn to create a new document using the Mongo shell and you're going to create the 91 08:26.810 --> 08:31.920 second product in our products collection which is the pencil product. 92 08:31.970 --> 08:33.280 Pause the video now 93 08:33.380 --> 08:39.360 and in your Mongo shell create the second product. 94 08:39.420 --> 08:42.950 OK so we're going to follow the format that we had previously. 95 08:42.990 --> 08:45.150 We're going to tap into our database, 96 08:45.150 --> 08:51.960 we're going to look for the collection that's called products and then we're going to again insert one 97 08:52.020 --> 09:01.890 product. And that product is going to be again a Javascript object and it's going to have an id of 2 98 09:02.070 --> 09:04.830 this time because this is the second entry. 99 09:05.160 --> 09:13.830 And then it's going to have a name of pencil and it will have a price of 0.80, so 80 cents or 100 09:13.890 --> 09:15.000 80p. 101 09:15.210 --> 09:20.260 And when we hit enter, then we've created our second document. 102 09:21.000 --> 09:27.790 So now we have a database called ShopDB that contains a single collection called products 103 09:27.900 --> 09:35.250 and inside this collection is two documents one with this data and another with this data.