NoSQL / MongoDB中的1:1和群聊模式

时间:2016-02-06 20:33:19

标签: mongodb nosql

我想为1:1和群聊活动创建聊天应用。

我为两种情况都创建了一个模式:

{ // group 
    "id": 1 // id of the group
    "name": "Chat Group" // name of group; if there are more than 2 members
    "members": [ "member1", "member2", ...] // ids of the group chat members; only they have access to the JSON document
    "chatlog": [ "timestamp1": ["member1", "message1"], "timestamp2": ["member2", "message2"], ...] // who sent which message in chronological order
}

如上所示,将用户的访问列表存储在“members”数组中会更好吗?或者这个解决方案更好"chat_groups"

{ // user 
    "id": 1 // id of the user
    "name": "Max" // name of the user
    "chat_groups": [ "1", "2", ...] // ids of the groups, the user is a member of
}

1 个答案:

答案 0 :(得分:0)

根据this帖子,应该有3个节点,userconvomessages。并且convo决定天气是1:1聊天或群聊。此外,您可以在creator中添加另一个属性convo来设置群组管理员权限。

示例:用户

{ // user
   "id":123,
   "name":"Omkar Todkar"
}

示例: Convo

{ // group
   "id":1,
   "members": [123, 234, 345]
   "creator":123
},
{ // 1:1
   "id":2,
   "members": [123, 345]
   "creator":123
}

示例:消息

{ // group message
   "convo_id:1,
   "author":123,
   "content":"Welcome, to our new group."
},
{ // 1:1 message
   "convo_id:2,
   "author":123,
   "content":"Hey, you wanna join us in group?."
},
{ // 1:1 message
   "convo_id:2,
   "author":345,
   "content":"Sure, I would love to join."
},
{ // group chat
   "convo_id:1,
   "author":234,
   "content":"Hi, Happy to see you both here."
}