1 00:00:02,070 --> 00:00:07,100 So we have our dummy connection here because we're not really using it usefully here, 2 00:00:07,290 --> 00:00:11,580 now I want to use that connection and actually do something useful with it, 3 00:00:11,670 --> 00:00:16,410 I want to be able to store products in the database for now. 4 00:00:16,410 --> 00:00:24,920 So what we can do is we can cut that code from the app.js file for now and go to products.js and 5 00:00:24,920 --> 00:00:25,690 here in products.js 6 00:00:25,700 --> 00:00:31,280 if we scroll down, you should find a post route which is the route that handles the creation of a 7 00:00:31,280 --> 00:00:32,480 new product. 8 00:00:32,540 --> 00:00:41,060 Here I got some code that extracts all the product data from the incoming request and then right now, 9 00:00:41,090 --> 00:00:44,520 I don't do anything with it except for returning a response. 10 00:00:44,540 --> 00:00:48,520 Now here I want to take that new product data and store it to the database 11 00:00:48,620 --> 00:00:55,170 and as you can see I also made an annotation that I want to take that data and store the price as a 12 00:00:55,370 --> 00:01:02,340 128 bit decimal, so that we can practice that transformation of the type 2. 13 00:01:02,780 --> 00:01:04,840 So what I'll do here is 14 00:01:05,000 --> 00:01:08,830 I'll paste in my mongodb connection code because I'll need a connection 15 00:01:08,930 --> 00:01:14,350 and if we just establish a connection here, this connection will by default not be reusable, 16 00:01:14,360 --> 00:01:19,490 so we always have to connect from scratch if we want to do something with the database, something I'll 17 00:01:19,490 --> 00:01:20,670 change soon too 18 00:01:20,720 --> 00:01:22,550 but for now, let's do it like this. 19 00:01:22,580 --> 00:01:24,630 So here I'll then connect the database 20 00:01:24,650 --> 00:01:31,580 and once the connection is established before I close it, I can do something with it except for logging 21 00:01:31,580 --> 00:01:34,260 some useless message. Here 22 00:01:34,430 --> 00:01:39,060 I can use my client and call db to get access to the database, 23 00:01:39,160 --> 00:01:46,150 the database I have access to is this database by the way, the shop database and then call collection here 24 00:01:46,200 --> 00:01:46,490 right, 25 00:01:46,490 --> 00:01:50,270 we have collection as a method. 26 00:01:50,340 --> 00:01:58,700 Now mongodb by the way won't work here because one thing is I also need to grab that import and copy 27 00:01:58,700 --> 00:02:03,320 that over into products because whenever you are using some feature in a file, you need to make sure 28 00:02:03,320 --> 00:02:06,990 that feature like the client in this case is available in that file. 29 00:02:07,010 --> 00:02:13,390 Now I'll also change it slightly and import mongodb like this and then create a new constant here, 30 00:02:13,570 --> 00:02:17,070 mongo client and basically use mongodb 31 00:02:17,120 --> 00:02:19,860 mongo client like this and you'll see why in a second. 32 00:02:20,030 --> 00:02:24,390 So now mongo client is the constant which I use for connecting, 33 00:02:24,530 --> 00:02:30,330 so then there, we use mongo client connect and now we get that client with the collection method 34 00:02:30,560 --> 00:02:33,840 and then we can access a collection and I'll use products. 35 00:02:34,010 --> 00:02:35,990 Now this collection doesn't exist, 36 00:02:35,990 --> 00:02:42,020 if we wanted to create it ahead of time because we want to add schema validation or anything like that, 37 00:02:42,410 --> 00:02:45,920 we would create it from inside the shell. 38 00:02:45,920 --> 00:02:51,510 So in the shell, we can of course also connect to our Atlas cluster, there 39 00:02:51,530 --> 00:02:58,800 if I click on connect and we click on connect with the shell, the latest shell, you can copy this command 40 00:02:59,180 --> 00:03:02,800 and here you want to use your admin user, 41 00:03:02,840 --> 00:03:05,210 in my case that is Max, 42 00:03:05,210 --> 00:03:11,030 Max is an admin, so you then can connect to your cluster on that username 43 00:03:11,060 --> 00:03:14,800 who is the admin, you need the password 44 00:03:14,900 --> 00:03:17,380 and since I forgot it, I will regenerate it 45 00:03:20,810 --> 00:03:26,010 and then you can create collections and so on from inside that shell. 46 00:03:26,090 --> 00:03:31,760 So inside of that shell, you want to interact with your shop database and then for example create a 47 00:03:31,760 --> 00:03:34,340 collection with whatever setting you have. 48 00:03:34,340 --> 00:03:39,530 Now here I don't want any special settings so I can use the on the fly approach and here, I will simply 49 00:03:39,530 --> 00:03:44,480 use a crud operation to insert something into the collection and the collection and the database will 50 00:03:44,480 --> 00:03:46,170 then get created. 51 00:03:46,190 --> 00:03:48,780 Now how can I insert something here? 52 00:03:49,070 --> 00:03:55,120 Well with the same methods I showed you in the shell, for example here we also have insert many and insert 53 00:03:55,180 --> 00:03:55,650 one, 54 00:03:55,670 --> 00:03:57,630 so this works in exactly the same way 55 00:03:57,770 --> 00:04:00,820 and that will be the case for all the drivers of all the languages. 56 00:04:00,950 --> 00:04:03,380 The syntax of course differs, in PHP, 57 00:04:03,380 --> 00:04:10,190 you obviously use PHP syntax for accessing methods and so on but the general methods you have available, 58 00:04:10,220 --> 00:04:14,130 the operations you can do are the same and they are equivalent, 59 00:04:14,200 --> 00:04:19,430 also in the way of how you use them and how you configure them to the operations I taught you in this course 60 00:04:19,530 --> 00:04:23,470 in the shell which of course was the idea of this course. 61 00:04:24,020 --> 00:04:30,560 So we can use insert one here and I want to insert my new product, new product refers to this object, 62 00:04:30,560 --> 00:04:32,820 this javascript object which is created here 63 00:04:33,050 --> 00:04:35,140 and these are my keys here 64 00:04:35,330 --> 00:04:36,700 and these are my fields, 65 00:04:36,770 --> 00:04:42,570 this already looks a lot like the json document format we used throughout this course. 66 00:04:42,770 --> 00:04:43,910 Now important here, 67 00:04:44,150 --> 00:04:48,860 this should also be converted as or stored as a 128 bit decimal, 68 00:04:48,860 --> 00:04:51,340 so let's see how that works in the next lecture.