MongoDB:消息应用程序的最佳设计 [英] MongoDB: best design for messaging app
问题描述
1)每个会话 1个文档,消息
是一组消息对象
{参与者:['john','marry'],
消息:[
{发件人: 'john',content:'howdy',time_created:new Date()},
{sender:'marry',content:'good u',time_created:new Date()},
.. 。
]
}
2)每个消息1个文档
{参与者:['john','marry'],发件人:'john',message:'howdy ',time_created:new Date()} // document 1
{参与者:['john','marry'],sender:'marry',message:'good u',time_created:new Date() //文件2
....
哪种方法在插入方面有更好的表现一个新消息(更新一个对话而不是创建一个新的文档)?
还是有更好的方法像我的第二种方法,我不知道这是否是一个很好的设计来指定每个文档中的参与者字段)?
谢谢!
根据您的消息传递应用的示例数据,您可以做的是拥有两个集合:对话和消息。
关系是一个对话有很多消息。
对话:
{id:123
参与者:['john','marry'],
}
消息:
{sender:'john',
content: 'howdy',
time_created:new Date(),
converstationId:123
},
{sender:'marry',
content:'good u'
time_created:new Date(),
converstationId:123
},
在这种情况下,创建新的文档消息将会更好,因为您可以有两个应用程序(1个用于john和1进行结婚),而无需处理两个应用程序更新同一文档的可能性。
他们恰好是共享同一个对话会话。
此外,如果会话是单个文档,则可能会出现一个非常大的文档。 (文档增长关注)
您可以找到有关此mongodb文档的数据建模的更多信息。
http://docs.mongodb.org/manual/core/data-modeling-introduction/
另请参阅 MongoDB:Socialite < a>为社会网络用例的例子/讨论。
希望有帮助。
干杯。
A very simple design problem. Say I want to build Facebook Messenger. Let's say John and Marry are chatting, which is a better approach?
1) 1 document per conversation, messages
is an array of message object
{ participants: ['john', 'marry'],
messages: [
{ sender: 'john', content: 'howdy', time_created: new Date() },
{ sender: 'marry', content: 'good u', time_created: new Date() },
...
]
}
2) 1 document per message
{ 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
....
Which approach has better performance in terms of inserting a new message (updating a conversation vs. creating a new document) ?
or are there any better approach (as in my 2nd approach, i'm not sure if it's a good design to specify the participants field in each document)?
Thanks!
Based on your example data for the messaging app, what you could do is having two collections: Conversation and Messages. Where the relationship is one Conversation have many Messages.
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
},
Creating a new document message would be better in this case, as you can then have two applications (1 for john and 1 for marry) without handling the possibility of the two of them updating the same document. They just happens to be sharing the same conversation session.
Also, if a conversation is a single document, you might end up with a very large document. (Document growth concern)
You can find out more about data modelling for this mongodb doc
http://docs.mongodb.org/manual/core/data-modeling-introduction/
Also see MongoDB: Socialite for examples/discussion for social network use case.
Hope it helps. Cheers.
这篇关于MongoDB:消息应用程序的最佳设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!