meow
meow

Reputation: 28164

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"

Upvotes: 1

Views: 519

Answers (1)

Joshua Partogi
Joshua Partogi

Reputation: 16425

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

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

Upvotes: 1

Related Questions