Thursday, September 18, 2014

mongoDB Basics

Hi,

From last few days i started on mongoDB. It looks interesting as i never work on noSQL type database. Before that i worked on SQL, MySQL and Oracle Database.

mongoDB is the different one from the above DBs. Here, i like to share with you a quick intro on mongoDB.

mongoDB: is a cross-platform document-oriented database.
Classified as a NoSQL database, MongoDB eschews the traditional table-based relational database structure in favor of JSON-like documents with dynamic schemas (MongoDB calls the format BSON)
making the integration of data in certain types of applications easier and faster.

I use the mongoDB in one of my project... I installed this on my machine Whose OS is Ubuntu 14.04 version.

Installation
# apt-get install mongodb
It will install the client and server and other library associated to it.

Open the port in mongod.conf,
# sudo vi /etc/mongodb.conf
Just uncomment the below line in conf file
port = 27017

Start of mongoDB
# sudo service mongodb start

Its a pretty simple to setup on Ubuntu... :)

mongo is a part of the standard mongoDB distribution and provides a full JavaScript environment with complete access to the JavaScript language and all standard functions as well as a full database interface for mongoDB.

Now Start the mongo
# mongo
It will take you on mongo prompt....

Some basic commands for mongoDB

> db  // show current db

> show dbs //show all db with their size

> use db-name  //shitch to your selected db

> help  //show help for the mongodb

> show collections //show the collections used in your selected db

> db.collection-name.find()  // give you all records in your collection

> db.collection-name.insert({name:'Tarun', message:'Welcome'})  //insert record into the collection

> db.collection-name.find({name:'Tarun'})  //give the particular record from your collection
Here, important point is, this is case-sensitive.
if your search as
> db.collection-name.find({name:'tarun'})  // it will not show records whose name as Tarun  

> db.collection-name.findOne()  //to get the one document from the collection
 
> db.collection-name.find().limit(3)  // to get 3 document from all document


2 comments :

Vik2696 said...

Hello sir,

There should be sudo vi /etc/mongodb.conf in place of sudo vi /etc/mongod.conf
and sudo service mongodb start in place of sudo service mongod start

Tarun Kumar Singhal said...

Thanks Vik to correct me.