代码之家  ›  专栏  ›  技术社区  ›  Apichai

如何为Mongodb设计通知模式

  •  2
  • Apichai  · 技术社区  · 9 年前

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

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

    如果你有一个例子,请给我一些建议或一些链接来研究。

    谢谢

    1 回复  |  直到 9 年前
        1
  •  4
  •   campovski    9 年前

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

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