So we have our dummy connection here because we're not really using it usefully here,
now I want to use that connection and actually do something useful with it,
I want to be able to store products in the database for now.
So what we can do is we can cut that code from the app.js file for now and go to products.js and
here in products.js
if we scroll down, you should find a post route which is the route that handles the creation of a
new product.
Here I got some code that extracts all the product data from the incoming request and then right now,
I don't do anything with it except for returning a response.
Now here I want to take that new product data and store it to the database
and as you can see I also made an annotation that I want to take that data and store the price as a
128 bit decimal, so that we can practice that transformation of the type 2.
So what I'll do here is
I'll paste in my mongodb connection code because I'll need a connection
and if we just establish a connection here, this connection will by default not be reusable,
so we always have to connect from scratch if we want to do something with the database, something I'll
change soon too
but for now, let's do it like this.
So here I'll then connect the database
and once the connection is established before I close it, I can do something with it except for logging
some useless message. Here
I can use my client and call db to get access to the database,
the database I have access to is this database by the way, the shop database and then call collection here
right,
we have collection as a method.
Now mongodb by the way won't work here because one thing is I also need to grab that import and copy
that over into products because whenever you are using some feature in a file, you need to make sure
that feature like the client in this case is available in that file.
Now I'll also change it slightly and import mongodb like this and then create a new constant here,
mongo client and basically use mongodb
mongo client like this and you'll see why in a second.
So now mongo client is the constant which I use for connecting,
so then there, we use mongo client connect and now we get that client with the collection method
and then we can access a collection and I'll use products.
Now this collection doesn't exist,
if we wanted to create it ahead of time because we want to add schema validation or anything like that,
we would create it from inside the shell.
So in the shell, we can of course also connect to our Atlas cluster, there
if I click on connect and we click on connect with the shell, the latest shell, you can copy this command
and here you want to use your admin user,
in my case that is Max,
Max is an admin, so you then can connect to your cluster on that username
who is the admin, you need the password
and since I forgot it, I will regenerate it
and then you can create collections and so on from inside that shell.
So inside of that shell, you want to interact with your shop database and then for example create a
collection with whatever setting you have.
Now here I don't want any special settings so I can use the on the fly approach and here, I will simply
use a crud operation to insert something into the collection and the collection and the database will
then get created.
Now how can I insert something here?
Well with the same methods I showed you in the shell, for example here we also have insert many and insert
one,
so this works in exactly the same way
and that will be the case for all the drivers of all the languages.
The syntax of course differs, in PHP,
you obviously use PHP syntax for accessing methods and so on but the general methods you have available,
the operations you can do are the same and they are equivalent,
also in the way of how you use them and how you configure them to the operations I taught you in this course
in the shell which of course was the idea of this course.
So we can use insert one here and I want to insert my new product, new product refers to this object,
this javascript object which is created here
and these are my keys here
and these are my fields,
this already looks a lot like the json document format we used throughout this course.
Now important here,
this should also be converted as or stored as a 128 bit decimal,
so let's see how that works in the next lecture.