mongodb数据库设计多个集合或1个大文件? [英] mongodb database design multiple collections or 1 big document?
问题描述
所以我有用户和产品,每个用户可以有多个产品,这将使得1到许多关系。现在我正在考虑为包含所有产品文档的每个用户动态创建username_products排序规则。
最好将所有内容存储在一个名为product_id的外键的产品中,或者是我的设计是否有效并且提高效率?
So i have users and products each user can have multiple products which would make it 1 to many realationship. Now i am thinking about dynamically creating "username"_products collation for every user which would contain all products documents. Is it better to store everything in 1 collation called products which would have foreign key of user_id or is my design valid and preformance improving?
还要考虑同样的事情对于Mysql来说,最好是用1个表用户和第二个表的产品与外键user_id或更好地创建名为username_products的产品的独立表?考虑性能?
Also consider same thing for Mysql, is it better to have 1 table user and second table products with foreign key user_id or is it better to create seperate table of products called "username"_products? considering performance?
我知道索引,如果表变大,我应该添加索引,如果我使用解决方案号1只有2个表/排序规则,而不是
I know about indexes and that i should add indexes in any case if tables get big and if i go with solution number 1 only 2 tables/collations instead of multiple.
推荐答案
mongo(和nosql)违反数据库规范化。您将拥有冗余的数据,但这符合nosql所提供的精神,例如速度和超轻松的可伸缩性。
mongo (and nosql) goes against database normalization. you will have redundant data but thats in the spirit of things that nosql does offer -- such as speed and ultra-easy scalability.
基本上你想要一个简单有效的找到索引的密钥。
basically you want to a simple and efficient find on an indexed key.
products = db.products.find("_id": user_id);
您不想通过进行两次查询来模拟连接。
you don't want to simulate a join by conducting two queries.
user = db.users.find("_id": user_id);
products_id = user.products_id;
products = db.products.find("_id": products_id);
aka
products = db.products.find("department": "sales");
// ?! wtf关系信息在产品表上?
//?!wtf relationship info on a product table!?
因为上述需要以下nosql噩梦:
because the above would require the following nosql nightmare:
sales_users = db.users.find("department": "sales");
myproductarray = [];
for each record in sales_users {
products_id = record.products_id;
products = db.products.find("_id": products_id);
myproductarray.update(products)
}
return myproductarray;
进一步与mongo您可以索引任何键,包括看似愚蠢的像部门
further with mongo you can index any key including seemingly foolish ones like "department"
在我强烈的意见中,你最好放弃你的表和数据结构,直到至少半定稿你的各种用例和你的用户界面。例如,如果您的用例需要,请在产品表中包含部门。
in my strong opinion, you are better putting off your tables and data structure until at least semi-finalizing your various use cases and your user interface. for example, include things like "department" in the product table if your use case dictates such a need.
这篇关于mongodb数据库设计多个集合或1个大文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!