Mongo using a subcollection

Does it make sense to use subcollections (about 15) for each user? The number of users is about 10 thousand. The number of records in subcollections can reach 2M. Or maybe I should use a common large collection? Thank you for your responses.

+7
source share
1 answer

Built-in collections simplify the work with the database (reduce the number of collections) and speed up the database. Usually I try to implement everything, and only if I cannot create separate collections. If your inline collection is large, you can exclude it from the user at boot time:

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

Above the request will upload messages without comment. Documentation

But inline collections also add some complexity. For example, mongodb cannot sort the built-in collection for you. Ordering is always the default. But you can do it on the client side. If the default order works for you, you can insert a nested collection through $ slice:

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

Also check out the doc schema description.

So + 1 for implementation when possible.

+8
source

All Articles