So we get our application and now we want to integrate mongodb into nodejs. Now to do that, you can
dive into the official docs and there, choose mongodb drivers.
There you'll find instructions on how to install and use the driver for the language, the programming
language your application uses. You
see there are a bunch of languages and for us, nodejs, this one is the relevant driver. So you can click
on that
and we are forwarded to the docs of that specific driver.
Now there, you can simply choose the latest documentation and now learn more on how to use that driver
and you see, you learn how to install the driver and then you have a quick start and some instructions
on how to do crud operations and how to connect and so on
and you should find this for every driver. The documentation might look different but you should find
similar guides for every driver so that you can learn how to use that driver with your language
and that already gives you like most of the knowledge you need.
You also can dive into the API documentation to see a list of all methods or all objects that are part
of this driver,
for example here you also see the constructors for decimal 128 bit objects
in case you want to store such a decimal, if you remember the numbers module of this course because
nodejs is based on javascript and as you as a javascript developer should know, every number in javascript
by default is a 64 bit float,
so if you want to store it as bigger decimal, you will need to convert it or not convert it but create
such an object with that constructor,
the same exists for 32 bit integers and for longs which are 64 bit integers.
So this is also useful if you want to find out what's in there,
how could I possibly get such a decimal,
well the API reference should help you. But we don't need to dive into that right now,
we just need to install the driver
and as you can see with nodejs, this is the command we have to execute in our project folder to install
the official
mongodb driver.
So we can just comment this
and then in here in our project, I'll open the integrated terminal which is integrated into Visual
Studio Code,
you'll find it under terminal here, new terminal
and this is simply the normal command prompt or terminal you have on your operating system, just already
navigated into this project folder which is great of course.
So I'll paste this command in here and this will now install mongodb in this project, so that I
can use it from inside this project.
Now with that installed, we will be able to use it.
Now one important note, it's the nodejs driver and if you remember our documentation,
there was no reactjs driver,
the reason for that is that you never connect to mongodb through the official drivers directly in your
client side applications,
so in your angular, react or vue user interfaces, you never put your database connection code in there
because you will expose your credentials there to the user which you don't want. On node which runs on a
server and is inaccessible by the user,
these credentials are secure and I will show you what I mean real quick. If we have a look at our running
application and you have a look at the developer tools which you can open by right clicking and inspecting
in Chrome, in these developer tools here,
this is the html code but if you click on sources here, you will actually be able to access the entire
javascript source code that makes up the frontend part of this application,
so the react applications code.
You can access it here under source and here we have our react code and if you had the logic to connect
to mongodb in here,
well then every user using your website can see that
and that's not a security issue,
that's just how frontend javascript works.
It's readable by your users,
therefore the connection logic and the entire interaction logic has to live in the nodejs part, so
on the server. Now there is an alternative which we'll learn about in the stitch module which gives you
a different way of working with
mongodb
but there in the end, it'll still be such that no credentials are exposed in the code
and for now, we don't have to worry about that
other alternative, here
this is how it is,
you must not put your mongodb data, your credentials into this frontend code.
So that is why there is no reactjs driver and no angular and vue or in general, frontend javascript
driver
but why we use the nodejs driver. And with all that out of the way, in the next lexture
let's finally connect our backend here to mongodb.