node.js - MongoDB: best design for messaging app -


this question has answer here:

a simple design problem. want build facebook messenger. let's john , marry chatting, better approach?

1) 1 document per conversation, messages 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 new message (updating conversation vs. creating new document) ?

or there better approach (as in 2nd approach, i'm not sure if it's design specify participants field in each document)?

thanks!

based on example data messaging app, having 2 collections: conversation , messages. relationship 1 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 new document message better in case, can have 2 applications (1 john , 1 marry) without handling possibility of 2 of them updating same document. happens sharing same conversation session.

also, if conversation single document, might end large document. (document growth concern)

you can find out more data modelling mongodb doc

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

also see mongodb: socialite examples/discussion social network use case.

hope helps. cheers.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -