title: MongoDB Structure for message app
body: <p>I am breaking my mind up thinking about a good document structure for handling a message app.</p>
  
  <p>I basically need three (or four) types of objects:</p>
  
  <ol>
  <li>The user (username, email, password, etc.)</li>
  <li>The contacts list (containing different contacts or contacts groups)</li>
  <li>The conversation (a conversation is a collection of messages between some persons)</li>
  <li>The message (contains the message body, some timestamp and the creator.)</li>
  </ol>
  
  <p>My idea was to embed the contacts into the user document and to embed the messages in a conversation document:</p>
  
  <p><strong>1. User</strong></p>
  
  <pre><code>{
      username: 'dev.puS',
      usernameCanonical: 'dev.pus', // used for unique constraints
      email: 'developement.pus@gmail.com,
      emailCanonical: 'developement.pus@gmail.com,
      salt: 'some hash',
      password: 'hash with salt',
      logs: { last_login: 12.06.2008, last_password_reset: 04.03.2007 },
      state: { online: true, available: false },
      contacts: [ user_id1, user_id2, user_id3 ]
  }
  </code></pre>
  
  <p><strong>2. Conversation</strong></p>
  
  <pre><code>{
      members: [ user_id1, user_id2 ],
      messages: [
          { author: user_2, body: 'Hi what's up' },
          { author: user_1, body: 'Nothing out here :(' },
          { author: user_2, body: 'Whanna ask some question on stackoverflow' },
          { author: user_1, body: 'Okay, lets go' }
      ]
  }
  </code></pre>
  
  <p>What do you think about this schema?</p>
  
  <p>I think it would be better to keep them seperated (so each document for it's own) because each document has different update frequency. But I really don't have any experience about it so it would be good to hear some advices :)</p>
  
  <p>Regards</p>