Saturday, July 26, 2014

Installing and configuring MongoDB in windows


MongoDB is the buzz word now in Information technology industry.

MongoDB was  developed by a company named "10gen".

To download and configure the mongoDB follow the below steps:

Go to the below link
           http://www.mongodb.com/subscription/downloads

Look for
Download the file and install the MongoDB database. Selecting all the tools for production.

The 64 bit or 32 bit operating system should be determined prior to installing mongoDB.

Below command in command prompt will provide the result
wmic os get osarchitecture

Once completed, navigate to the "C:/Program Files/MongoDB 2.6 Enterprise/" 
Check for "bin" folder.

Now its ready for starting the service.
 Open a command prompt, use "Run as administrator" option.
 use the below command
 > mongod
since you haven't set the data file storage path error message will be displayed as in the below snapshot.
Figure: 1
 
The below data folder should be created because its a document-oriented database.

1. We need to create a folder as structured data/db => can be either created inside the above directory structure and start the service again with the below command specifying the path of the data folder (database)
       > mongod --dbpath="C:/mongodb/data"
2. We can also create a folder and use a config file to start the mongodb using the mongod command.
 you can also create folder log/ at the same level of data/ structure.
Create a file named mongodb.config, with the below content.

Figure:2
                now start the mongodb using this config file
            >mongod --config="C:\mongodbdata\mongodb.config"
Refer the above command prompt snapshot (figure 1)

Now we need to create a database and also verify everything is correct.

Open another new cmd, Ensure that the bin directory is set in environment variable path.
use the below command:
      > mongo
this will start the mongodb shell.
Then use the below commands to validate it:
     > show dbs;

the above command will display the database (schema in mySql).

Below command will create a database in mongodb.
   > use sampletbl;
The tables in the relational database can be related to collections in mongoDB.
Use the below command to create a collection (or create a table)
  >  db.sampletbl.insert({firstname:"myname"});

To see the inserted value in the collection use 
   > db.sampletbl.find();

Using the command show dbs; again will display all the database in the mongodb.

just creating a db using use dbname; will not display the created database since mongo db needs at least a single collection within it to display it for the command show dbs;

We are almost done with installing and configuring the mongodb.

No comments:

Post a Comment