雾化类场的价值?

时间:2012-04-24 19:42:05

标签: mongodb sinatra mongoid model-associations

考虑以下Mongoid模型

class Doc
  include Mongoid::Document
  field :name, type: String
  embeds_many :images
  embeds_many :videos
end

class Image
  include Mongoid::Document
  field :url, type: String
  field :caption, type: String
  embedded_in :Doc
end

class Video
  include Mongoid::Document
  field :url, type: String
  field :caption, type: String
  embedded_in :Doc
end

与此模型相比

class Doc
  include Mongoid::Document
  field :name, type: String
  embeds_many :images
  embeds_many :videos
end

class Image
  include Mongoid::Document
  embeds_many :urls
  embeds_many :captions
  embedded_in :Doc
end

class Video
  include Mongoid::Document
  embeds_many :urls
  embeds_many :captions
  embedded_in :Doc
end

class Url
  include Mongoid::Document
  embedded_in :image
  embedded_in :video
  field :url, type: String
end

class Caption
  include Mongoid::Document
  embedded_in :image
  embedded_in :video
  field :caption, type: String
end

每种型号对另一种型号的好处是什么?

我应该为了它的简洁而选择第一个,还是应该将它原子化到url.url点,这样我以后可以更好地控制查询?

1 个答案:

答案 0 :(得分:0)

第一个模型允许您将一个URL与每个图像或视频完全关联。第二个模型允许您将许多URL与每个图像或视频相关联。

哪种更适合您的业务需求?图像是否可能包含多个URL或只有一个?

就个人而言,我会选择第一个模型,除非您对给定图像严格需要多个URL。由于网络上每个媒体的URL几乎都保证是唯一的,因此过度规范化数据结构确实没有意义。 URL类中的记录与视频和图像记录的总和一样多,那么您可以节省多少?

如果是某些其他字符串字段可能具有非唯一值(例如,标签),那么将其分解是非常有意义的,因为您在标签模型中的记录将大大减少,然后是图像和视频记录,假设高度或标签重复使用。

有意义吗?