如何在 Mongodb 中建立多对多关系?
可能的重复:
MongoDB 多对多关联
我已经使用 MySQL/SQLite 多年并且有我真的很难理解如何在 MongoDB 中处理关系,尤其是多对多关系,我只是不知道什么是最佳实践。
好的,我有一个与公司
合作的项目。一名员工
可以为多家公司工作,而一家公司也可以拥有多名员工。连接表称为contracts
。
IE: 公司-<合同>-员工
字段。
公司:id、名称
合同:长度、工资、company_id、employee_id
员工:ID、姓名
我实际的最终应用程序每个表都有 20 多个字段,但我暂时保持简单。
当我尝试在 Mongodb 中复制这个时,这就是我开始感到头疼的地方。
IE;
// Idea 1. Put everything in one hierarchy.
// But, What should I be putting in contracts?
// Where does employees go?
//
// Pseduocode (based from MongoDB online browser shell)
var company = {name:'Company A', contracts:[????]};
问题是,如果一名员工可以为 3 家不同的公司工作。对于他工作的每家公司,我都会将员工内容重复 3 遍。
那么,为了减少重复内容的数量,我需要将公司、员工和合同文件分开,对吗?
如果我将它们分开,我该如何连接,在哪里连接?
是否会存储 MongoDB 为我公司文档中的每行/项目生成的非常长的唯一 ID?
如果是这样,我该如何拿回我的东西?
即:打印一份为 1 个公司工作、合同期限少于 10 周的所有员工的列表。
正如你所看到的,我在 MongoDB 上仍然有点挣扎,并且我仍然以 SQL 类型的方式思考,但我希望有人可以帮助至少在如何处理方面指出正确的方向以及 MongoDB 中多对多关系的最佳实践。
谢谢。
Possible Duplicate:
MongoDB Many-to-Many Association
I've used MySQL/SQLite for years and having real trouble getting my head around how to do relationships in MongoDB, especially ManyToMany relationships, I just don't know what is best practice.
Okay, I have a project with a company
. An employee
can work for multiple companies, and a company can have multiple employees. The join table is called contracts
.
ie:company -< contracts >- employees
Fields.
company: id, name
contracts: length, salary, company_id, employee_id
employee: id, name
My actual final app has 20+ fields for each table, but I have kept it simple for the moment.
This is where my head starts to hurt when trying to replicate this in Mongodb.
ie;
// Idea 1. Put everything in one hierarchy.
// But, What should I be putting in contracts?
// Where does employees go?
//
// Pseduocode (based from MongoDB online browser shell)
var company = {name:'Company A', contracts:[????]};
The problem is, if an employee can work for 3 different companies. I will be repeating the employee content 3 times for each company he works for.
So, to reduce the amount of duplicating content I would need to separate the companies, employees and contracts documents, correct?
If I separate them, how do I make the joins, and where?
Would it be a case of storing the really long unique ID that MongoDB generates for each row/item inside my company document?
And if so, how do I get my stuff back?
i.e: Print a list of all employees working for 1 company that has a contract of less than 10 weeks.
As you can see, I'm still a bit struggling with MongoDB, and I'm still thinking in a SQL type of way, but I'm hoping someone can help with at least pointing in the right direction in terms of how to handle, and the best practices for ManyToMany relationships in MongoDB.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下介绍了您可以在多对多关系中使用的策略。
http://www.scribd.com/doc/47326395/MongoBoulder-Schema-Design 本质上,
这是信息重复(因此需要额外维护)与查询简单性之间的权衡。
Here's a presentation on the strategies you can use from Many-to-Many relationships.
http://www.scribd.com/doc/47326395/MongoBoulder-Schema-Design
Essentially its a trade off between duplication of information (and therefore extra maintenance) versus the simplicity of query.