title: MongoDB : Snapchat like notification schema for the database
body: <p>I've done some research regarding the following subject and I wanted to get advice from more experienced developper to see if my solution was the best one.</p>
  
  <p>If you don't know what Snapchat is, it's a mobile application that let you share pictures with your friends. </p>
  
  <p>What I'am interested in is the notification part of the application, where the user checks if he has received new pictures. It looks like this :</p>
  
  <p><a href="https://i.stack.imgur.com/u6Ajo.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/u6Ajo.png" alt="snapchat notification view"></a></p>
  
  <p>What I want to do is find the best way to check, when the user opens the app or refresh the view, if he has received new images/messages using <strong>MongoDB</strong>.</p>
  
  <p>After some research the best solution is to have three collections in my Mongo database :  </p>
  
  <pre><code>User : User_ID
         Username
         PasswordHash
         ...
  </code></pre>
  
  <p>Where the User_ID is unique and generated by MongoDB</p>
  
  <pre><code>Messages: Message_ID
            Message_Content
            ...
  </code></pre>
  
  <p>Where the <em>Message_ID</em> is unique and generated by MongoDB. The collection could store other informations like an image URL (images stored in a BLOB), etc.</p>
  
  <pre><code>Notifications : Notification_ID
                  Sender_ID
                  Receiver_ID (index)
                  Message_ID
  </code></pre>
  
  <p>Where the Notification_ID is unique and generated by MongoDB. The Sender_ID and Receiver_ID are User_ID from the collection <em>User</em>. The <em>Message_ID</em> is from the collection <em>Messages</em>. The <em>Receiver_ID</em> field is indexed.</p>
  
  <p>So when an user launches or refresh the view, I query the collection <em>Notifications</em> with the field <em>Receiver_ID</em> (which is indexed) set to the actual <em>User_ID</em> to find all the messages he may have received. If he has received messages, I query the <em>Messages</em> collection to find all the information about the messages he received (with <em>Message_ID</em> obtained by the previous query one <em>Notifications</em>).</p>
  
  <p>After that, I delete the document(s) on the collection <em>Notifications</em> (if he has not yet opened the messages, I store the <em>Message_ID</em> locally on the device). </p>
  
  <p>To add documents to the <em>Notifications</em> and <em>Messages</em> collections, it is done when an user sends a new message.</p>
  
  <p>The field <em>Receiver_ID</em> from the <em>Notifications</em> collection is indexed to query faster. If I understand correctly indexes with MongoDB, the write operations are  a bit slowed down but the reading speed is a lot better. But just to be sure, the <em>Receiver_ID</em> index will be updated automatically when new documents are added to the collection <em>Notifications</em> or I'll have to update it manually ?</p>
  
  <p>Is it the best solution to solve this problem or is there a better one ?</p>
  
  <p>EDIT : There is another question I came accross :
  The <em>Receiver_ID</em> field is indexed so I can query faster the <em>Notifications</em> collections. Doing so let me get the <em>Message_ID</em> but after that, I need to query the <em>Messages</em> collection in order to retrieve all the information regarding the message. Should I also index the field <em>Message_ID</em> in the collection messages, knowing that this collection will store all the messages ever sent and it could take some time to find the corresponding message ?
  Or is the field _id is automatically indexed by Mongo so I wouldn't need to do that ?</p>
  
  <p>EDIT 2 : Regarding the question from the second EDIT, I found out that I don't need to index the <em>Message_ID</em> because all _id are indexed by default.</p>
  
  <blockquote>
    <p>MongoDB creates the _id index, which is an ascending unique index on the _id field, for all collections when the collection is created. You cannot remove the index on the _id field.</p>
  </blockquote>
  
  <p>You can learn more about it <a href="https://docs.mongodb.org/manual/core/index-single/#index-type-id" rel="nofollow noreferrer">here</a></p>
  
  <p>Thanks for reading !</p>
  
  <p>Here are some links I found when looking for a solution :</p>
  
  <p><a href="https://stackoverflow.com/questions/14997828/develop-database-schema-for-notify-like-facebook">Develop Database Schema for Notify like facebook</a></p>
  
  <p><a href="https://hashnode.com/post/how-to-implement-instant-notification-system-using-express-mongo-rest-api-ciibz8fg8019zj3xtzkwfx3hw" rel="nofollow noreferrer">How to implement instant notification system using Express + Mongo Rest API?</a></p>
  
  <p><a href="https://www.quora.com/Is-it-better-to-store-notifications-of-all-users-under-one-collection-table-OR-rather-provide-each-user-his-own-collection-table-to-store-notifications" rel="nofollow noreferrer">Is it better to store notifications of all users under one collection / table OR rather provide each user his own collection / table to store notifications?</a></p>