Reputation: 341
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.
If you don't know what Snapchat is, it's a mobile application that let you share pictures with your friends.
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 :
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 MongoDB.
After some research the best solution is to have three collections in my Mongo database :
User : User_ID
Username
PasswordHash
...
Where the User_ID is unique and generated by MongoDB
Messages: Message_ID
Message_Content
...
Where the Message_ID is unique and generated by MongoDB. The collection could store other informations like an image URL (images stored in a BLOB), etc.
Notifications : Notification_ID
Sender_ID
Receiver_ID (index)
Message_ID
Where the Notification_ID is unique and generated by MongoDB. The Sender_ID and Receiver_ID are User_ID from the collection User. The Message_ID is from the collection Messages. The Receiver_ID field is indexed.
So when an user launches or refresh the view, I query the collection Notifications with the field Receiver_ID (which is indexed) set to the actual User_ID to find all the messages he may have received. If he has received messages, I query the Messages collection to find all the information about the messages he received (with Message_ID obtained by the previous query one Notifications).
After that, I delete the document(s) on the collection Notifications (if he has not yet opened the messages, I store the Message_ID locally on the device).
To add documents to the Notifications and Messages collections, it is done when an user sends a new message.
The field Receiver_ID from the Notifications 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 Receiver_ID index will be updated automatically when new documents are added to the collection Notifications or I'll have to update it manually ?
Is it the best solution to solve this problem or is there a better one ?
EDIT : There is another question I came accross : The Receiver_ID field is indexed so I can query faster the Notifications collections. Doing so let me get the Message_ID but after that, I need to query the Messages collection in order to retrieve all the information regarding the message. Should I also index the field Message_ID 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 ?
EDIT 2 : Regarding the question from the second EDIT, I found out that I don't need to index the Message_ID because all _id are indexed by default.
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.
You can learn more about it here
Thanks for reading !
Here are some links I found when looking for a solution :
Develop Database Schema for Notify like facebook
How to implement instant notification system using Express + Mongo Rest API?
Upvotes: 4
Views: 2270
Reputation: 9473
the research provided looks impressive!
Regarding notification process - looks OK - but what if user uses more than one device (tablet, phone etc?) - that need to include device ID and status array per device
Index once created will be maintained by DB engine
Upvotes: 3