我想为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
}

最佳答案

根据this帖子,应该有3个节点,userconvomessagesconvo决定是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."
}
以及当参与者之一删除某些消息或对话本身时是否需要维护状态
该模型将是
conversation :{
id:String,
participants:[String], //user_id's
deleted_by:[String] //user_id's
....
}
message:{
  conversation_id:String,
  deleted_by:[String] //user_ids,
  content:String
  ...
}
在某些时候,所有参与者都删除了对话或消息,我添加了一个触发器pre save以检查是否参与者=== deleted_by,然后将其永久删除
它为我工作!

关于mongodb - 1 :1 and group chat schema in NoSQL/MongoDB,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35246145/

10-12 23:50