views:

1458

answers:

6

I'm used to using relational databases like MySQL or PostgreSQL, and combined with MVC frameworks such as Symfony, RoR or Django, and I think it works great.

But lately I've heard a lot about MongoDB which is a non-relational database, or, to quote the official definition,

a scalable, high-performance, open source, schema-free, document-oriented database.

I'm really interested in being on edge and want to be aware of all the options I'll have for a next project and choose the best technologies out there.

In which cases using MongoDB (or similar databases) is better than using a "classic" relational databases? And what are the advantages of MongoDB vs MySQL in general? Or at least, why is it so different?

If you have pointers to documentation and/or examples, it would be of great help too.

+3  A: 

There are numerous advantages.

For instance your database schema will be more scallable, you won't have to worry about migrations, the code will be more pleasant to write... For instance here's one of my model's code :

class Setting
  include MongoMapper::Document

  key :news_search, String, :required => true
  key :is_availaible_for_iphone, :required => true, :default => false

  belongs_to :movie
end

Adding a key is just adding a line of code !

There are also other advantages that will appear in the long run, like a better scallability and speed.

... But keep in mind that a non-relational database is not better than a relational one. If your database have a lot of relations and normalization, it might makes little sense to use something like mongodb. It's all about finding the right tool for the job.

For more things to read I'd recommend taking a look at "Why I think Mongo is to Databases what Rails was to Frameworks" or this post on the mongodb website. To get exited and if you speak french, take a look at this article explaining how to set up mongodb from scratch.

Edit: I almost forgot to tell you about this railscast by Ryan. It's very interesting and makes you want to start right away!

marcgg
This railscast seems really interesting; going to take a look at it, hopefully I'll have a better understanding of how this works.
Guillaume Flandre
+1  A: 

After a question of databases with textual storage), I glanced at MongoDB and similar systems.
If I understood correctly, they are supposed to be easier to use and setup, and much faster. Perhaps also more secure as the lack of SQL prevents SQL injection...
Apparently, MongoDB is used mostly for Web applications.
Basically, and they state that themselves, these databases aren't suited for complex queries, data-mining, etc. But they shine at retrieving quickly lot of flat data.

PhiLho
There's a couple of mis-conceptions in your answer. While MongoDB isn't vulnerable to SQL injection, it is susceptible to injection more generally. You can specify arbitrary Javascript in the $where clause of a query.Also, unlike a lot of the other NoSQL option, MongoDB can actually do some pretty complex queries.
Emily
Thanks for the precisions. Note that, as I stated, it is the MongoDB site itself that emitted restrictions on relational queries. Unless I misunderstood something else...
PhiLho
It seems quite likely that they said MongoDB is unsuited for complex relational queries, but for complex non-relational queries, it's quite well-suited. Take a look at http://www.mongodb.org/display/DOCS/Advanced+Queries for some of the cool stuff you can do.
Emily
+2  A: 

MongoDB was featured on FLOSS Weekly this week - http://twit.tv/floss105 A database using a similar concept is CouchDB which was featured on another FLOSS Weekly: http://twit.tv/floss36

I think it's worth listening to those in addition to the links provided by @marcgg

Patrick
+5  A: 

Here are some of the advantages of MongoDB for building web applications:

  1. A document-based data model. The basic unit of storage is analogous to JSON, Python dictionaries, Ruby hashes, etc. This is a rich data structure capable of holding arrays and other documents. This means you can often represent in a single entity a construct what would require several tables to properly represent in a relational db.
  2. Deep query-ability. MongoDB supports dynamic queries on documents using a document-based query language that's nearly as powerful as SQL.
  3. No schema migrations. Since MongoDB is schema-free, your code defines your schema.
  4. Better performance. There are many reasons for this. One is that, since the document model frequently doesn't need joins, MongoDB doesn't support them; another is that MongoDB uses memory-mapped files and a different consistency model.
  5. A clear path to horizontal scalability.

You'll need to read more about it and play with it to get a better idea. Here's an online demo:

http://mongo.kylebanker.com/

Kyle Banker
I accepted this answer, but take a look below at the other good answers. @marcgg answered with interesting links for instance.
Guillaume Flandre
+1  A: 

It's all about trade offs. MongoDB is fast but not ACID, it has no transactions. It is better than MySQL in some use cases and worse in others.

AABBCCDD
+1  A: 

The advantage of schema-free is that you can dump whatever your load is in it, and no one will ever have any ground for complaining about it, or for saying that it was wrong.

It also means that whatever you dump in it, remains totally void of meaning after you have done so.

Some would label that a gross disadvantage, some others won't.

The fact that a relational database has a well-established schema, is a consequence of the fact that it has a well-established set of extensional predicates, which are what allows us to attach meaning to what is recorded in the database, and which are also a necessary prerequisite for us to do so.

Without a well-established schema, no extensional predicates, and without extensional precicates, no way for the user to make any meaning out of what was stuffed in it.

Erwin Smout