MongoDB:消息传递应用的最佳devise

一个非常简单的devise问题。 假设我想build立Facebook Messenger。 假设约翰和玛丽正在聊天,这是一个更好的方法?

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) 每个消息1个文件

 { 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 .... 

哪种方法在插入新消息(更新对话与创build新文档)方面具有更好的性能?

还是有没有更好的方法(如我的第二种方法,我不知道是否是一个好的devise,在每个文档中指定参与者字段)?

谢谢!

根据您的消息应用程序的示例数据,您可以做的是有两个集合:对话和消息。 关系是一个对话有很多消息。

 Conversation: { id: 123 participants: ['john', 'marry'], } Message: { sender: 'john', content: 'howdy', time_created: new Date(), converstationId: 123 }, { sender: 'marry', content: 'good u', time_created: new Date(), converstationId: 123 }, 

在这种情况下创build一个新的文档信息会更好,因为你可以有两个应用程序(1个用于john,1个用于结婚),而不处理它们两个更新同一个文档的可能性。 他们恰好在分享同一个会话。

另外,如果一个对话是一个单一的文件,你可能会得到一个非常大的文件。 (文件增长关注)

你可以find更多关于这个mongodb文档的数据build模

http://docs.mongodb.org/manual/core/data-modeling-introduction/

另请参阅MongoDB:Socialite社交networking用例的示例/讨论。

希望能帮助到你。 干杯。