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

MJQ*_*347 5 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
}
Run Code Online (Sandbox Code Playgroud)

如上所示,将用户的访问列表存储在“成员”数组中会"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
}
Run Code Online (Sandbox Code Playgroud)

Omk*_*kar 8

根据这篇文章,应该有 3 个节点userconvomessages。并convo决定是1:1聊天还是群聊。此外,还可以添加其他财产creatorconvo设置组管理员权限。

示例:用户

{ // user
  "id": 123,
  "name": "Omkar Todkar"
}
Run Code Online (Sandbox Code Playgroud)

示例: Convo

{ // group
  "id": 1,
  "members": [123, 234, 345]
  "creator": 123
},
{ // 1:1
  "id": 2,
  "members": [123, 345]
  "creator": 123
}
Run Code Online (Sandbox Code Playgroud)

示例:消息

{ // 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."
}
Run Code Online (Sandbox Code Playgroud)

如果您需要在参与者之一删除某些消息或对话本身时保持状态,则模型将是

conversation :{
id:String,
participants:[String], //user_id's
deleted_by:[String] //user_id's
....
}
Run Code Online (Sandbox Code Playgroud)
message:{
  conversation_id:String,
  deleted_by:[String] //user_ids,
  content:String
  ...
}
Run Code Online (Sandbox Code Playgroud)

在某些时候,所有参与者都会删除对话或消息,我添加了一个触发器预存来检查,如果参与者 ===deleted_by 然后永久删除对话或消息

它对我有用!