简体   繁体   中英

Storing relationship data in mongodb

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"

You would store the User Ids inside the User model as an array. eg

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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