如何在 MongoDB 中组织多对多关系
我有两个表/集合;用户和组。用户可以是任意数量的组的成员,并且用户也可以是任意数量的组的所有者。在关系数据库中,我可能有第三个表,称为 UserGroups,其中包含 UserID 列、GroupID 列和 IsOwner 列。
我正在使用 MongoDB,并且我确信文档数据库中的这种关系有不同的方法。我是否应该将组和组作为所有者的列表作为两个 ObjectID 数组嵌入到 Users 表中?我是否还应该将组表中的成员和所有者列表存储为两个数组,从而有效地镜像关系,从而导致关系信息重复?
或者桥接用户组表对于多对多关系来说是文档数据库中的合法概念吗?
谢谢
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.
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?
Or is a bridging UserGroups table a legitimate concept in document databases for many to many relationships?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我所看到的以及我当前使用的是每个文档中带有节点 ID 的嵌入式数组。
因此文档 user1 具有属性 groups: [id1,id2]
并且文档 group1 具有属性 users:[user1]。文档 group2 还具有属性用户:[user1]。
这样您就可以获得一个 Group 对象并轻松选择所有相关用户,对于 User 也是如此。
创建和更新对象时这需要更多的工作。当你说两个对象相关时,你必须更新这两个对象。
MongoDB 中还有一个概念 DBReferences,根据您的驱动程序,它会在检索文档时自动提取引用的对象。
http://www.mongodb.org/display/DOCS/Database+References #DatabaseReferences-DBRef
What I've seen done, and what I currently use are embedded arrays with node id's in each document.
So document user1 has property groups: [id1,id2]
And document group1 has property users: [user1]. Document group2 also has property users: [user1].
This way you get a Group object and easily select all related users, and the same for the 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.
There's also a concept DBReferences in MongoDB and depending on your driver, it'll pull referenced objects automatically when retrieving a document.
http://www.mongodb.org/display/DOCS/Database+References#DatabaseReferences-DBRef
如果有人感兴趣的话,我刚刚在 mongoDB 博客上发现了一篇非常好的文章。 6 条经验规则MongoDB 架构设计。本文分为 3 个部分,读完这 3 个部分你就会有一个很好的理解。
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.
让我们通过示例来理解多对多关系
书籍与作者是几对几关系,因此我们可以在另一个文档中包含一系列书籍或作者。学生到老师也是如此。我们还可以冒着重复的风险进行嵌入。然而,这将要求每个学生在插入之前在系统中都有一名老师,反之亦然。应用程序逻辑可能始终不允许这样做。换句话说,父对象必须存在,子对象才能存在。
但是,当您具有多对多关系时,请使用两个集合并具有真正的链接。
Let's understand Many to Many Relations with an examples
The books to authors is a few to few relationship, 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 many relationship, use two collections and have a true linking.