如何在 MongoDB 中组织多对多关系
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4839881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to organise a many to many relationship in MongoDB
提问by Typo Johnson
I have two tables/collections; Users and Groups. A user can be a member of any number of groups and a user can also be an owner of any number of groups. In a relational database I'd probably have a third table called UserGroups with a UserID column, a GroupID column and an IsOwner column.
我有两个表/集合;用户和组。用户可以是任意数量组的成员,用户也可以是任意数量组的所有者。在关系数据库中,我可能有一个名为 UserGroups 的第三个表,其中包含一个 UserID 列、一个 GroupID 列和一个 IsOwner 列。
I'm using MongoDB and I'm sure there is a different approach for this kind of relationship in a document database. Should I embed the list of groups and groups-as-owner inside the Users table as two arrays of ObjectIDs? Should I also store the list of members and owners in the Groups table as two arrays, effectively mirroring the relationship causing a duplication of relationship information?
我正在使用 MongoDB,并且我确信文档数据库中的这种关系有一种不同的方法。我应该将组和组作为所有者的列表作为两个 ObjectID 数组嵌入到用户表中吗?我是否还应该将 Groups 表中的成员和所有者列表存储为两个数组,从而有效地镜像关系,从而导致关系信息重复?
Or is a bridging UserGroups table a legitimate concept in document databases for many to many relationships?
或者桥接 UserGroups 表是文档数据库中多对多关系的合法概念吗?
Thanks
谢谢
采纳答案by Alexandru Petrescu
What I've seen done, and what I currently use are embedded arrays with node id's in each document.
我所看到的以及我目前使用的是每个文档中带有节点 ID 的嵌入式数组。
So document user1 has property groups: [id1,id2]
所以文档 user1 有属性组:[id1,id2]
And document group1 has property users: [user1]. Document group2 also has property users: [user1].
并且文档 group1 具有属性 users:[user1]。文档 group2 也有属性 users:[user1]。
This way you get a Group object and easily select all related users, and the same for the User.
通过这种方式,您可以获得一个 Group 对象并轻松选择所有相关用户,对于 User 也是如此。
This takes a bit more work when creating and updating the object. When you say 2 objects are related, you have to update both objects.
在创建和更新对象时,这需要做更多的工作。当您说 2 个对象相关时,您必须更新这两个对象。
There's also a concept DBReferences in MongoDB and depending on your driver, it'll pull referenced objects automatically when retrieving a document.
MongoDB 中还有一个 DBReferences 概念,根据您的驱动程序,它会在检索文档时自动提取引用的对象。
http://www.mongodb.org/display/DOCS/Database+References#DatabaseReferences-DBRef
http://www.mongodb.org/display/DOCS/Database+References#DatabaseReferences-DBRef
回答by Mamun Sardar
In-case anyone interested, I just bumped into a very good article posted in mongoDB blog. 6 Rules of Thumb for MongoDB Schema Design. There are 3 parts in this article, after reading all 3 you'll have a good understanding.
万一有人感兴趣,我刚刚在 mongoDB 博客上看到了一篇非常好的文章。MongoDB 模式设计的 6 条经验法则。本文一共分为3个部分,看完3个你就会有一个很好的理解。
回答by xameeramir
Let's understand Many to Many Relations with an examples
让我们通过一个例子来理解多对多关系
- books to authors
- students to teachers
- 给作者的书
- 学生对老师
The books to authors is a few to fewrelationship, so we can have either an array of books or authors inside another's document. Same goes for students to teachers. We could also embed at the risk of duplication. However this will required that each student has a teacher in the system before insertion and vice versa. The application logic may always not allow it. In other words, the parent object must exist for the child object to exist.
书籍与作者的关系是少对少的关系,因此我们可以在另一个文档中包含一系列书籍或作者。学生对老师也是如此。我们也可能冒着重复的风险进行嵌入。然而,这将要求每个学生在插入之前在系统中都有一个老师,反之亦然。应用程序逻辑可能始终不允许。换句话说,父对象必须存在,子对象才能存在。
But when you have many to manyrelationship, use two collections and have a true linking.
但是当你有多对多的关系时,使用两个集合并有一个真正的链接。