在mongodb中存储关系数据
假设我们想在 mongodb 中实现 2 个用户之间的以下关系。
但是您还想存储一些有关关系的属性(例如创建时间或引用时间)
在传统关系模型中,您将有一个存储这些属性的关系表(由 2 个用户 ID 索引)
您会如何做在 Mongodb 中?
如果在User表中定义关系,很简单,但我不确定属性数据会去哪里
class User
references_many :follows, :inverse_of => :followers ,:class_name=>"User"
references_many :followers, :inverse_of => :follows ,:class_name=>"User"
Say we want to implement a following relationship in mongodb, between 2 users.
But you also want to store some attribute about the relationship (like time created, or referred by)
In a traditional relationship model, you would have a relationship table where these attributes are stored (indexed by the 2 user ids)
How would you do so in Mongodb?
If you define the relationship in the User table, it is simple, but i am not sure where the attribute data will go
class User
references_many :follows, :inverse_of => :followers ,:class_name=>"User"
references_many :followers, :inverse_of => :follows ,:class_name=>"User"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将用户 ID 作为数组存储在用户模型中。例如
You would store the User Ids inside the User model as an array. e.g