Sunday, June 05, 2011

MongoDB architecture

As already stated in some previous blogposts the MongoDB is somewhat different than the databases you are might used to. The concept a schema, a table and a record is something that is not used within the MongoDB setup. When you are used to work databases like a Oracle database this might come a little strange to you at first however due to the nature of MongoDB you will get the hang of it quite quick.

To visualize the way MongoDB is working you can see the below chart;

Database;
within MongoDB you will be able to create separate databases. A database will use its own users, store it own data and will be its own datafile on your servers filesystem. As a good practice you can keep the rule that evert application should have its own database. a example of how to create a database you can see in this previous blogpost.

Collection;
As stated MongoDB is not using the concept of a table, if you would like to make a reference between MongoDB and a "normal DB" you can state that a collection is a table. A collection holds one or more documents. lets say you want to add the following document to your database {"shipping_number" : "1046", "receivedate" : "12-FEB-2011"} you will have to place this in a collection. You can state that the document in this case is the record (or row). To insert this in the collection shipping you can enter the use the following command;

db.shipping.insert({"shipping_number" : "1046", "receivedate" : "12-FEB-2011"})

One more option is to mention when we are talking about collections, sub-collections. You have the option to logically group collections in a sub-collection. This is not having any technical implication, it is just a handy feature which can make you development and administration a little more easy. For example if you have a lot of collections regarding shipping in your database, for example data on shipments, trucks, locations of warehouses, authorized personal, average loading times, etc etc etc. You might want to group all those collections in a logical way so that people who work on developing code for your shipping module know where to find this this code. If you want to insert the above line however now you would like to make use of sub-collections (for example the sub-collection "ship" you should use the following command:

db.ship.shipping.insert({"shipping_number" : "1046", "receivedate" : "12-FEB-2011"})

Documents;
we already touched the subject of documents, a document can be seen as the record (or row) in your table. In MongoDB this is a document in a collection. meaning that your document which you inserted in the previous example is in essence; {"shipping_number" : "1046", "receivedate" : "12-FEB-2011"}

Key-value pair:
the key value pair can be compared the best with the column and the value of a record. For example {"shipping_number" : "1046", "receivedate" : "12-FEB-2011"} in this document (record) we have 2 key-value pairs (columns), the shipping_number column and the receivedate column. In this example the shipping_number key has the value 1046 and the key receivedate has the value 12-FEB-2011.

If you take this comparison as a guideline this might help you to translate your idea from a standard DB into a MongoDB way of thinking.

1 comment:

Anonymous said...

Thanks for your post, helped me understand certain stuff...