Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongodb - many documents in one collection vs many collection

Tags:

mongodb

I'm using mongodb as database storage.

my web app has to collect user responses.

a user response is a document in mongodb (or a row in sql). length of a document is about 10~200.

user responses are categorized(to only one category). for each category, number of user response is between 100~5000. if two document is in same category, the have same length. (or they have same columns in sql)

a category can be dynamically created/deleted by request of admins.

currently, my data structue is

category collection
{_id, 'name' : 'c1', 'somevalue' : '123'}
{_id, 'name' : 'c2', 'somevalue' : '23'}
{_id, 'name' : 'c3', 'somevalue' : '143'}
{_id, 'name' : 'c4', 'somevalue' : '153'}
...

'c1' collection
{ userresponse1 }
{ userresponse2 }
{ userresponse3 }
...

'c2' collection
{ userresponse1 }
{ userresponse2 }
{ userresponse3 }
...

'c3' collection
{ userresponse1 }
{ userresponse2 }
{ userresponse3 }
...

'cN' collection
{ userresponse1 }
{ userresponse2 }
{ userresponse3 }
..

Is this a wise decision? I'm worried about possibility of something going bad by assigning a collection for each category. will there be some performance issues if I have many collections? should I merge my collections and give the userresponses some identifiers instead?

like image 907
thkang Avatar asked Jan 30 '13 15:01

thkang


1 Answers

Of course the answer depends on you query patterns and how many collections you're looking at having. Without knowing more, I would suspect that you would need to make queries that span many of the response collections.

For example if each userresponse had a userId field and suppose you wanted to get a date sorted list of all the responses for a specific user. You would need to loop over all the collections, query each, and combine the results in client code. Obviously this would be highly inefficient compared to a single simple query/sort in an indexed UserResponse collection.

like image 93
mjhm Avatar answered Nov 15 '22 06:11

mjhm