mongodb service is not running or mongodb closes automatically

just go to bin folder where your mongod/mongos exe files exists…

now run cmd..

mongod –repair

Thats it


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


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 windows and type “services.msc” where you will find a wrench gear up 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 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.

What is mongodb? Basics of mongodb

MongoDB is a cross-platform, document oriented database that provides, high performance, high availability, and easy scalability. MongoDB works on concept of collection and document.


Database is a physical container for collections. Each database gets its own set of files on the file system. A single MongoDB server typically has multiple databases.


Collection is a group of MongoDB documents. It is the equivalent of an RDBMS table. A collection exists within a single database. Collections do not enforce a schema. Documents within a collection can have different fields. Typically, all documents in a collection are of similar or related purpose.


A document is a set of key-value pairs. Documents have dynamic schema. Dynamic schema means that documents in the same collection do not need to have the same set of fields or structure, and common fields in a collection’s documents may hold different types of data.

Below given table shows the relationship of RDBMS terminology with MongoDB

Database Database
Table Collection
Tuple/Row Document
column Field
Table Join Embedded Documents
Primary Key Primary Key (Default key _id provided by mongodb itself)
Database Server and Client
Mysqld/Oracle mongod
mysql/sqlplus mongo


Sample document

Below given example shows the document structure of a blog site which is simply a comma separated key value pair.

   title:'MongoDB Overview', 
   description:'MongoDB is no sql database',by:'tutorials point',
         message:'My first comment',
         message:'My second comments',

_id is a 12 bytes hexadecimal number which assures the uniqueness of every document. You can provide _id while inserting the document. If you didn’t provide then MongoDB provide a unique id for every document. These 12 bytes first 4 bytes for the current timestamp, next 3 bytes for machine id, next 2 bytes for process id of mongodb server and remaining 3 bytes are simple incremental value.

Any relational database has a typical schema design that shows number of tables and the relationship between these tables. While in MongoDB there is no concept of relationship

Advantages of MongoDB over RDBMS

  • Schema less : MongoDB is document database in which one collection holds different different documents. Number of fields, content and size of the document can be differ from one document to another.
  • Structure of a single object is clear
  • No complex joins
  • Deep query-ability. MongoDB supports dynamic queries on documents using a document-based query language that’s nearly as powerful as SQL
  • Tuning
  • Ease of scale-out: MongoDB is easy to scale
  • Conversion / mapping of application objects to database objects not needed
  • Uses internal memory for storing the (windowed) working set, enabling faster access of data

Why should use MongoDB

  • Document Oriented Storage : Data is stored in the form of JSON style documents
  • Index on any attribute
  • Replication & High Availability
  • Auto-Sharding
  • Rich Queries
  • Fast In-Place Updates
  • Professional Support By MongoDB

Where should use MongoDB?

  • Big Data
  • Content Management and Delivery
  • Mobile and Social Infrastructure
  • User Data Management
  • Data Hub


 External References: