Quantcast
Viewing latest article 16
Browse Latest Browse All 28

MongoDB - A quick tutorial

MongoDB is a scalable, high-performance, open source, schema-free, document-oriented database and is one of the interesting non-relational databases available today.It is written in C++ and supported by 10gen.
MongoDB and SQL, a comparison.

MongoDB                     RDBMS
database                     database
table                      collection
index                     index
row                     document
column                      document field
primary key                         _id
join                     embedding or linking

Setting up MongoDB 
1. Download the MongoDB from the following location (binary)
    MongoDB Downloads
2. Unzip the  binary to any location
3. Create a folder C:\data\db - this is the default location mongoDB stores data. And this won't be created automatically. If you would like to use a different location provide the new location using the -dbpath to the mongod.exe command (eg : mongod -dbpath ../ )
4. Start the mongodb using the "mongod.exe" command
eg: C:\mongodb2.0.2\bin\mongod


After the server is started :-
1. MongoDB comes with an interactive shell - mongo (similiar to sqlplus). This is a java script based shell and takes java scripts as input. There are many built in shell functions available.
eg: C:\Tools\mongodb2.0.2\bin\mongo
or  TRY the free shell here
2. Admin UI's are also available to browse the MongoDB


Notes :
  • No need to create a database explicitly. Following command on the shell will automatically create the database eg; for creating the db "mytest"   >  use mytest
  • No need to create a collection explicitly. First use of collection name will create a collection.
  • Documents in the same collection can have different structures (schemaless!)
  • Input is JSON-style documents, internally stores in BSON format (Binary JSON)
  • use "_id" as the field name to have the user defined key as the primary key of the document.
  • If "_id" is not set, MongoDB add a "_id" field to the document with an auto generated value. eg:  _id : ObjectId( "47cc67093475061e3d95369d" )
  • Format of  "_id" value is a 4-byte timestamp + 3- byte machine id( hostname) + 2-byte process id + 3 byte counter (an ever incrementing value from a counter or a random value, when the counter is not available)
  • All the writes are fire and forget.
  • No joins, no transactions.
  • Autosharding is the cool feature of MongoDB which is the backbone of easy scale out.
  • MongoDB has pretty good querying capabilities.
Hands on :-
1. Open a mongo shell using
 eg : C:\Tools\mongodb2.0.2\bin\mongo
type the below command on the shell to create and use the "practice" db
 > use practice
Sample output from mongo shell
C:\Tools\mongodb2.0.2\bin>mongo
MongoDB shell version: 2.0.2
connecting to: test
> use practice
switched to db practice


2. Inserting  data into a collection. Type in the below in the shell.. note "employee" is the collection name and will be created automatically in the first insert
> j={"name" : "TOM", "dob":"12/11/1998"};
> db.employee.insert(j);


3. Accessing data from a collection
> db.employee.find();
the output of the above find will be
{ "_id" : ObjectId("4f499a3f111314dc717421d5"), "name" : "TOM", "dob" : "12/11/1
998" }


4. Deleting data from a colleciton
>db.employee.remove();


5. Adding index.  By default _id fields are indexed, "ensureIndex" is used for adding indexes on additional fields. 1- means ascending and -1 means descending. Indexing will help to increase the performance of large collection. But indexing on a live mongoDB is not recommended, because indexing on large collection may take minutes/hours to complete. use background : true option to run the indexing in background.
> db.employee.ensureIndex({"name" : 1});

Viewing latest article 16
Browse Latest Browse All 28

Trending Articles