Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongo using subcollection

Is there any sense in using subcollections(about 15) for every user? Amount of users is about 10k. Amount of records in subcollections can reach 2M. Or maybe I should use common large collection? Thanks for your answers.

like image 264
alexvassel Avatar asked Oct 17 '11 10:10

alexvassel


1 Answers

Embedded collections make database simpler (they decrease number of collections) and make database work faster. I am usually trying embedd everything and only if i can't i am create separate collections. If your embedded collection will be big you can exclude it from user during loading:

db.posts.find( { tags : 'tennis' }, { comments : 0 } );

Above query will load posts without comments. Documentation

But embedded collections also add some complexity. For example you mongodb can't sort embedded collection for you. Order always default. But you can do it on client side. If default order work for you, you can page nested collection via $slice:

db.posts.find({}, {comments:{$slice: [20, 10]}}) // skip 20, limit 10

Also take a look into this doc about schema design.

So + 1 to embedding whenever it possible.

like image 87
Andrew Orsich Avatar answered Oct 19 '22 04:10

Andrew Orsich