9
一個非常簡單的設計問題。假設我想構建Facebook Messenger。假設約翰和瑪麗在聊天,這是一個更好的方法?MongoDB:消息應用的最佳設計
1)每會話1文件,messages
是消息對象的陣列
{ participants: ['john', 'marry'],
messages: [
{ sender: 'john', content: 'howdy', time_created: new Date() },
{ sender: 'marry', content: 'good u', time_created: new Date() },
...
]
}
2)每個消息
{ participants: ['john', 'marry'], sender: 'john', message: 'howdy', time_created: new Date() } // document 1
{ participants: ['john', 'marry'], sender: 'marry', message: 'good u', time_created: new Date() } // document 2
....
哪種方法具有在以下方面更好的性能1文件插入新消息(更新對話與創建新文檔)?
還是有沒有更好的方法(如我的第二種方法,我不知道是否是一個好的設計來指定每個文檔中的參與者字段)?
謝謝!
更新每個新消息的2個集合是很多數據庫寫入? –
您只會更新每條新消息的消息收集。對話集合僅在有新參與者或新對話開始時創建/更新。您的應用程序可以保留對話ID(很像會話)。 –
,但如果您不更新每條新消息的對話集(以存儲上次更新的時間),那麼當用戶打開應用程序時,如何獲取從最近到最近排序的所有對話? –