2016-02-06 13 views
2

我想爲1:1和羣聊活動創建聊天應用程序。NoSQL/MongoDB中的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 
} 

它會更好,以存儲用戶的「成員」數組訪問列表就像上面說的,或者這是解決與"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 
} 
+0

模式的選擇應該是由應用程序的使用和大小決定。假設它是一個小型的演示應用程序,聊天組或用戶的所有信息都可以來自單個集合。當用戶看到他的儀表板並想要開始或加入與用戶或組。 – Avi

回答

0

根據this後應該有3個節點,user,convomessages。並且convo決定天氣它是1:1聊天或羣聊。此外,您可以在convo中添加另一個屬性creator來設置組管理權限。

示例:用戶

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

實施例:康沃

{ // 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." 
}