Sunday, November 18, 2012

MongoDb for beginners


What is MongoDb?

MongoDb is a non-relational data store for JSON documents. By non-relational we mean that it does not store data in tables like relational databases does. It stores JSON documents.


What is JSON? JSON(JavaScript Object Notation) is a standard for representing data structures in a text-based human-readable form. JSON documents look like this:

{"Name" : "Bob"} 

This very simple JSON document has the key "Name" and the value "Bob". You can create more complex JSON documents with hierarchies:

{"Name" : "Bob", "Age" : 50, "Hobbies" : ["hockey", "football", "movies"], "Account" : {"Login" : "Bobby", "Password" : "12345"}}

JSON documents are stored within MongoDb collections(collections are analogous to tables in a relational database). It’s very useful when writing programs, because this type of data structures looks a lot closer to what you have inside your programs, than relational database does:
 class Person  
 {  
   public string Name;  
   public int Age;  
   public string[] Hobbies;  
   public AccountDetails Account;  
 }  
 class AccountDetails  
 {  
   public string Login;  
   public string Password;  
 }  

That’s new, because you never do that in relational table. The fact that MongoDb stores documents makes programming your domain model very convenient.

Schemaless 

Another interesting aspect about MongoDb is that it also schemaless. Schemaless means that two documents don’t need to have a same schema.

For example, you can store this documents inside the same collection:
{"a" : 1, "b" : 2}
{"a" : 1, "b" : 2, "c" : 3}

MongoDb vs Relational databases


The idea of MongoDb is to retain most of scalability and performance, giving you a lot of functionality to work with, but not quite as much as you can get in relational database management systems. 

That’s because there are few different things are missing in MongoDb:
  • Joins. MongoDb stores documents. Each document is stored in collection. But if you want to do a JOIN between two collections - you can’t do that in MongoDb. And the reason is that JOINs scale very poorly.
  • Transactions. That sound very bad, but the truth is that you don't need transactions in MongoDb in applications where you would need them in relational systems. And the reason is that documents are hierarchical in MongoDb and you can access those documents atomically. 
Choice between non-relational and relational data stores can't be easy. But if you want to create a scalable, high-performance application you should consider MongoDb as an option.

This article is based on video lectures from 10gen education course https://education.10gen.com.

2 comments: