【问题标题】:How to design Notifications schema for Mongodb如何为 Mongodb 设计通知模式
【发布时间】:2018-02-13 08:02:58
【问题描述】:

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

{
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}})

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

谢谢。

【问题讨论】:

    标签: mongodb schema


    【解决方案1】:

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

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

    【讨论】:

    • 是的,我们应该有发送者和接收者字段谢谢兄弟的建议:)
    • 如果我有多个外键,首选方法是什么? type: string; // or preferable ForeignKey to another table, in which you store possible types (see normalization of database)
    • @AbhiBurk 是的,但这不是 OP 所要求的。此外,必须重写其他功能
    • 例如,您可以添加字段 'html' 并存储类似 的内容,以便进行概括
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-21
    • 2015-06-27
    • 2011-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多