title: MongoDB Schema Design - Real-time Chat
body: <p>I'm starting a project which I think will be particularly suited to MongoDB due to the speed and scalability it affords.</p>
  
  <p>The module I'm currently interested in is to do with real-time chat.  If I was to do this in a traditional RDBMS I'd split it out into:</p>
  
  <ul>
  <li>Channel (A channel has many users)</li>
  <li>User (A user has one channel but many messages)</li>
  <li>Message (A message has a user)</li>
  </ul>
  
  <p>The the purpose of this use case, I'd like to assume that there will be typically 5 channels active at one time, each handling at most 5 messages per second.</p>
  
  <p>Specific queries that need to be fast:</p>
  
  <ul>
  <li>Fetch new messages (based on an bookmark, time stamp maybe, or an incrementing counter?)</li>
  <li>Post a message to a channel</li>
  <li>Verify that a user can post in a channel</li>
  </ul>
  
  <p>Bearing in mind that the document limit with MongoDB is 4mb, how would you go about designing the schema?  What would yours look like?  Are there any gotchas I should watch out for?</p>