0 1 00:00:00,510 --> 00:00:07,020 All right now that you've got MongoDB installed on your local system the next step is to go ahead 1 2 00:00:07,110 --> 00:00:07,970 and use it 2 3 00:00:08,010 --> 00:00:08,550 right? 3 4 00:00:08,550 --> 00:00:14,730 So let's head over to the www.mongodb.com website and we're going to navigate to the documentation 4 5 00:00:15,180 --> 00:00:19,100 through the docs tab and then we're going to head over to getting started. 5 6 00:00:19,320 --> 00:00:25,700 And if you take a look over here, there's the MongoDB CRUD operations. 6 7 00:00:25,770 --> 00:00:31,680 I recommend that whenever you're exploring a new database system the first thing to wrap your head around 7 8 00:00:31,770 --> 00:00:36,090 is how do you perform CRUD using that particular database. 8 9 00:00:36,090 --> 00: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 10 00:00:43,950 --> 00:00:46,960 in the last module. In this section 10 11 00:00:46,980 --> 00:00:52,740 we're going to look at how we can do that using MongoDB on the command line. 11 12 00:00:52,770 --> 00: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 13 00:01:00,690 --> 00:01:05,020 some people call it 'mongod', and this will spin up our mongo server 13 14 00:01:05,070 --> 00:01:13,570 when you hit enter. Once you see waiting for connections on port 27017 then you are ready to go. 14 15 00:01:13,650 --> 00:01:16,700 This is kind of similar to how we had a localhost 3000. 15 16 00:01:16,800 --> 00:01:22,600 But in this case we've got a local database that's being served up on this particular port. 16 17 00:01:22,860 --> 00: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 18 00:01:30,990 --> 00:01:37,960 T, then we can open up a brand new terminal. And this is a different connection because if you notice 18 19 00:01:37,960 --> 00:01:41,940 him where on MongoDB database is running 19 20 00:01:41,980 --> 00:01:43,400 we actually don't see our prompt. 20 21 00:01:43,420 --> 00: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 22 00:01:49,750 --> 00:01:52,470 don't have an active connection to our terminal. 22 23 00:01:52,540 --> 00:01:55,810 This terminal is connected to the MongoDB database. 23 24 00:01:56,170 --> 00: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 25 00:02:04,840 --> 00:02:05,110 that 25 26 00:02:05,110 --> 00:02:08,870 the key word is mongo without the 'd'. 26 27 00:02:08,980 --> 00:02:16,030 And now if we hit enter and you see your prompt being changed to this > then you are ready 27 28 00:02:16,060 --> 00:02:18,880 to use the Mongo shell. 28 29 00:02:18,950 --> 00: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 30 00:02:25,010 --> 00:02:28,260 local system using the command line. 30 31 00:02:28,550 --> 00: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 32 00:02:34,760 --> 00:02:36,540 have. How do we do that? 32 33 00:02:36,590 --> 00:02:41,150 Well if at any point you get stuck and you're inside the Mongo shell 33 34 00:02:41,150 --> 00: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 35 00:02:48,110 --> 00:02:53,810 you will be able to get a helpful list of things that you can do while using the Mongo shell. 35 36 00:02:53,810 --> 00: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 37 00:02:59,930 --> 00:03:04,910 the names of the database that we currently have on our local system. 37 38 00:03:04,910 --> 00: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 39 00:03:12,980 --> 00:03:21,170 MongoDB, it comes preloaded with three databases: admin, config and local and you can see that all 39 40 00:03:21,170 --> 00:03:26,560 of them are taking up zero gigabytes of space because they're completely empty. 40 41 00:03:26,570 --> 00:03:31,840 This is what it would look like if you have a brand new configuration of MongoDB. 41 42 00:03:31,910 --> 00: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 43 00:03:41,620 --> 00:03:44,980 and then we specify the name of our database. 43 44 00:03:45,100 --> 00: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 45 00:03:52,540 --> 00:03:57,400 I can write use shopDB and hit enter. 45 46 00:03:57,400 --> 00:04:03,240 So now Mongo tells me that I've switched to the database that is called ShopDB. 46 47 00:04:03,250 --> 00:04:09,700 But if I go back and I write show dbs, I don't see shopDB anywhere. 47 48 00:04:10,120 --> 00:04:14,560 Well that's because for it to be listed, it has to have some content. 48 49 00:04:14,560 --> 00:04:16,490 So we first have to give it some data. 49 50 00:04:16,720 --> 00:04:24,450 We're now ready to tackle the 'C' in CRUD and see how we can create data using MongoDB. 50 51 00:04:24,690 --> 00: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 52 00:04:31,500 --> 00:04:35,230 tells you that you're currently working within the shop database. 52 53 00:04:35,610 --> 00:04:38,690 So how do we create data in our database? 53 54 00:04:38,700 --> 00:04:45,090 This is the time to head back over to our documentation where we've got the MongoDB CRUD operations 54 55 00:04:45,120 --> 00:04:46,410 explained to us. 55 56 00:04:46,410 --> 00: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 57 00:04:54,360 --> 00:04:58,520 into our database, the syntax looks something like this. 57 58 00:04:58,620 --> 00:05:05,260 We write db.collection and this collection is actually the name of a collection. 58 59 00:05:05,310 --> 00:05:11,820 And if that collection name doesn't currently exist then by writing this line and by executing it, it 59 60 00:05:11,820 --> 00:05:18,920 will actually create that collection. And then we use a method such as insertOne or insertMany. 60 61 00:05:19,380 --> 00:05:22,750 And this is an example of how you would use this method. 61 62 00:05:23,010 --> 00:05:30,000 So for example if you wanted a collection called users inside your database you would say db.users. 62 63 00:05:30,090 --> 00:05:31,610 insertOne. 63 64 00:05:31,860 --> 00:05:39,250 And inside the method insertone you would pass over a Javascript object which will be the document. 64 65 00:05:39,750 --> 00:05:42,840 So it will have fields and it will have values. 65 66 00:05:43,260 --> 00:05:49,590 And this pretty much follows the key value pairs that we've been seeing in Javascript objects. 66 67 00:05:49,660 --> 00:05:54,120 Let's take what we learn here and let's implement it for our shopDB. 67 68 00:05:54,250 --> 00: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 69 00:06:01,960 --> 00:06:07,630 in MongoDB is kind of similar to tables in the SQL world. 69 70 00:06:07,720 --> 00:06:14,350 They are a collection of documents and a document is simply just a single data record, 70 71 00:06:14,350 --> 00:06:16,080 so that would be a single row 71 72 00:06:16,150 --> 00:06:22,660 if you were using a SQL based database. If this collection products doesn't currently exist in our 72 73 00:06:22,660 --> 00:06:31,300 database then by executing this line of code it will create that collection called products. And then 73 74 00:06:31,300 --> 00:06:40,030 we write another . and we call the method insertOne and we open up a set of round brackets or parentheses. 74 75 00:06:40,660 --> 00:06:42,950 As the inputs for this method 75 76 00:06:42,980 --> 00:06:53,140 insertOne we get to insert a Javascript object. The product that we want to insert is the pen product 76 77 00:06:53,140 --> 00:06:55,110 that we had previously. 77 78 00:06:55,120 --> 00:06:59,310 So in this case it will have an id of 1, 78 79 00:06:59,680 --> 00:07:09,550 it will have a name of let's say Pen and it will have a price of 1.20. 79 80 00:07:09,550 --> 00: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 81 00:07:18,670 --> 00: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 82 00:07:28,690 --> 00:07:34,390 and it will show me all the collections that currently exist in the current database which remember 82 83 00:07:34,390 --> 00:07:35,620 is shopDB. 83 84 00:07:35,620 --> 00: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 85 00:07:43,210 --> 00:07:46,010 which is my pen product. 85 86 00:07:46,210 --> 00:07:52,170 This is how easy it is to add data to our database using MongoDB. 86 87 00:07:52,340 --> 00:07:59,050 And if you look at the documentation you can see that you can either insertOne or you can insertMany. 87 88 00:07:59,180 --> 00:08:05,470 And if you click on it you can see the syntax as well as examples for each of these methods. 88 89 00:08:07,250 --> 00:08:14,270 MongoDB is extremely well documented and pretty much anything you want to do with it is explained really 89 90 00:08:14,270 --> 00:08:19,150 well through the use of graphics and examples in their documentation. 90 91 00:08:19,160 --> 00: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 92 00:08:26,810 --> 00:08:31,920 second product in our products collection which is the pencil product. 92 93 00:08:31,970 --> 00:08:33,280 Pause the video now 93 94 00:08:33,380 --> 00:08:39,360 and in your Mongo shell create the second product. 94 95 00:08:39,420 --> 00:08:42,950 OK so we're going to follow the format that we had previously. 95 96 00:08:42,990 --> 00:08:45,150 We're going to tap into our database, 96 97 00:08:45,150 --> 00: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 98 00:08:52,020 --> 00: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 99 00:09:02,070 --> 00:09:04,830 this time because this is the second entry. 99 100 00:09:05,160 --> 00: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 101 00:09:13,890 --> 00:09:15,000 80p. 101 102 00:09:15,210 --> 00:09:20,260 And when we hit enter, then we've created our second document. 102 103 00:09:21,000 --> 00:09:27,790 So now we have a database called ShopDB that contains a single collection called products 103 104 00:09:27,900 --> 00:09:35,250 and inside this collection is two documents one with this data and another with this data.