MongoDB Basics

Hi all. Today we are going to see about mongodb basics.

what is it?

Mongodb is a No-Sql Database , document based database (closely dictionary in python , ruby).  Here you can read about it official documentation .

why is it used for?

It’s used when you no need to care about your schema . It means when you are using mysql , we have to have ID field integer, name varchar . Here it’s not needed .

Installation:

open terminal (cntl+alt + T)

sudo apt-key adv –keyserver hkp://keyserver.ubuntu.com:80 –recv 7F0CEB10

sudo apt-get update

sudo apt-get install -y mongodb-org

Hope you have successfully installed mongo in your system.

if you’re having trouble see official site for trouble shooting here

Time to play with it :

type mongo and enter in your shell

you will see something like this

MongoDB shell version: 2.4.9
connecting to: test

>

To see all database:

show dbs 

it lists all databases. This is equal to show databases in MYSQL

To select a particular database

use databasename

To see all collections inside a database.

Note: In mongodb collections refers tables. we won’t call them as tables . It’s collections here.

show collections

this is equals to show tables in MYSQL

Let’s create a database called bala .

use bala

unlike MYSQL it won’t throw you error unknown database.

to check in which db you’re issue db command  in mongo shell.

it shows bala

now create a document in it.

db.mycollection.insert({“name”: “bala”})

what i did here?

db => bala

mycollection => our collection name

insert => operation name

{“name”: “bala”}  => data we are inserting into our collection

To see a document is inserted into our db , type this command

> db.mycollection.find()
{ “_id” : ObjectId(“55a6070cab3e0cf5c31434cf”), “name” : “bala” }
>

see our document is inserted, find returns all documents in a collection(table) .

Have you noticed _id in above output. Yes that’s automatically created by mongo . Its alpha numeric . Similar to id primary key in MYSQL. 

To see one document in a collection

> db.mycollection.findOne()
{ “_id” : ObjectId(“55a6070cab3e0cf5c31434cf”), “name” : “bala” }
>

Now try inserting other document

db.mycollection.insert({“name”: “hari”, “country”: “india”, “state”: “Haryana”, “age”: 20})

what we did ?

we gave country , state, haryana . so many keys .  what happened?

As i said it’s schema less database , it doesn’t care about schema.

now let’s go and check it out.

> db.mycollection.find()
{ “_id” : ObjectId(“55a6070cab3e0cf5c31434cf”), “name” : “bala” }
{ “_id” : ObjectId(“55a60976ab3e0cf5c31434d0”), “name” : “hari”, “country” : “india”, “state” : “Haryana”, “age” : 20 }
>

see it shows two documents in our collection. how cool is this .

To find a document by condition (by name , age or any field)

let’s see i want to see all document’s which have name “hari”

> db.mycollection.find({“name”: “hari”})
{ “_id” : ObjectId(“55a60976ab3e0cf5c31434d0”), “name” : “hari”, “country” : “india”, “state” : “Haryana”, “age” : 20 }
>

here i have only one document having name “hari”, if I ‘ve many then it will show all documents.

We can see particular field value also by condition

let’s say i want to see all names alone in my collection

> db.mycollection.find({}, {“name”:1})
{ “_id” : ObjectId(“55a6070cab3e0cf5c31434cf”), “name” : “bala” }
{ “_id” : ObjectId(“55a60976ab3e0cf5c31434d0”), “name” : “hari” }
>

{} => empty dict to select all documents

“name”:1 => select only name from all documents

we can select  as many documents as possible

fieldname: 1  => selects the field to result.

fieldname:0  => ignores the filed to result.

Here instead of { } , we can give condition also .

Try it out those commands also.

Let’s update a document named “bala” , adding age field into it

> db.mycollection.update({“name”: “bala”}, {$set : {“age”: 20}})

$set is used to update field

let’s see it’s updated or not.

> db.mycollection.find({“name”: “bala”})
{ “_id” : ObjectId(“55a6070cab3e0cf5c31434cf”), “age” : 20, “name” : “bala” }
>

its updated , age field added into it. if we have many documents named “bala” then all got updated by above command.

till now we seen creation,insertion, updation.

Let’s move on to deletion of a document

To delete a document in collection

> db.mycollection.remove({“name”: “bala”})
> db.mycollection.find({“name”: “bala”})
>

first command deleted the document, second one verifies that we don’t have document name “bala”.

To remove all documents in a collection

db.mycollection.remove()

This command removes all document in a collection.

That’s all about mongodb basic (curd operations) .  Try playing with that , we will see with advanced commands or integration with any language (python or ruby) later .

 

Thanks for reading . Happy hacking .