Introduction to mongo db

26
Introductio n To Mongodb

Transcript of Introduction to mongo db

Introduction To Mongodb

AgendaWhat is No-SQL?Why choose No-SQL?MongoDBInstallation GuideConfiguration GuideCommon Database CommandsCRUD in MongoDB

Hello!I am Vijay Shukla

You can find me at:[email protected]

1.What is no

Sql?

Not Only SQL Database.

A form of Data Base Managment System which is non-relational.

Systems are often schema less, avoid joins and easy to scale.

Term NoSQL was coined in 1998, by Carlo Strozzi and again in early 2009 with no:sql east confrence.

The better term would have been “No REL”, but NoSQL caught on.

Redis, MongoDB, CouchDB, are type of NoSQL

Why choose nosql

Amount of data stored is on the up and upEasy DistributionKey-Value (k-V) storesDocument storesNon-relational and schema-less data modelLow latency and high performanceHigh scalability

Reference:- https://www.devbridge.com/articles/benefits-of-nosql/

MongoDB

MongoDB is an open source document database that provides high performance, high availability and automatic scaling.

A record in MongoDB is a document, which is a data structure composed of fields and value pairs.

MongoDB objects are very similar to JSON Objects.{Name:”vijay”,Age:23,}

Installation Guide

1. Import public key used by package management systema. sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80

--recv EA312927

2. Create a list file for MongoDBa. echo "deb http://repo.mongodb.org/apt/ubuntu

precise/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list

3. Reload local package databasea. sudo apt-get update

4. Install MongoDB packagesa. sudo apt-get install -y mongodb-org

5. Install a specific releasea. sudo apt-get install -y mongodb-org=3.2.4 mongodb-org-

server=3.2.4 mongodb-org-shell=3.2.4 mongodb-org-mongos=3.2.4 mongodb-org-tools=3.2.4

Installation Guide

Starting MongoDB ServiceMongod

Once we have issue this command, MongoDB will start up and you should see: waiting for connections on port 27017

Connecting to Mongodb Servicemongo

However, after all the steps above and mongodb installed in our machine, now if you try to start the mongodb service sudo service mongod start it will fail.

"start: Unable to connect to Upstart: Failed to connect to socket /com/ubuntu/upstart: Connection refused”

Failed to start mongod.service: Unit mongod.service failed to load: No such file or directory.

You probably will be able to manually start it with sudo mongod -f /etc/mongod.conf - but in this way MongoDB will remain connected as long as that terminal is opened.

Configuration Guide

1. First we are going to change MongoDB’s default data store files from /var/lib/mongodb to/data/db

2. So, first create a folder /data/db in your machine. Run sudo mkdir -p /data/db

3. Now open the main mongo configuration file with sudo gedit /etc/mongod.conf and change the “dbpath” line as below

4. Replace dbpath=/var/lib/mongodb TO dbpath=/data/db and then save the file.

5. Then delete the old default /var/lib/mongodb

6. But the directory you created doesn’t have the correct permissions and ownership right after creation – it needs to be writable by the user who runs the MongoDB process. Hence we must make all the directories/files owned by mongod user

7. Run sudo chown -R mongodb:mongodb /data/db

Configuration Guide

Document Structure

MongoDb Command

Basic Commands

show dbs List All Databases

use db_name Change DB or Create DB

db Show current selected database name

save Create both the database and collection

find Read

update Update

remove Delete

✖ Createdb.collection.insert(<document>)db.collection.save(<document>)db.collection.update( <query>, <update>, { upsert: true })

✖ Readdb.collection.find( <query>, <projection> )db.collection.findOne( <query>, <projection> )

✖ Updatedb.collection.update( <query>, <update>, <options> )

✖ Deletedb.collection.remove( <query>, <justOne> )

// save one user$ db.users.save({ name: 'Chris' });

// save multiple users$ db.users.save([{ name: 'Chris'}, { name: 'Holly' }]);

Create

Read

// show all users$ db.users.find();

// find a specific user$ db.users.find({ name: 'Holly' });

db.users.update({ name: 'Holly' }, { name: 'Holly Lloyd' });

Update

Delete

// remove alldb.users.remove({});

// remove onedb.users.remove({ name: 'Holly' });

To Be Continue...

Theory of noSQL: CAPWhat is Sharding?ACID - BASEHow to store large file (>16 MB)Grails Project with MONGODB

What Next

Thanks!Any questions?