title: MongoDB: best design for messaging app
body: <p>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? </p>
  
  <p>1) <strong>1 document per conversation</strong>, <code>messages</code> is an array of message object</p>
  
  <pre><code>{ participants: ['john', 'marry'], 
    messages: [ 
        { sender: 'john', content: 'howdy', time_created: new Date() },
        { sender: 'marry', content: 'good u', time_created: new Date() },
        ...
    ]
  }
  </code></pre>
  
  <p>2) <strong>1 document per message</strong></p>
  
  <pre><code>{ 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
  .... 
  </code></pre>
  
  <p>Which approach has better performance in terms of inserting a new message (updating a conversation vs. creating a new document) ?</p>
  
  <p>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)?</p>
  
  <p>Thanks!</p>