1 00:00:02,540 --> 00:00:07,610 So now that everything is set up and you should have a working mongodb server and the shell 2 00:00:07,610 --> 00:00:08,770 connected to it, 3 00:00:08,900 --> 00:00:14,470 let me clear this screen by running cls and this only well clears the screen, it doesn't do anything else 4 00:00:14,690 --> 00:00:16,690 and now let's work with the database, 5 00:00:16,700 --> 00:00:18,310 let's get started working. 6 00:00:18,320 --> 00:00:23,750 First of all, you can type show dbs to see existing databases and you do start off with three different 7 00:00:23,750 --> 00:00:26,040 databases that store some metadata, 8 00:00:26,120 --> 00:00:27,990 we don't need to worry about these. 9 00:00:28,310 --> 00:00:33,230 Now first of all, let's connect to a database with the use command and you can connect to a brand new 10 00:00:33,230 --> 00:00:35,400 database by simply typing its name 11 00:00:35,420 --> 00:00:41,980 even if this doesn't exist yet, it will be created on the fly once you start inserting data. Now in there, 12 00:00:41,990 --> 00:00:48,050 you can create a new collection also on the fly because no collection exists right now and you do this 13 00:00:48,050 --> 00:00:48,840 by typing 14 00:00:48,850 --> 00:00:52,510 db, this refers to the database you are currently connected to, 15 00:00:52,640 --> 00:00:58,400 so shop in my case and then the name of the collection where you want to insert data, let's say products 16 00:00:58,520 --> 00:01:00,730 and this also doesn't need to exist yet. 17 00:01:00,920 --> 00:01:07,700 And then you can insert one new product with the insertOne command and you pass this json object 18 00:01:07,700 --> 00:01:08,190 to it, 19 00:01:08,270 --> 00:01:09,950 so this double curly braces 20 00:01:10,010 --> 00:01:13,710 surround the data you want to insert into this collection 21 00:01:14,120 --> 00:01:20,450 and there you can add your keys, your key value fields, like name Max. 22 00:01:20,450 --> 00:01:26,390 Now the good thing is here, this would work but you can omit the quotation marks around the key name 23 00:01:26,420 --> 00:01:31,730 here in the shell, not around the value because this should be some text and text is marked by quotation 24 00:01:31,730 --> 00:01:34,170 marks but around the key name you can omit it, 25 00:01:34,180 --> 00:01:35,540 this is simply a little feature, 26 00:01:35,540 --> 00:01:38,750 it will be stored with the quotation marks behind the scenes. 27 00:01:38,750 --> 00:01:44,690 Now we can add a second value by adding a comma and then the age, 29 and now we will insert a document 28 00:01:44,750 --> 00:01:46,790 with the name and the age. 29 00:01:46,820 --> 00:01:49,560 However for a product, this doesn't make too much sense, 30 00:01:49,610 --> 00:01:54,570 so let's maybe name it a book and then give it a price of 12.99. 31 00:01:54,580 --> 00:01:55,920 So if I now hit enter, 32 00:01:55,970 --> 00:02:01,520 you should get this command which basically confirms that the data was inserted into the database and 33 00:02:01,610 --> 00:02:07,010 that mongodb automatically generated a unique ID for it which it returns you here. 34 00:02:07,040 --> 00:02:09,610 Now let's have a look at our data then right, 35 00:02:09,620 --> 00:02:12,290 this can be done by simply running db products, 36 00:02:12,290 --> 00:02:17,120 so again the currently connected database and then the collection we stored the data in 37 00:02:17,120 --> 00:02:23,420 and then by typing find. This gives us if you add no other arguments, which we'll do in the course but 38 00:02:23,420 --> 00:02:24,260 not right now, 39 00:02:24,410 --> 00:02:29,320 if you add no other arguments this will give you all the data in that collection, find. 40 00:02:29,330 --> 00:02:31,420 So this gives us the one entry we added 41 00:02:31,490 --> 00:02:35,090 and here you see the quotation marks around the key names too. 42 00:02:35,090 --> 00:02:40,850 Now there also is another version number of this, you can add .pretty at the end to simply output it in 43 00:02:40,850 --> 00:02:41,730 a prettier way, 44 00:02:41,750 --> 00:02:44,620 it's still the same data, just easier to read. 45 00:02:44,870 --> 00:02:46,870 Now try that on your own, pause the video, 46 00:02:46,880 --> 00:02:50,300 try that on your own and add another product on your own, 47 00:02:50,300 --> 00:02:58,770 although try adding name and price and also one extra field, like the description with some dummy text. Were you 48 00:02:58,770 --> 00:02:59,660 successful? 49 00:02:59,910 --> 00:03:04,180 Well you would repeat db products insertOne 50 00:03:04,380 --> 00:03:09,810 and by the way, if you repeat a command which you executed before, you can also just press the up key 51 00:03:09,930 --> 00:03:12,950 on your keyboard to cycle through your past commands, 52 00:03:13,040 --> 00:03:18,330 this allows you to quickly go back to a past command, simply edit it a bit and re-execute it, 53 00:03:18,570 --> 00:03:20,270 this will not change the past data, 54 00:03:20,310 --> 00:03:23,360 it will be treated as a new command but it's easier for you, 55 00:03:23,370 --> 00:03:25,470 you don't have to write everything again. 56 00:03:25,470 --> 00:03:30,340 So here you could add a T-shirt with some price 57 00:03:30,360 --> 00:03:35,580 and as I said, now let's also add a new key, a description where I 58 00:03:35,640 --> 00:03:41,840 say this is a high quality T-shirt or whatever you want. If I now hit enter, 59 00:03:41,910 --> 00:03:43,690 this will be inserted as well 60 00:03:43,890 --> 00:03:47,450 and now let me clear the screen again with the cls command 61 00:03:47,760 --> 00:03:48,960 and let me execute find 62 00:03:48,960 --> 00:03:49,830 pretty again 63 00:03:49,830 --> 00:03:52,780 and now we see two documents in that collection 64 00:03:52,920 --> 00:03:59,150 and you also already see that schemaless thing, our documents don't have exactly the same structure. 65 00:03:59,160 --> 00:04:04,020 Now let's take it one step further and let's insert another document 66 00:04:04,020 --> 00:04:09,790 by the way you can press alt and then left arrow to jump through these words quicker 67 00:04:10,950 --> 00:04:17,840 and let's name this computer, ramp up the price a little bit 68 00:04:23,420 --> 00:04:29,770 and then simply type a high quality computer because I'm really creative when it comes to text and there, 69 00:04:29,810 --> 00:04:36,560 let's add some details and this now will be an embedded document with another pair of curly braces inside 70 00:04:36,560 --> 00:04:37,840 of that other document 71 00:04:38,030 --> 00:04:40,610 and here, we could have something like CPU 72 00:04:40,910 --> 00:04:45,120 and that is let's say the Intel i7, 73 00:04:45,560 --> 00:04:46,270 this one. 74 00:04:46,280 --> 00:04:47,320 No I'm not getting paid, 75 00:04:47,330 --> 00:04:55,070 I don't know if that even exists but I think it does and memory, let's say there we have 32 gigabytes 76 00:04:55,070 --> 00:04:56,120 like this. 77 00:04:56,130 --> 00:04:59,870 And now if I insert this and if find them again, now we see all three 78 00:04:59,870 --> 00:05:02,750 and you also see that embedded document. 79 00:05:02,750 --> 00:05:05,430 So this is just playing around with the shell here, 80 00:05:05,450 --> 00:05:10,720 of course we can also delete and update documents and we can filter when we're retrieving them, 81 00:05:10,730 --> 00:05:15,550 we don't have to always retrieve all of them but that's just our first real quick start. 82 00:05:15,560 --> 00:05:19,650 This is how we will work with mongodb for a big chunk of this course 83 00:05:19,730 --> 00:05:25,670 and now let me show you how this relates to the real world where you would of course write an application 84 00:05:25,760 --> 00:05:31,340 with PHP, with node, with C++ and would interact with the database from there. 85 00:05:31,340 --> 00:05:34,720 So how does that shell stuff you're learning in this course relate to that?