Node JS – Part 6

Hi Friends,

Now, let’s start this module by installing Mongo DB. Mongo DB is an open source document db, so, we need to install the same from the site Now, as shown below in the screen shot, in the downloads section you will get different options for downloading mongoDB based on your OS. For my machine, I have installed Windows 64 bit option.

Now, once i have downloaded the file, then i’ll go ahead and install the same as shown below in the screen shot.

Now, once we have installed mongo db, we can go ahead and use the same in our project. Below, in the screen shot you can see that same has been created in my program files directory.

Now, what i’ll do from here, i’ll just copy the bin files and put the same in our project in a different directory, so that i can use mongo db in our project as shown below.

Then, i will go ahead and open the command prompt with the above new folder and create one new folder say database just to store the database for my project. Then, i’ll type the command “mongod -dbpath ./database“. Now, what this command will do, it will simply store all my data in that repository. Once i typed this command, it will open the port which i can utilize for my application. Now, the default port is 27017 as shown below in the screen shot. So, once port started then i can simply go ahead launch the port in browser to simply test the same.

Now, also for diagnosis purpose or for all admin related query, you can also enable one more that is 28017. But, for doing the same what you need to do, you need to enable rest endpoints as shown below in the screen shot and then when you launch the same in browser, it will open the console.

Now, since the mongodb is installed, let’s see how to access the same via JS. So, the 1st dependency we have here is we need to have driver for accessing the same. so, inorder to do the same we need to install mongodb via npm, so that it gets installed in our “package.json” dependency folder. So, once that is installed, we can verify the same in references and package.json as shown below in the screen shot.

Now, once this done, next thing i would like to access the server directly. Now, for doing the same i will create one new file inside the data folder say “MovieReviewDb”.

Now, the next thing is i need function to write the same in database. For, that i need to modify index.js file under dataRep folder as shown below in the snippet. Now, with this change in place when i run the solution, then i’ll get the below output

Now, this section is getting bigger. Hence, I’m wrapping this session here. In the next section, we’ll see how to query the data and take the request to write the data from UI. Till then stay tuned and happy coding.

Rahul Sahay

1,048 total views, no views today