1 00:00:01,130 --> 00:00:03,660 So no matter if you installed MongoDB 2 00:00:03,660 --> 00:00:07,080 on Windows or on Mac OS, we will now create 3 00:00:07,080 --> 00:00:10,570 our first local database, using the Mongo Shell. 4 00:00:10,570 --> 00:00:13,780 So this terminal application which is the Mongo Shell 5 00:00:13,780 --> 00:00:16,900 works exactly the same on Windows and Mac, 6 00:00:16,900 --> 00:00:19,390 so from now on it doesn't matter anymore which 7 00:00:19,390 --> 00:00:20,633 platform you're using. 8 00:00:21,860 --> 00:00:24,810 So here in this terminal tab I still have 9 00:00:24,810 --> 00:00:27,730 my Mongo server basically running in the background 10 00:00:27,730 --> 00:00:31,600 on this port, and so let's go ahead and open up 11 00:00:31,600 --> 00:00:33,260 the Mongo Shell. 12 00:00:33,260 --> 00:00:36,060 So remember, that is just typing Mongo here 13 00:00:36,060 --> 00:00:38,660 in the command line and that will then open up 14 00:00:38,660 --> 00:00:42,580 the Mongo Shell, then Command K to clear the terminal, 15 00:00:42,580 --> 00:00:46,370 and so now let's actually create our first database. 16 00:00:46,370 --> 00:00:49,320 And we do that by using "use". 17 00:00:49,320 --> 00:00:53,060 So we use the "use" command inside the Mongo Shell, 18 00:00:53,060 --> 00:00:56,290 and then the name of the database that we want to create. 19 00:00:56,290 --> 00:00:58,760 Now this command here is also used to switch to 20 00:00:58,760 --> 00:01:01,830 a already existing database, but if we try 21 00:01:01,830 --> 00:01:04,570 to basically switch to a database that doesn't 22 00:01:04,570 --> 00:01:07,580 yet exist, it will then create a new one. 23 00:01:07,580 --> 00:01:08,650 So let's say, "use 24 00:01:10,080 --> 00:01:11,550 natours- 25 00:01:11,550 --> 00:01:12,840 test", 26 00:01:12,840 --> 00:01:13,673 okay. 27 00:01:13,673 --> 00:01:17,750 And so it created that database, and it also switched to it. 28 00:01:17,750 --> 00:01:20,380 Okay, and now this blank database 29 00:01:20,380 --> 00:01:22,830 is ready to receive some data. 30 00:01:22,830 --> 00:01:26,350 Now remember that inside a database we have collections, 31 00:01:26,350 --> 00:01:29,180 and then each collection has documents in it. 32 00:01:29,180 --> 00:01:31,430 And the data that we create in the Mongo Shell 33 00:01:31,430 --> 00:01:33,000 is always documents. 34 00:01:33,000 --> 00:01:35,710 And so of course we have to create the document 35 00:01:35,710 --> 00:01:38,730 inside of a collection, and so we specify that 36 00:01:38,730 --> 00:01:41,767 collection before we insert a document. 37 00:01:41,767 --> 00:01:45,340 And this works like this, so db, which stands 38 00:01:45,340 --> 00:01:48,160 for the current database, which is in this case 39 00:01:48,160 --> 00:01:51,450 natours-test, and then we specify the name of the collection 40 00:01:52,430 --> 00:01:54,560 which is tours, and then on that 41 00:01:54,560 --> 00:01:56,653 we use the insertMany function. 42 00:01:58,960 --> 00:02:02,948 So insertMany just like that, so just to recap here, 43 00:02:02,948 --> 00:02:06,760 Database is the currently used database that is 44 00:02:06,760 --> 00:02:09,450 right now active, and when we want to insert a 45 00:02:09,450 --> 00:02:12,470 document into it we need to specify the collection 46 00:02:12,470 --> 00:02:14,220 where that document is gonna live. 47 00:02:14,220 --> 00:02:16,610 And we do that by using dot, and then 48 00:02:16,610 --> 00:02:19,870 the name of the collection, which is in this case "tours". 49 00:02:19,870 --> 00:02:22,280 Now right now this collection hasn't been created, 50 00:02:22,280 --> 00:02:26,600 and so it will create it once we run this command, okay. 51 00:02:26,600 --> 00:02:29,830 And later on we will then have a collection for users, 52 00:02:29,830 --> 00:02:32,790 or for reviews, so basically for all the resources 53 00:02:32,790 --> 00:02:35,890 that we created in the last section, right. 54 00:02:35,890 --> 00:02:38,410 So, using tours here because that is one of our 55 00:02:38,410 --> 00:02:41,310 resources in our application, okay. 56 00:02:41,310 --> 00:02:43,740 But don't worry too much about that for now, 57 00:02:43,740 --> 00:02:46,920 we will talk about data modeling a bit later in the course. 58 00:02:46,920 --> 00:02:50,350 Okay, anyway, let's now actually insert a document 59 00:02:50,350 --> 00:02:52,210 into this database collection. 60 00:02:52,210 --> 00:02:53,860 And it's actually very simple. 61 00:02:53,860 --> 00:02:57,590 So, remember that MongoDb uses BSON, 62 00:02:57,590 --> 00:02:59,960 which is quite similar to JSON, 63 00:02:59,960 --> 00:03:02,990 so we can actually simply pass a JavaScript object 64 00:03:02,990 --> 00:03:05,540 into this insertMany function, and it will 65 00:03:05,540 --> 00:03:10,000 then convert it into JSON and BSON, okay. 66 00:03:10,000 --> 00:03:12,800 So a regular JavaScript object like this, 67 00:03:12,800 --> 00:03:14,730 and then just like before, let's define 68 00:03:14,730 --> 00:03:15,833 the name of the tour. 69 00:03:19,370 --> 00:03:21,950 And I'm calling it The Forest Hiker, which is 70 00:03:21,950 --> 00:03:25,330 one of the tours that we actually have in our JSON 71 00:03:25,330 --> 00:03:28,580 document at this point, so The Forest Hiker, 72 00:03:28,580 --> 00:03:30,450 let's set a price 73 00:03:30,450 --> 00:03:31,283 to 74 00:03:32,359 --> 00:03:33,192 297, 75 00:03:34,080 --> 00:03:38,550 and then also a rating, like an average rating, 76 00:03:38,550 --> 00:03:39,760 which can be 4.7 77 00:03:40,848 --> 00:03:41,681 okay. 78 00:03:41,681 --> 00:03:45,510 So this here looks like just a regular JavaScript object, 79 00:03:45,510 --> 00:03:48,640 now we could also use quotes on the property names, 80 00:03:48,640 --> 00:03:51,450 but that is actually optional, okay. 81 00:03:51,450 --> 00:03:52,943 Now here on these, 82 00:03:53,900 --> 00:03:56,100 and here we probably need the double quotes, 83 00:03:57,380 --> 00:04:01,430 okay so JSON does not recognize the single quotes, 84 00:04:01,430 --> 00:04:03,360 and so to be on the safe side 85 00:04:03,360 --> 00:04:05,720 let's just use the double quotes here, even though 86 00:04:05,720 --> 00:04:08,530 I usually like to use the single ones. 87 00:04:08,530 --> 00:04:11,460 And now just before we hit return, we actually need to 88 00:04:11,460 --> 00:04:14,275 also correct this function here, because it's not 89 00:04:14,275 --> 00:04:17,180 insertMany, because we're actually only inserting 90 00:04:17,180 --> 00:04:18,453 one document here, 91 00:04:19,390 --> 00:04:22,300 so it's called insertOne, okay. 92 00:04:22,300 --> 00:04:25,630 So we use insertMany to create multiple documents, 93 00:04:25,630 --> 00:04:28,930 and we use insertOne when we just wanna create one. 94 00:04:28,930 --> 00:04:32,090 Okay, so let's now hit return, and just like this 95 00:04:32,090 --> 00:04:35,650 we created our first document in our database. 96 00:04:35,650 --> 00:04:39,022 Okay, and now just to check that, let's use DB, 97 00:04:39,022 --> 00:04:42,430 which again is the current database, then .tours 98 00:04:44,367 --> 00:04:45,200 .find 99 00:04:47,050 --> 00:04:49,850 all right and here indeed is the document 100 00:04:49,850 --> 00:04:51,310 that we just created. 101 00:04:51,310 --> 00:04:54,120 And notice how it also automatically created 102 00:04:54,120 --> 00:04:57,500 this object ID here which is the unique identifier 103 00:04:57,500 --> 00:04:58,930 of this document. 104 00:04:58,930 --> 00:05:02,320 So remember how I said that MongoDB would automatically 105 00:05:02,320 --> 00:05:05,750 create these unique identifiers behind the scenes, 106 00:05:05,750 --> 00:05:08,400 and so that's exactly what you see here. 107 00:05:08,400 --> 00:05:11,100 And besides that, well, you see that this really 108 00:05:11,100 --> 00:05:13,970 is just a regular JSON object. 109 00:05:13,970 --> 00:05:17,290 And so this makes it great and really easy to work with 110 00:05:17,290 --> 00:05:20,050 JavaScript, and so as you can guess, this is going to 111 00:05:20,050 --> 00:05:24,500 make it really easy to work with MongoDB data in JavaScript. 112 00:05:24,500 --> 00:05:27,360 Because we're already using kind of the same format 113 00:05:27,360 --> 00:05:29,610 that we're already used to in JavaScript. 114 00:05:29,610 --> 00:05:32,300 And so that is one of the main reasons why MongoDB 115 00:05:32,300 --> 00:05:35,730 is so popular for Node JS applications. 116 00:05:35,730 --> 00:05:40,300 Okay, now another very useful command is show dbs, 117 00:05:40,300 --> 00:05:44,090 which will basically show us all the databases that we have 118 00:05:44,090 --> 00:05:48,540 in MongoDB, and so here you see our natours-test database. 119 00:05:48,540 --> 00:05:50,000 But we also have some other ones, 120 00:05:50,000 --> 00:05:52,570 which MongoDB automatically creates for us. 121 00:05:52,570 --> 00:05:56,070 And yours might not be the same as these three, 122 00:05:56,070 --> 00:05:58,810 but if not, don't worry, and just like before 123 00:05:58,810 --> 00:06:02,710 we could use the "use" command to switch to one of these. 124 00:06:02,710 --> 00:06:06,470 So let's say we now wanted to use admin, and so now 125 00:06:06,470 --> 00:06:09,280 we switched to DB admin, all right. 126 00:06:09,280 --> 00:06:12,800 So again, "use" is to switch to an existing database 127 00:06:12,800 --> 00:06:15,210 or to create a new one, if the name that 128 00:06:15,210 --> 00:06:17,293 we pass into it does not yet exist. 129 00:06:19,420 --> 00:06:22,667 So we want to go back to natours-test 130 00:06:22,667 --> 00:06:24,840 because now I want to show you 131 00:06:24,840 --> 00:06:27,370 just another quick command here, 132 00:06:27,370 --> 00:06:28,887 so "show collections". 133 00:06:30,400 --> 00:06:33,000 And so here you see the tours collection 134 00:06:33,000 --> 00:06:34,720 that we just created before. 135 00:06:34,720 --> 00:06:38,550 So remember how I said that back here when we created 136 00:06:38,550 --> 00:06:41,830 the document we also created the tours collection, 137 00:06:41,830 --> 00:06:44,790 because every document always has to be inside of 138 00:06:44,790 --> 00:06:45,760 a collection. 139 00:06:45,760 --> 00:06:48,630 Okay, so these are the very basic commands 140 00:06:48,630 --> 00:06:51,320 in the Mongo Shell, and over the next couple of lectures 141 00:06:51,320 --> 00:06:54,670 we will then create documents, query them, update them, 142 00:06:54,670 --> 00:06:55,970 and also delete them. 143 00:06:55,970 --> 00:06:58,243 So basically all the craft's operation. 144 00:06:59,460 --> 00:07:02,520 For now let me just show how we can actually quit 145 00:07:02,520 --> 00:07:03,800 the Mongo Shell. 146 00:07:03,800 --> 00:07:06,740 So, very easy, you just type quit like this 147 00:07:06,740 --> 00:07:08,660 and it will then exit it. 148 00:07:08,660 --> 00:07:11,630 Just to quickly show you that our database was 149 00:07:11,630 --> 00:07:16,020 actually persisted, let's simply run the Shell again, 150 00:07:16,020 --> 00:07:19,890 then clear it quickly again, "show dbs", 151 00:07:19,890 --> 00:07:22,430 and you see that natours is still here, 152 00:07:22,430 --> 00:07:23,813 and we can also use it. 153 00:07:27,820 --> 00:07:30,980 Okay, so everything is still there, meaning 154 00:07:30,980 --> 00:07:34,313 that we're now ready to move on to the next video.