如何为 Mongodb 设计通知架构

Api*_*hai 4 schema mongodb

如果我将所有用户的通知存储在一个集合下是否更好:

{
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}})
Run Code Online (Sandbox Code Playgroud)

如果您有示例,请给我一些建议或一些学习链接。

谢谢。

cam*_*ski 7

是的,绝对建议将通知存储在自己的表中。这有助于保持数据库干净并将苹果与同行分开。所以你的桌子可能看起来像这样

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;
}
Run Code Online (Sandbox Code Playgroud)