我正在學習如何使用mongo與紅寶石使用mongoid寶石。我創建了一個簡單的待辦事項應用程序作爲學習體驗。我每星期一星期一到星期一,每個星期都有許多待辦事項。我的文件將是這個樣子:Mongoid:如何模擬一週內的嵌入式文檔?
week = {
'year': 2012,
'week': 10,
'monday': [
{'task': 'take out the trash', 'effort_points': 1},
{'task': 'build a house', 'effort_points': 5}
],
'tuesday': [],
'wednesday': [],
'thursday': [],
'friday': []
}
到目前爲止,我有一些類,看起來像這樣(有些可能是僞代碼):
class Week
include Mongoid::Document
field :year, type: Integer
field :week, type: Integer
embeds_one :day, as: :monday
embeds_one :day, as: :tuesday
embeds_one :day, as: :wednesday
embeds_one :day, as: :thursday
embeds_one :day, as: :friday
end
class Day
include Mongoid::Document
embedded_in :week
embeds_many :items
end
class Item
include Mongoid::Document
embedded_in :day
field :task
field :effort_points
end
如何適當這些對象進行建模使用mongoid?
嗯...似乎這將違背使用Mongo的優勢。由於這些「任務」的數據量非常小,因此將它們組合在一起(並將它們嵌入到更大的文檔中)是有利的,因此獲取和顯示記錄效率最高。我對Mongo的表現並不瞭解,但我認爲提取單個文檔比返回25個文檔更有效率。你怎麼看? – Andrew 2012-03-07 21:25:39
如果您正在使用的字段上有一個索引來選擇它們,它是1個數據庫調用,速度非常快。與您所談論的SQL數據庫相比,您可以通過將相關內容保存在單個文檔中來避免執行JOIN。例如,如果任務有很多「註釋」,則可以將這些任務留在任務中,而不必加入「筆記」表以檢索它們。 – nicholaides 2012-03-07 23:31:26