MongoDB 模式设计 --- 与群体的友谊
嗨朋友们~ 我想使用 MongoDB 来实现基于群组的友谊模型。就像谷歌嗡嗡声一样。 例如,
我叫汤姆,史蒂夫和加文是我的朋友。史蒂夫是我的同学兼同事,加文是我的同事。
Tom -Group Classmates Steve -Group Coworkers Steve Gavin
我的问题是如何设计这个架构?
在rails和Mongoid中,我编写了以下代码:
这是user.rb
class User include Mongoid::Document field :username field :email field :block_list, :type => Array, :default => [] key :username embeds_many :groups embeds_many :pending_requests has_and_belongs_to_many :friends, :class_name => "User" end
group.rbending_request.rb
class Group include Mongoid::Document embedded_in :user field :name field :members, :type => Array, :default => [] end
有
class PendingRequest include Mongoid::Document embedded_in :user field :username field :body end
什么建议吗?谢谢。
Hi friends~
I wanna use MongoDB to implement a group based friendship model. Like Google Buzz.
For example,
My name is Tom, Steve and Gavin are my friends. Steve is my classmate and coworker, Gavin is my coworker.
Tom -Group Classmates Steve -Group Coworkers Steve Gavin
My Question is how to design this schema?
In rails and Mongoid, I wrote the follow code:
Here is user.rb
class User include Mongoid::Document field :username field :email field :block_list, :type => Array, :default => [] key :username embeds_many :groups embeds_many :pending_requests has_and_belongs_to_many :friends, :class_name => "User" end
group.rb
class Group include Mongoid::Document embedded_in :user field :name field :members, :type => Array, :default => [] end
pending_request.rb
class PendingRequest include Mongoid::Document embedded_in :user field :username field :body end
Any suggestions? Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过多种方法为此目的设计架构。要问的重要问题之一是:如果汤姆说加文是同事,这是否意味着加文也会将汤姆显示为同事?
是:
如果汤姆或加文可以在他们两人之间创建一个链接,并且无论哪种方式都是相同的链接(无论创建该链接的过程如何),那么您真正谈论的是一种关系。 Mongo 是一个 NoSql 数据库,不执行联接,因此您必须通过多个选择和更新查询来管理这种关系。汤姆可以保留一份同事名单,加文也可以保留一份同事名单,只要他们中的任何一个添加另一个,两个文件都需要更新。说实话,这种关系才是关系数据库所擅长的。 Mongo 可能不是最好的解决方案。
否:
如果 Tom 可以完全独立于 Gavin 的想法来确定 Gavin 是否是同事,那么您应该在用户集合中存储一组同事。对于同学来说也是如此。每个用户文档都有一个姓名字段、一个同事字段、一个同学字段等。要获取 Tom 的信息,您只需从单个集合中提取单个文档即可获得全部信息。
请记住,跟踪文档之间的关系并不是 Mongo 的强项。如果您需要处理大量关系和相对较小的文档,那么 Mongo 确实不是您的最佳选择。
使用 mySql 来管理用户和关系,同时在 Mongo 中存储活动日志、评论、帖子等并没有什么问题。
There are a few ways you could design a schema for this purpose. One of the important things to ask is this: If Tom says Gavin is a coworker, does that mean Gavin will also show Tom as a coworker?
Yes:
If Either Tom or Gavin can create a link between the two of them and it's the same link either way (regardless of the process to create that link), then what you're really talking about is a relationship. Mongo is a NoSql database and doesn't do joins, so you would have to manage this relationship with multiple select and update queries. Tom could keep a list of coworkers and Gavin could keep a list of coworkers and any time either of them adds the other, both documents would need updated. In all honesty, this kind of relationship is what relational databases are good at. Mongo probably isn't the best solution.
No:
If Tom can determine whether Gavin is a coworker completely independently of what Gavin thinks, then you should store an array of coworkers in your user collection. The same goes for classmates. Each user document would have a name field, a coworkers field, a classmates field, etc. To get Tom's info, you only need to pull a single document from a single collection and you have it all.
Keep in mind that tracking relationships between documents is not Mongo's strong suit. If you have a lot of relationships to deal with and relatively small documents, Mongo really isn't your best bet.
There's nothing wrong with using mySql to manage users and relationships, while storing activity logs, comments, posts, etc. in Mongo.