mongodb-一个集合中的许多文档与许多集合中的文档 [英] mongodb - many documents in one collection vs many collection

查看:202
本文介绍了mongodb-一个集合中的许多文档与许多集合中的文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用mongodb作为数据库存储.

I'm using mongodb as database storage.

我的网络应用必须收集用户回复.

my web app has to collect user responses.

用户响应是mongodb中的文档(或sql中的一行).文件长度大约为10〜200.

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

用户响应已分类(仅一类).对于每个类别,用户响应的数量在100到5000之间.如果两个文件属于同一类别,则长度相同. (或者它们在sql中具有相同的列)

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.

目前,我的数据结构是

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?

推荐答案

当然,答案取决于您的查询模式以及要查找的集合数.在不了解更多信息的情况下,我怀疑您将需要进行跨越许多响应集合的查询.

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.

例如,如果每个userresponse都有一个userId字段,并且假设您想获取特定用户所有响应的日期排序列表.您将需要遍历所有集合,对每个集合进行查询,然后将结果合并到客户端代码中.显然,与带索引的UserResponse集合中的单个简单查询/排序相比,这将是非常低效的.

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.

这篇关于mongodb-一个集合中的许多文档与许多集合中的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆