mongodb-an-introduction

In this tutorial we will see a high level introduction to MongoDB. MongoDB is one of the widely used NoSQL solution in market today. It provides large set of APIs basically functions using which we can easily store or retrieve data to/from MongoDB. It also provides libraries for some of the programming languages using which anybody can interact with mongoDB.

In this tutorial we will:

1. Download and install MongoDB
2. Dump sample data in MongoDB using mongoimport command.
3. Query the imported data and try to retrieve results.
4. Using GUI for MongoDB.

Get a MongoDB:

Get a mongoDB installation file from MongoDB website
Here I am using MongoDB 2.6 for windows for this tutorial.

mongodb-installation-files

After installation you will find mongodb under your Program Files folder on C: drive, (or where-ever you have installed it manually).
Now, go to C:\Program Files\MongoDB 2.6 Standard\bin basically in bin/ folder and start mongod.exe which will start your mongodb instance.

devzone-mongodb-tutorial (7)

At this point you might see an error which will say (as shown in screen-shot):

*********************************************************************
ERROR: dbpath (\data\db\) does not exist.
Create this directory or give existing directory in –dbpath.
See http://dochub.mongodb.org/core/startingandstoppingmongo
*********************************************************************
devzone-mongodb-tutorial (1)

This means you need to create a data repository which is a data folder basically in root folder of your drive. So to resolve this we need to create a “data” folder on c: drive and inside that data folder create a folder named “db” so this will look like “C:\data\db”

After creating C:\data\db folder go back to mongoDB directory and again start mongod.exe this time it will start successfully.
devzone-mongodb-tutorial (2)

This shows your mongoDB server is running now.

Now you can start mongo.exe which is basically your MongoDB Shell.

devzone-mongodb-tutorial (3)

Some basic commands on this shell:

– this will show the current verion of mongoDB

- this will enlist all the databases in mongoDB.

REMEMBER: A. Tables (as in RDBMS) in mongoDB are called as Collections.
B. while creating a database or collection in mongodb, mongoDB will check wheather the given database or collection is already exists in monsgodb if exists it will dump data in respective database or collection and if the given database or table doesn’t exists within mongoDB then it will create it. It will not show any error as RDBMS.

Creating database:
Syntax: use ;
Ex:

Above command will create a database named myDatabase if doens’t already exists. Though you might not be able to see it under SHOW DATABASES; command till some collection is created.

Prepare a Sample Dataset:

Click here to Download the JSON test dataset. Extract the zip file. You will be having dataset.json file in the extracted folder.

Create a folder on C: drive called “test” and put the dataset.json in this test folder.

Importing data in MongoDB:

devzone-mongodb-tutorial (5)

MongoDB comes with mongoimport and mongoexport to do import and export respectively. These commands should be triggered on command prompt.

General format for mongoimport and mongoexport is:

Now, open command prompt and point it to your bin folder, and run follwing command:

C:\Program Files\MongoDB 2.6 Standard\bin>mongoimport –db myDatabase –collection contacts2 –file C:\test\dataset.json
Replace C:\test\dataset.json with your actual path of dataset.json we just downloaded.

devzone-mongodb-tutorial (4)

On successfully import you will see a message something like “imported 25359 objects”

devzone-mongodb-tutorial (6)

Now go to mongoDB shell and trigger show databases; You will see the new database we just created.
now trigger USE myDatabase;
then

You will see the collection imported there.

Exporting data from MongoDB:

General format:

Using GUI for MongoDB:

For ease we will use Robomongo, a free GUI for MongoDB. Download it from Robomongo website
Download and install it. Configure it for your mongoDB server.

Performing some Basic Queries on MongoDB:

1) Select all contents:

Syntax:

Example:

2) Select with condition:

Example:

Insert command:

Syntax:

Example:

Update Command:

Syntax:

Example:

Delete:

Now you can play around mongoDB.

Here are some good Reference links:

MongoDB Official Docs
MongoDB Official Docs