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.

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.

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

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.

To insert documents into collections use the command—

>db.collection.insert(document);

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

>db.collections.update({document});

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

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;

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()

>db.collections.find().pretty();

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”

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.