如何为Mongodb设计通知模式

如何为Mongodb设计通知模式,mongodb,schema,Mongodb,Schema,如果我将所有用户的通知存储在一个集合下是否更好: { 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,

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

{
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}})
如果你有一个例子,请给我一些建议或一些链接来研究


谢谢。

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

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;
}

是的,我们应该有发送者和接收者字段谢谢你的建议兄弟:)如果我有多个外键,首选的方式是什么<代码>类型:字符串;//或者,最好将ForeignKey转换为另一个表,在该表中存储可能的类型(请参见数据库规范化)@AbhiBurk是的,但这不是OP所需要的。此外,还必须重写其他功能。例如,为了通用化,您可以添加字段“html”并存储类似的内容