Introduction to Mongodb

Untitled Blog Post Name

MongoDB is a NOSQL database model which is unstructured/schema-less type, its easy to scale, and fast to query large data type comparing to traditional database type such as mysql.

There are quiet a few choice to choose for nosql, here are some of the big names out there:

  • CrunchDB
  • MongoDB
  • Redis
  • Gemfire
I choose MongoDB because i typically find that its easier for me to understand and has got lots of great tutorials on the net.
So to install MongoDB all you need to do is go to your terminal on Mac, for PC/ other platform users please check out http://docs.mongodb.org/manual/installation/

brew install mongodb

or 

port install mongodb

after installed, you should be able to test it with in terminal key in 

mongo

if you see some command like:

MongoDB shell version: 2.4.5
connecting to: test


Then you are good to go…
MongoDB has got great features which i will talk about later,
for now just remember that here are some key ideas no nosql database, in traditional database, e.g.: mysql, each database has tables which is similar to NOSQL collections.
try to see it yourself, again on terminal, key in 

show dbs

What it does is actually what you are typing in, “show database”, by default you show have a database called “test” or if not, thats ok because we are going to create one later. Now type in

use test

again its pretty expressive, it say use the “test” database. So now we are inside the “test” database, which I mentioned earlier, these datas in here are organised as collections as what “tables” in mysql. Say if you want to check the collections in here you can do 

show collections

If you do not have any collection here, no worry, because we are going to create one now with some document and fields in here, ok again document is what is refer to rows in mysql and fields are columns in mysql. Here is an example how the data looks like:

{

username: “maxlibin”,

age: 25,

job: “front-end developer”

},

{

username: “johnsontan”,

age: 30,

job: “project manager”

}

I know you been thinking, this looks like json, infect it is which is called BSON, its binary json type.

Ok lets get going to create a new document and its fields,

first we going user insert function in terminal like this.

db.user.insert({ })

you noticed that we did not create a “user” document, the great thing about mongodb is that it automatic create an document only when we insert or save some data to it. So go ahead and add in some fields into the insert function.

db.user.insert({username: “maxlibin”, age: 25, job: “front-end developer”});
db.user.insert({username: “johnsontan”, age: 30, job: “project manager”});

This will add in some data to the mongodb database, and if you want to check those queries jus do:

db.user.find()

Ok this is the end of the first session on introduction to mongodb, I will continue to write on second session on queries of mongodb…