Showing posts with label mongoDB. Show all posts
Showing posts with label mongoDB. Show all posts

Saturday, July 16, 2011

Basic quickstart commands on MongoDB

In this post I will explain the basic commands for inserting, retrieving, limiting the data sent from/to the database.

Ok so now we have inserting data to the db.

go to /bin folder and type mongo. The shell appears.
Now type insert these three documents:


> db.movies.save({title:"Harry Potter",year:2010, duration: "120 min"});
> db.movies.save({title:"Expendables",year:2010,duration:"140 min"});
> db.movies.save({title:"Green Lantern",year:2011,duration:"150 min"});


We are creating collection called movies and add three documents for movies. (Note: The data here is maybe not correct, it is just for example). So these commands corespond to the SQL statement

insert into movies (title,year,duration) values ("Harry Potter", 2010,"120 min");
insert into movies (title,year,duration) values ("Expendables", 2010,"140 min");
insert into movies (title,year,duration) values ("Green Lantern", 2011,"150 min");

 Now use this command

> db.movies.find();

This will list all documents in the movies collection. Now we will create a script with for statement.

> for (var i = 1; i <= 25; i++) db.script.save({blog:"TunePHP", j : i});
 
This script will create a collection called "script" and add documents in the format 
{blog:"TunePHP", j : i}. 
 
run > db.script.find(); 
 
Now the limit of the find command is 20 rows. In case where there
are more than 20 documents in a collection we use the it command. So now in terminal run
 
> it 
 
it will show the rest docs in the script collection.

Installing MongoDB

I use MongoDB on Ubuntu but you can also use it on Windows. I will explain how to install it on Ubuntu.

I installed myselft with oppening terminal and typing:

$ sudo apt-get install mongodb

Wait a moment and it will be installed. There is an option to build it from binary sources but this is much more complex (In other post).

Next, you must create directories for the db so open terminal and type in these commands:

$ sudo mkdir -p /data/db/
$ sudo chown `id -u` /data/db 
 
This creates folder data/db at root level and change owner to root. 
Now you should be able to use MongoDB. Start the server by typing in terminal:
 
$ sudo start mongodb  or this $ service mongodb start. You will get a message 
whether the service is started. If it is started than go to this folder /bin and type  
mongo in terminal i.e
 
$ cd /bin
$ mongo    you will get the ">" char in terminal so it means it works.
For start type in 
 
> use mydb 
> db.testcol.save({json : "this is a json object"});
> db.testcol.find();
You should get something like this as output:
{ "_id" : ObjectId("4c2209f9f3924d31102bd84a"), "json" : "this is a json object" }
 
With these commands we use the database called mydb and than create a collection 
(table) called testcol and save one BSON document (row)  
{json : "this is a json object"}. The find command prints all document in the collection  
testcol. You should visit the SQL to Mongo Mapping page at SQL-Mongo . 
 
You can notice that we use json notation to create documents (rows) in the database, 
Mongo works its way i.e it constantly converts from json to bson and vice verse. 
 
I will add some examples on how to work with Mongo so make sure you read well 
about json.  
 
 You might wanna visit the downloads page @ downloads . 
Select you operating system version and download it.  Also visit Quickstart

Intro to MongoDB

Slideshare presentation - PHP to MongoDB

PHP, Lithium and MongoDBRead this presentation and post some questions/comments. This is just for start, will add some content soon.  
View more presentations from Mitch Pirtle

Wednesday, July 13, 2011

Intro to document-oriented databases

In this post I will explain the basics and advantages of the document-oriented databases over relational dbs.

Document database is a database constructed of documents that has its own structure and can be independent from each other i.e there is no need of relations like in RDBMS.  There are no tables, rows, relations at all. So when I want to add new field in the database I simply select a document and add it without the need to update and affect other documents in the db.

DO databases are called semi-structured dbs because they don't have the need of a predefined schema like in RDBMS.

For example if I have one document that have these fields: name, surname, proffession, phone, I can create document by filling all fields except the phone field which can be empty and it will not be kept as null but it will simply do not exist. But I can also add new field which is not in the above list for example email and give it a value of my email.

Below is a list of the advantages over RDBMS:

-  Objects are stored as documents. So now we can use the serialize method in a language to convert object to a document and save it to the db.
- Documents can be of any complexity. Objects model can be read and written at once. NICEEEEEEE :)
In this case we are relieved of the need to think of complex sql statements and stored procs.
-  Documents are all the way independent. This is good for performance and the side effects of concurrency are decreased
- Formats for accessing and using DO dbs. The documents in the dbs are described with JSON, XML and derivatives like Binary JSON
- Schema free. This ability gives us flexibilty to sturcture the data inside without the need to restructure it again after some time.
-  Built-in verisoning. DO databases can support versioning of documents and it can be done with several clicks

The DO dbs can be sorted in these categories Document, Graph, Key/Value, and Tabular/Wide Column.
 All of them with its own advantages and drawbacks.

The most popular products that declare themselves as DO dbs are:

- CouchDB
- RavenDB
- MongoDB

So where these dbs would best fit in? Well DO dbs are good for CMS and CRM systems where the user usually wants to customize his data and this is easy with saving documents. Also DO dbs would be useful in creating and storing data from a shopping cart and user sessions.

In the next posts I will try to explain briefly the above given products and their properties.