Sunday, August 21, 2016

Notification Schema Design in MongoDB

 I was searching for a best solution to design a notification schema in No-SQL database. I was using MongoDB and Mongoose ORM in Node app. I had to maintain the idea of scalability. That's why I tried to keep my schema in a single collection instead of using multiple collections.
As I am using No-SQL, I must try to use the benefits of document basis database. After researching a lot, I wrote my schema as the flowing way. It has helped me to keep scalability in my application and done the best solution for multiple notification features.

NotificationSchema   = new Schema({
    sender: {type:mongoose.Schema.Types.ObjectId, ref:'User'}, // Notification creator
    receiver: [{type:mongoose.Schema.Types.ObjectId, ref:'User'}], // Ids of the receivers of the notification
    message: String, // any description of the notification message 
     readerId:{type:mongoose.Schema.Types.ObjectId, ref:'User'},
     read_at: {type: Date, default:}
    created_at:{type: Date, default:},

Why this looks best notification schema to me:
Let's explain the schema, I wrote here and what things can be done with this solution.

Sender: I have keep sender field as an object, object is referred to user collection for population. Because, the notification happens generally for a single action, I mean it may create from the activity of a single person. So, the sender is who is sending this notification, the source of the notification.

Receiver: I have keep the receiver field as an array of objects and the object is referred to user collection, if needed for population. The receiver can be one person or many persons. So, the receiver field will hold the IDs of the recipients.

Messagemessage field is used for holding the notification message details.

Read_by: the read_by field is an array of two object properties. this object contain readerId(user id) and read_at (the seen date-time). The read_by array reduced many works and has given more advanced option to work with. It will be used to track each recipient's view time to track. So, the notification sender can track which recipient has read the message in which time.

Created_at: this field simply holds the notification creation date and time.

So, this schema is very helpful to use for making notification from one to one and one to many. That's why I have considered it as a best notification schema for MongoDB database. If you have any suggestion or best solution to improve the idea, you can let me know by comments.

No comments:

Post a Comment