Mar*_*ria 16 database mongoose mongodb node.js express
一个非常简单的设计问题.说我想建立Facebook Messenger.让我们说John和Marry在聊天,这是一种更好的方法吗?
1)每个会话1个文档,messages是一个消息对象数组
{ participants: ['john', 'marry'],
messages: [
{ sender: 'john', content: 'howdy', time_created: new Date() },
{ sender: 'marry', content: 'good u', time_created: new Date() },
...
]
}
Run Code Online (Sandbox Code Playgroud)
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
....
Run Code Online (Sandbox Code Playgroud)
在插入新消息(更新会话与创建新文档)方面,哪种方法具有更好的性能?
或者是否有更好的方法(如我的第二种方法,我不确定在每个文档中指定参与者字段是否是一个好的设计)?
谢谢!
Wan*_*iar 23
根据您的消息传递应用程序的示例数据,您可以做的是拥有两个集合:对话和消息.关系是一个对话有很多消息.
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
},
Run Code Online (Sandbox Code Playgroud)
在这种情况下,创建新文档消息会更好,因为您可以有两个应用程序(1个用于john,1个用于结婚),而不会处理两个应用程序更新同一文档的可能性.他们碰巧正在分享同一个对话会话.
此外,如果对话是单个文档,您最终可能会得到一个非常大的文档.(文件增长问题)
您可以找到有关此mongodb文档的数据建模的更多信息
http://docs.mongodb.org/manual/core/data-modeling-introduction/
另请参阅MongoDB:社交网站,了解社交网络用例的示例/讨论.
希望能帮助到你.干杯.
| 归档时间: |
|
| 查看次数: |
8996 次 |
| 最近记录: |