Working with MongoDB shell commands

Previously I have shown you the installation procedures of MongoDB on windows or Linux or MAC

Today, in this post we will see how to get working with the shell commands of MongoDB.

Connecting to MongoDB

Connecting to MongoDB

Firstly, you have to connect to the database as shown in the image above.

We will be given a sample database named “test” in the software.

  1. You can create any database by using the command–>

>use manoj;

This will create if the database is not available.

Thie return value of this command will be

Creating or switching databases

Creating/switching databases

  1. To list database available in your server, use the command…

>show dbs

This will list all databases available specifying their size.

In MongoDB, we call tables as COLLECTIONS and each row of data is called TUPLE

The default datatype for storing data into Collections is called document, and that in MySQL it is an array

The format of document is similar to JSON but it is binary instead of ASCII.

for example

In MongoDB:

document = {“title” : “my new cms with MongoDB”,”description” : “MongoPress is the latest scalable cms developed in MongoDB”};

This seems a bit awesome structured rather than look like array in MySQL.

  1. To insert documents into collections use the command—


Inserting data into collections

Inserting data into collections

  1. To update previously stored data…use the command update()


This is similar to the SQL command UPDATE collections SET “title” = “mongodb” WHERE “id” = 12;

  1. To remove the unwanted data or remove based on criteria use the command—-remove()

>db.collections.remove({“criteria”});//criteria is your condition to remove data

THis is similar to the SQL command DELETE FROM collections WHERE id=12;

  1. To find data use the command….find()

>db.collections.find(); //this will display all the data in your collection.

To get a good looking output similar to JSON format use the sub function pretty()


finding data

finding data

This is similar to the SQL command SELECT * FROM collections

To find only one tuple from database based on criterion use the command findOne()

>db.collections.findOne({“title”:”my new tutorial”}).pretty();

This is similar to the SQL command…

                  SELECT * FROM collections WHERE title=”my new tutorial”

  1. To list all the collections in the database use the command….

>show collcetions;

We will see integration of MongoDB with PHP in the next post.

About these ads

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your 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