简体   繁体   English

在mongodb中存储关系数据

[英]Storing relationship data in mongodb

Say we want to implement a following relationship in mongodb, between 2 users. 假设我们要在mongodb中实现2位用户之间的以下关系。

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) 在传统的关系模型中,您将具有一个关系表,其中存储了这些属性(由2个用户ID索引)

How would you do so in Mongodb? 您将如何在Mongodb中这样做?

If you define the relationship in the User table, it is simple, but i am not sure where the attribute data will go 如果您在User表中定义关系,这很简单,但是我不确定属性数据会去哪里

class User
  references_many :follows, :inverse_of => :followers ,:class_name=>"User"
  references_many :followers,  :inverse_of => :follows ,:class_name=>"User"

You would store the User Ids inside the User model as an array. 您将用户ID作为数组存储在用户模型内。 eg 例如

class User
  ...
  key :follower_ids, Array, :typecast => 'ObjectId'
  many :followers, :in => :follower_ids

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM