MEAN Tutorial (Part 2) : Set up MongoDB

July 10, 2015
by Inaam Hussain

MongoDB is a scalable, high-performance, open source, document-oriented database.
Classified as a NoSQL database.
No fixed table schema, or rather no need to predefined columns, and structure.
In MongoDB, data stored in Collections ,which are like tables.
Items are called “Documents” not “Rows”.
Two documents in the same collection may have different forms.
So, making the integration of data in certain types of applications easier and faster.
 

DEMO DOWNLOAD

Installation

Following 2 things you need to installed before proceed further,

  1. MongoDB : https://www.mongodb.org/downloads : For downloading specific version for your system, First check System architecture by typing following command in CMD(Command prompt)(i.e: wmic os get osarchitecture). and then based on that download MongoDB.
  2. RoboMongo : http://robomongo.org/download.html : (Shell-centric cross-platform MongoDB management tool). Using this software we can easily manage data and collection of data.

After installing above 2 tools, Now will set up and then connect to db.
 

Setup & Run

First will Set up MongoDB and then run,

  1. Go to the path where mongodb installed (i.e: C:\Program Files\MongoDB\Server\3.0\bin) for windows. and set that path to Environment variable which you can find in settings.
    So that you can access MongoDB from any location in command prompt.
  2. And create one folder anywhere in your system in which will store your data. lets create folder mongodata in C drive.
    Now will connect mongodb to that folder by using following command,

    • mongod.exe --dbpath "c:\mongodata"(If you have not set environment variable then go to bin folder of mongodb (i.e: C:\Program Files\MongoDB\Server\3.0\bin ) and type this command) or
    • mongod --dbpath "c:\mongodata" (If you have set environment variable type this command)

And now you can see that your db is connected to port 27017.
Above setup process is for windows. and if u have another operating system like OS X or Linux. Please visit following links.

  1. Linux : Here
  2. OS X : Here

Create database for bookstore

In above all steps we have seen how to setup mongodb in your system. and now will create database for our book store app.
first will start mongodb. see above setup and connection part and that will run on port 27017.
and then open another command prompt and go to path where mongodb is installed. for windows path is in C:\Program Files\MongoDB\Server\3.0\bin location.

Command : cd C:\Program Files\MongoDB\Server\3.0\bin

Now will access mongodb by typing following command,

Command : mongo

And now will create database name books by using following command, use command is used for create or switch databases.

Command : use books

by using above command we have successfully created database for our book store app. and now will use this database in API’s section. in which server.js file we have done connection to database ( i.e: var db = mongo.db("mongodb://localhost:27017/books", {native_parser:true}) ) while creating API’s – See Here full tutorial.
And then finally will run app – See Here full tutorial.
 

About

Engineer. Blogger. I like to be updated on new upcoming emerging technologies.

Free PDF

Subscribe and get AngularJS Tips PDF. We never spam!
First Name:
Email:

8 comments

  1. |

    Hi , Can you tell me how to do showing on list on mongo and type “show dbs” the “books” is not on the list. How can connect it or need to be update? please help me how to do it. thanks.

    • |

      Hi arduino,
      Please check “Create database for bookstore” section above in which i have mentioned that how to do it for windows.
      and if you have to access “book” database then at least one object of data should be present in that database otherwise it won’t show.
      http://docs.mongodb.org/manual/reference/command/ : list of command for mongodb.
      Thanks.

    • |

      Hi Agnese,
      If you need to add angular app to node app then, in express we need to set base directory to that folder which contains angular app code. lets say our angular code is in ‘client’ folder. so in node js file you have to specify following line of code,
      app.use(‘/’,express.static(__dirname+’/client’));
      above code will automatically initialize at the start.
      Thanks.

      • |

        If I download this project from your github repository. How can I merge these two apps? I can not figure out :/

        • |

          Hi,
          In node app u have to specify the directory of angular app. lets say our angular app in client folder. then you have to add “app.use(‘/’,express.static(__dirname+’/client’));” in your node app code.that will run your base code of angular.
          Thanks.

  2. Andrew
    |

    Hi Inaam,

    Thankyou for the article, this is the best article I have come across which help me to understand angular in no time, I have downloaded your code and want to add some extra features for my personal use,

    I want to add a categories of book so that while adding a new book I can sort the book into specific category. for e.g the categories can be – Story books, magazines, programming books, now while adding a book I can choose the category and create a separate view where the categories will be listed and after clicking on the category all the books under that category will be shown.

    • |

      Hi Andrew,
      Thanks for the comment.
      Yes you can add categories, you have to add dropdown on add and edit book page. and based on that data you have to add in mongodb database. and while querying you can fetch data based on categories.
      Thanks.

Leave a Comment