MONGODB BASICS

Hello everyone!! Today I am going to explain MONGODB.

Mongodb is open source, cross platform, documnent oriented database(No-SQL Database).

Mongodb has _id of 12 bytes which assures the uniqueness of every document:
first 4 bytes :for the current timestamp,
next 3 bytes :for machine id,
next 2 bytes :for process id of mongodb server and
remaining 3 bytes :are simple incremental value.

MongoDB built in features :

1. Single machine having multiple database.
2. Mongodb Searches by field.
3. Scalable: we can add machine in a running system.
4. Any field in a document can be indexed.
5. Support master-slave replication :Master can perform read and write.
: Slave can copy master’s data and can only be use for read and backup but not  for write.
6. It runs over multiple machines.
7. Copy data to keep system up in case of failure.
8. Special support for location.
9. Java script can be used in queries.

Installation:
1. Open terminal (ctrl+alt + T)
2. sudo apt-key adv –keyserver hkp://keyserver.ubuntu.com:80 –recv 7F0CEB10
3. sudo apt-get update
4. sudo apt-get install -y mongodb-org

MongoDB commands:

(if database_name = document and if collection_name = doc)

Type mongo and enter in your shell

shubhala@shubhala-HP-15-Notebook-PC:~$ mongo

MongoDB shell version: 3.2.11
connecting to: test
Server has startup warnings:
2016-12-07T09:36:19.618+0530 I CONTROL  [initandlisten]
2016-12-07T09:36:19.618+0530 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is ‘always’.
2016-12-07T09:36:19.618+0530 I CONTROL  [initandlisten] **        We suggest setting it to ‘never’
2016-12-07T09:36:19.618+0530 I CONTROL  [initandlisten]
2016-12-07T09:36:19.618+0530 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is ‘always’.
2016-12-07T09:36:19.618+0530 I CONTROL  [initandlisten] **        We suggest setting it to ‘never’
2016-12-07T09:36:19.618+0530 I CONTROL  [initandlisten]
>
To see all database:

> show dbs

document

mydata

it lists all databases.

To select a particular database

> use database_name

i.e.,

>use document

To see all collections inside a database.

> show collections

doc

mycollection

it lists all collections

To select particular collection

> use collection_name
i.e.,

> use doc

To see all document in a collection

> db.document.find()
{ “_id” : ObjectId(“57cd170a8b94e93cfdbee718”), “name” : “Shubhala”, “age” : 23 }

Now try inserting other document

> db.doc.insert({“name”: “ss”, “age”: 20, “gender”: “female”}, {“name”: “ram”, “age”: 30, “gender”: “male”})

> db.doc.find()
{ “_id” : ObjectId(“57cd170a8b94e93cfdbee718”), “name” : “Shubhala”, “age” : 23 }
{ “_id” : ObjectId(“5848efaaf7a22f0ca9a9df98”), “name” : “ss”, “age” : 20, “gender” : “female” }
{ “_id” : ObjectId(“5848f2f5f7a22f0ca9a9df9d”), “name” : “ram”, “age” : 30, “gender” : “male” }

To see one document in a collection

> db.doc.findOne()
{ “_id” : ObjectId(“57cd170a8b94e93cfdbee718”), “name” : “Shubhala”, “age” : 23 }

>db.doc.find().pretty()  # display results in an easy-to-read format

> db.doc.find({“name”:”ram”})
{ “_id” : ObjectId(“5848f308f7a22f0ca9a9df9f”), “name” : “ram”, “age” : 30, “gender” : “male” }

To update a document
> db.doc.update({“name”:”ram”}, {$set:{“name”:”raman”}})
> db.doc.find()

{ “_id” : ObjectId(“57cd170a8b94e93cfdbee718”), “name” : “Shubhala”, “age” : 23 }
{ “_id” : ObjectId(“5848efaaf7a22f0ca9a9df98”), “name” : “ss”, “age” : 20, “gender” : “female” }
{ “_id” : ObjectId(“5848f2f5f7a22f0ca9a9df9d”), “name” : “raman”, “age” : 30, “gender” : “male” }

> db.doc.count()
3

To delete a particular document
> db.doc.remove({“name”:”raman”})
> db.doc.find()

{ “_id” : ObjectId(“57cd170a8b94e93cfdbee718”), “name” : “Shubhala”, “age” : 23 }
{ “_id” : ObjectId(“5848efaaf7a22f0ca9a9df98”), “name” : “ss”, “age” : 20, “gender” : “female” }

To remove all documents in a collection
> db.doc.remove()
> db.doc.find()
>

To remove collection from  the database
> db.doc.drop()
true

Thanks !!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

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