Installation of mongodb in windows

Lets wrench the installation of mongodb today.

Note: Here in this tutorial we are using in different way like how mysql stores all projects in bin of mysql. We are doing the same by creating a common workspace where all the databases store in data folder of mongodb folder. You can maintain a separate db space in your project folder by following this previous tutorial

First download zip file(preferably) or exe file from mongodb

Once after install or extract of mongodb you will find the following files like shown below

29-05-2014 12-22-59

Now copy those files and create a directory in c drive and make sure the path should look like

c:/mongodb

Here paste the files. Before we startup some thing lets create a couple of directories name “data” and “log” in mongodb fodler. So finally it should look like

29-05-2014 12-25-43

Inside of data folder again create a folder name “db” //all database data will store here

Now install git in your machine in replacement of command prompt.

Hope the installation is complete now…lets step forward

goto c:/mongodb/ folder. right click on bin folder and click “Git Bash” as shown below

29-05-2014 12-34-19

Hope you can see gitbash prompt which resembles like cmd

Now we should define default directory,db path and log manually with commands as shown below

Note: ‘/’ or ‘\’ is very important here in command prompts coz unix style use ‘\’ which works in most cases.

29-05-2014 12-40-58

From the above image you can see the command we should type and hit enter to set default directory,log path, log appends(instead of overwriting in mongodb.log everytime) and to use rest services.

Now you can see log name “mongodb.log” as shown below

29-05-2014 12-43-52

Now go back to mongodb folder and right click bin folder again and click gitbash

Type mongo and hit enter. you can see its connected

29-05-2014 12-46-37

Thats it now you can check dbs by show dbs command and create collections and documents as you wish.

Now we need install mongodb as a window service else you will be figured out with an error showing connection failed.

For that you just need to append –install command to the above command where the command finally will be

> mongod –directoryperdb –dbpath c:/mongodb/data/db  –logpath c:/mongodb/log/mongodb.log –logappend –rest –install

Hit enter, with that it will create a service for us.

29-05-2014 12-55-18

Just type “net start mongoDB

29-05-2014 12-58-10

To verify whether mongodb started or not..click windows and type “services.msc” where you will find a wrench gear up icon..click on it and check whether mongodb is started or not.

With this we completed successfully installing and starting mongodb. Lets goto c:/mongodb/bin gitbash prompt and type “mongo”.

Show databases:

Now you can see databases by showdbs command

Create/switch to database:

create db by use database_name command (eg: use sathya will create a database and switches to that database).

Show collections:

show collections – will show the list of collections(tables in oracle)

db.createCollection(“collectionname”) – will create a collection in your database

Lets take an example to insert data in collections..Now i will create a table called employee and stores(create/update/delete) some records in it.

Create collection:

Eg: db.createCollection(“employee”);

the above command creates a employee collection(table) in db. Now lets insert some stuff in it.

Insert records:

db.employee.insert({name:”sathya”,dept:”computers”,technology:”html5″}) – hit enter

To check the record inserted or not..use db.employee.find() – this results an unorder/unformatted content. Lets put up some make up for this.. 😉

Find records:

use command – db.employee.find().pretty()

Find Only one field:

If you wish to find only one employee with particular name..

just type db.employee.findOne({name:”sathya”}) – this results only one employee with name sathya

Update command:

db.employee.update({oldname},{new name},{ upsert})

Eg: db.employee.update({name:”sathya”}.{name:”SathyaManoj”}.{upsert:true})

You can even modify different fields/columns by db.employee.update({name:”sathya”,dept:”computers”}.{name:”SathyaManoj”,dept:”IT”}.{upsert:true})

This will update the name sathya with SathyaManoj. And what this upsert does is..it will check for the name sathya to update, if it doesnt find it..then it will create a new one.

Now check it by using db.employee.find().pretty()

Delete command:

db.employee.remove({name:”sathya”}) – will remove the record from the collection.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s