Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB: benefits of using embedded objects

What are the benifits of using embedded objects?

In my practice it is very rare when I don't use embedded objects outside of its parent. I mean common example with comments that are embedded_in post: I need to show last comments in the list of updates, I need to show comments for current_user etc. So comments can't be embedded or it would be a pain in the ass.

So there is very few use cases when I need to use embedded objects.

The question is: why should I use embedded objects even in those rare use cases, what are the benefits of embedding?

like image 480
fl00r Avatar asked Sep 11 '11 08:09

fl00r


1 Answers

Embedded objects it's a big big benefit of any nosql databases. Usually embedding = read performance + scalability. Let's take for example SO. You can embedd question/answer comments within question, answer document. And it's means that you no need to join comments when you need to display answers/questions. If you no need to join -- you can move collection to separate computer to speedup your app. In relational database sub objects usually become a separate table (except denormalization). And in my practice there is very very many cases where i need to embedd documents.

But not for all cases embedding good fit, you true about 'pain in the ass' with ebedding in some situations. You no need to embedd everything! In your case just create separate collection for comments or..

There is another good known concept in nosql world: denormalization.

I need to show last comments in the list of updates

Okay, if you need latest updates you can insert last comments into your updates collection to quick display it.

I need to show comments for current_user

Okay, embedd user comments into user objects.

BTW, denormalization usually async process, because writing one object into many collections can lead to write performance issues.

Hopefully, my answer will give you some ideas.

like image 83
Andrew Orsich Avatar answered Nov 07 '22 06:11

Andrew Orsich