title: How to structure Firestore database in chat app?
body: <p>The simplest way of storing chat messages is probably this:</p>
  
  <pre><code>message:
   -message1 {
     "user1"
     "user2"
     "message"
     "date"
    }
   -message2
   -message3
  </code></pre>
  
  <p>When the app grows in size (a lot of messages), and operations on database are done with <code>.whereEqualTo</code> are there any disadvantages of structuring chat app messages this way? Like with database iterating through all messages ?</p>
  
  <p>Because if there are problems with this approach, i've seen for example this way of structuring database, that would segregate messages in different chat rooms</p>
  
  <pre><code>chats: {
      -chat1 {
        "lastMessage"
        "timestamp"
        "users": {user1, user2}
      }
      -chat2
      -chat3
    }
  
  messages: {
   -chat1 {
    "message"
    "date"
   }
  }
  </code></pre>
  
  <p>But in this example, adding new message would require user to make 2 write operations, one writing new message and one updating <code>chat</code> document with new <code>lastMessage</code> and <code>timestamp</code> client-side, or creating a cloud  function to update <code>chat</code> document with new values, when new message is added. </p>
  
  <p>So, is the first option valid, or should I go with something like the second example?</p>