Apichai
Apichai

Reputation: 335

How to design Notifications schema for Mongodb

Is it bette if I store notifications of all users under one collection:

{
id_: Object,
type: String, // post or message
ref_id: Number, // post id or message id
owner: Number,
read: Boolean,
created_at: Date 
}

// when I get notification for a single user
db.notifications.find({"owner": user_id, "read": {$ne: true}})

If you have an example please give me some advice or some link to study.

Thanks.

Upvotes: 5

Views: 7001

Answers (1)

campovski
campovski

Reputation: 3153

Yes, it is definitely recommended to store notifications in own table. This helps keeping your database clean and apples separated from peers. So your table might look like this

Notification
{
    id: int;
    sender: ForeignKey to user;
    receiver: ForeignKey to user;
    type: string; // or preferable ForeignKey to another table, in which you store possible types (see normalization of database)
    content: string;
    is_read: boolean;
    created_at: Date;
}

Upvotes: 7

Related Questions