Subscribe via RSS Feed

How to Get Started with MongoDB?

July 5, 2014 1 Comment

In the previous article we have learned about mongodb as well as we have learnt how to install them in different OS. In this article we will learn how to run in different OS and many other things.

How to get started with mongodb?

To begin with mongodb we need to connect to a server at first. I have Ubuntu . So I’ll be using mongo shell for working in mongodb. So, now the question is what is mongo shell?

What is mongo shell?

As per the mongo shell is an interactive JavaScript shell for MongoDB, and is part of all MongoDB distributions. So, in a straight line we can say that mongo shell is an interface which is used to use CRUD on database via mongodb.

Start the mongo Shell

To start the mongo shell and connect to your MongoDB instance running on localhost with default port:
1. Go to your :


2. Type ./bin/mongo to start mongo:
If you have added the /bin to the PATH environment variable, you can just type mongo instead of ./bin/mongo.

3. To display the database you are using, type db:


The operation should return test, which is the default database. To switch databases, issue the use helper, as in the following example:


Now we have started mongo shell and already using the database we required.

Next let us go through some of the features that mongodb has. As an example let us first insert some data. To do that normally we have to create a database and go through step by step. But MongoDB can create databases on the fly. It is not required to create a database before you start working with it. .A number of databases can be run on a single MongoDB server. Default database of MongoDB is ‘db’, which is stored within data folder.

After having a basic idea of mongodb let us see where does the difference lies between mongodb and rdbms. With this difference I think you can grasp the concept more easily.


Document is the unit of storing data in a MongoDB database. Document use JSON (JavaScript Object Notation, is a lightweight format used to interchange data between various applications) style for storing data. Documents are analogous to the records of a RDBMS. Insert, update and delete operations can be performed on a collection.

Table Collection
Column Key
Value Value
Records / Rows Document / Object

The following table shows the various datatypes which may be used in MongoDB.

Data Types Description
string May be an empty string or a combination of characters.
integer Digits.
boolean Logical values True or False.
double A type of floating point number.
null Not zero, not empty.
array A list of values.
object An entity which can be used in programming. May be a value, variable, function, or data structure.
timestamp A 64 bit value referring to a time and unique on a single “mongod” instance. The first 32 bit of this value refers to seconds since the UTC January 1, 1970. And last 32 bits refer to the incrementing ordinal for operations within a given second.
Internationalized Strings UTF-8 for strings.
Object IDs Every MongoDB object or document must have an Object ID which is unique.


A collection may store number of documents. A collection is analogous to a table of a RDBMS.
A collection may store documents those who are not same in structure. This is possible because MongoDB is a Schema-free database. In a relational database like MySQL, a schema defines the organization / structure of data in database. MongoDB does not require such a set of formula defining structure of data. So, it is quite possible to store documents of varying structures in a collection. A collection is created, when the first document is inserted.

Collection names must begin with letters or an underscore. A Collection name may contain numbers.Using a “.” (dot) notation, collections can be organized in named groups. For example, tutorials.php and tutorials.javascript both belong to tutorials. This mechanism is called as collection namespace which is for user primarily. Databases don’t have much to do with it.
Following is how to use it programmatically :


Creating a Database/Inserting Records
Initially, no database is created. Copy the content below and paste it in mongo shell. And see what happens?

first: 'matthew',
last: 'setter',
dob: '21/04/1978',
gender: 'm',
hair_colour: 'brown',
occupation: 'developer',
nationality: 'australian'
first: 'james',
last: 'caan',
dob: '26/03/1940',
gender: 'm',
hair_colour: 'brown',
occupation: 'actor',
nationality: 'american'
first: 'arnold',
last: 'schwarzenegger',
dob: '03/06/1925',
gender: 'm',
hair_colour: 'brown',
occupation: 'actor',
nationality: 'american'

Confirm that the database and accompanying records have been created, type in the following command:


After executing this command you’ll get an output showing the data has inserted into a test db. Now insert as much data you want.
So, we are successful in inserting data. Now we have search a particular data from our db. Let us see how to do it.

Searching For Records

Say that we want to find all actors that are male. To accomplish that, you’ll need to run the following command:

db.nettuts.find({gender: 'm'});

Here we have specified that gender must be equal ‘m’. now if everything you did was correct then you will get all the names whose gender is ‘m’.

Deleting Records

If you want to delete a record, you have to pass in a set of selectors, as you also would with SQL, to determine the set of records to delete. If you don’t do this, you will delete all records and the database.
So, let’s say that we don’t want James Caan in our list of actors. Let’s remove him from the database using the following command:

db.nettuts.remove({first: 'james', last: 'caan'});

No visible output is provided to indicate whether we were successful or not , so let’s do a search to double check.

db.nettuts.find({first: 'james', last: 'caan'});

After this, you should see no results returned. If that’s what you’ve found, then we’ve successfully deleted James Caan from our database. But what if we want to delete all the records from the database?
Well, to do that, just remove the selectors from the previous call to remove, as below.


After running both commands above, we’ll see no output, indicating that the database, with all records have now been removed.
In the above article we just scratched a few features of mongodb to let you know how does it work. But there is so many things that differentiate mongodb from sql, that one or two articles are not enough.
So, from next article we will try to go deeper into the world of mongodb.

Related Links:

1> How to Get Started with MongoDB Database?
2> How to Import and Export Through Mongodb?
3> How to Use Projection in MongoDB?
4> Using sort method in mongodb
5> Map-Reduce in MongoDB
6> Introduction to Replication in MongoDB
7> Deploying a Replica Set in MongoDB
8> Discussing Replication Lag in MongoDB
9> Replica Set Members in Mongodb
10> Working with Sharding in MongoDB
11> Working with Index in MongoDB
12> Working with Aggregation in MongoDB
13> How to Work with Aggregation Framework in MongoDB?
14> Working with Pipeline Concept in MongoDB
15> Discussing about Pipeline Expression in MongoDB

If you find this article helpful, you can connect us in Google+ and Twitter.