使用MongoMapper设计MongoDB中的多对多对一对多

时间:2012-01-19 06:04:50

标签: database-design mongodb many-to-many one-to-many mongomapper

有一个这样的例子:

class Book
  include MongoMapper::Document
  key :title
  key :author_ids, Array
  many :authors, :in => :author_ids
end

class Author
  include MongoMapper::Document
  key :name
end

是否相当于

class Book
  include MongoMapper::Document
  key :title
  many :AuthorHasBook
end

class AuthorHasBook
  include MongoMapper::Document
  key :written_time

  belongs_to :book, :class_name => "Book"
  belongs_to :author, :class_name => "Author"
end

class Author
  include MongoMapper::Document
  key :name
  many :AuthorHasBook
end

哪个更好?我想如果我需要为“关系”实体添加一些字段,我必须使用第二种解决方案吗?

提前致谢!

1 个答案:

答案 0 :(得分:1)

你必须使用

many :author_has_books

所以MongoMapper可以拿起你的类名并将其关联起来。不确定它是否会起作用。

唯一的另一个区别是你可以反转多对多的关联。