【问题标题】:Many to Many vs. One to Many in MongoDB design with MongoMapper使用 MongoMapper 在 MongoDB 设计中多对多与一对多
【发布时间】:2012-01-19 06:04:49
【问题描述】:

有一个这样的例子:

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

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

提前致谢!

【问题讨论】:

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


    【解决方案1】:

    你必须使用

    many :author_has_books
    

    所以 MongoMapper 可以获取您的类名并将其关联。不确定它是否会起作用。

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

    【讨论】:

      猜你喜欢
      • 2011-12-18
      • 1970-01-01
      • 2013-11-20
      • 1970-01-01
      • 2019-09-25
      • 1970-01-01
      • 1970-01-01
      • 2011-10-16
      • 1970-01-01
      相关资源
      最近更新 更多